react-native-picker / picker

Picker is a cross-platform UI component for selecting an item from a list of options.
MIT License
1.49k stars 280 forks source link

Cannot open or close picker by focus and blur methods. #293

Open bhardwajMehul opened 3 years ago

bhardwajMehul commented 3 years ago
const pickerRef = useRef();

function open() {
  pickerRef.current.focus();
}

function close() {
  pickerRef.current.blur();
}

return <Picker
  ref={pickerRef}
  selectedValue={selectedLanguage}
  onValueChange={(itemValue, itemIndex) =>
    setSelectedLanguage(itemValue)
  }>
  <Picker.Item label="Java" value="java" />
  <Picker.Item label="JavaScript" value="js" />
</Picker>

I'm unable to open or close picker by the focus and blur methods. The current attribute does not contains the methods.

Instead these methods should be available in the Picker class only. like https://github.com/beefe/react-native-picker

MathieuRA commented 3 years ago

I got the same issue. RN: 0.63.2 "@react-native-picker/picker": "1.9.2"

andrespaggy commented 3 years ago

That's my versions: RN 0.64.2 "@react-native-picker/picker": "^1.16.4"

My component does work. That's my snippet:

const App = () => {
  const pickerRef = useRef();

  function openPicker() {
    pickerRef.current.focus();
  }

  function closePicker() {
    pickerRef.current.blur();
  }

  return (
    <>
      <Button title="Press me" onPress={openPicker}
      <Picker
        ref={pickerRef}
        selectedValue={selectedLanguage}
        onValueChange={(itemValue, itemIndex) =>
          setSelectedLanguage(itemValue)
        }>
        <Picker.Item label="Java" value="java" />
        <Picker.Item label="JavaScript" value="js" />
      </Picker>
    </>
  )
}

In your snippet the call to open() function is missing, so if you want to share the full snippet I'm happy to help.

bhardwajMehul commented 3 years ago

Time being I user https://github.com/beefe/react-native-picker this one. can you please tell from where this useRef coming from?

bitshop commented 2 years ago

@bhardwajMehul import {useRef} from "react"