lawnstarter / react-native-picker-select

🔽 A Picker component for React Native which emulates the native <select> interfaces for iOS and Android
https://npmjs.com/package/react-native-picker-select
MIT License
1.73k stars 491 forks source link

Not able to select Number values since @react-native-picker/picker upgrade to 2.6.1 #530

Open fahad86 opened 7 months ago

fahad86 commented 7 months ago

Describe the bug
If value is a number, then it always defaults to the first item

Issue started happening after upgrading peer dependancy: @react-native-picker/picker to version 2.6.1

To Reproduce
change values to numbers

Expected behavior
Value selected should change when using the picker

Screenshots
n/a

Additional details

Reproduction and/or code sample

wesleyvandevoorde commented 6 months ago

We are having the same issue, also objects as values don't seem to work. Only strings are working

karo-dc commented 6 months ago

Same here, any update?

joeldebont commented 6 months ago

I'm having the same issue on react-native 73.2.

zabojad commented 5 months ago

Has anyone found a solution?

dpatra commented 5 months ago

Any Solutions please

zabojad commented 5 months ago

use strings only with picker and convert them to numbers when needed...

girardinsamuel commented 4 months ago

I experienced the same issue ! I will convert values to strings in the meantime.

andrejpavlovic commented 4 months ago

Seems issue originates from here: https://github.com/react-native-picker/picker/issues/538

hardchopshoppirate commented 3 months ago

Temporary solution: create a wrapper component that converts a value (if it is a number) to a string, then back to the original type. Cannot use prev version due to expo 50 complains. That is the only way it may work for me :(

Pseudocode below:

type SelectProps = {
  value: string | number;
  options: { value: string | number; label: string };
  onChange: (value: any) => void;
}

const Select = ({ selectedValue, options, onChange }) => {
  const originalValueType = useRef(
    detectType(value),
  );

  const valueString = String(selectedValue);
  const options = options.map((item) => ({ label: item.label, value: String(item.value) }));
  const handleChange = (value: any) => {
    onChange(
      convertToType(value, originalValueType),
     );
  };

  return (
    <RNPickerSelect
      value={valueString}
      items={options}
      onValueChange={handleChange}
    />
  );
}