|
| 1 | +--- |
| 2 | +title: React Native Picker Select |
| 3 | +--- |
| 4 | + |
| 5 | +[React Native Picker Select](https://github.com/lawnstarter/react-native-picker-select) component for React Native which emulates the native `<select>` interfaces for iOS and Android. |
| 6 | + |
| 7 | +> ⚠️ All examples below are using **TypeScript**, if you're not using it you can simply remove all type definitions as the `React.FC<Props>` from component definition. |
| 8 | +
|
| 9 | +[Click here](https://snack.expo.io/@eliasgcf/unform-react-native-picker-select-example?session_id=snack-session-I6e6cmW7z&preview=true&platform=ios&iframeId=7pnhjsvupn&theme=light&supportedPlatforms=android,ios) to access a React Native Picker Select Live Demo. |
| 10 | + |
| 11 | +## Picker Component |
| 12 | + |
| 13 | +```tsx lineNumbers=true |
| 14 | +import React, { useEffect, useRef, useState } from 'react'; |
| 15 | +import Picker, { PickerSelectProps } from 'react-native-picker-select'; |
| 16 | +import { useField } from '@unform/core'; |
| 17 | + |
| 18 | +interface Props extends Omit<PickerSelectProps, 'onValueChange'> { |
| 19 | + name: string; |
| 20 | +} |
| 21 | + |
| 22 | +const RNPickerSelect: React.FC<Props> = ({ name, items, ...rest }) => { |
| 23 | + const pickerRef = useRef(null); |
| 24 | + const { fieldName, registerField, defaultValue = '' } = useField(name); |
| 25 | + |
| 26 | + const [selectedValue, setSelectedValue] = useState(defaultValue); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + registerField({ |
| 30 | + name: fieldName, |
| 31 | + ref: pickerRef.current, |
| 32 | + getValue: ref => { |
| 33 | + return ref.props.value || ''; |
| 34 | + }, |
| 35 | + clearValue: ref => { |
| 36 | + ref.props.onValueChange(ref.props.placeholder.value); |
| 37 | + }, |
| 38 | + setValue: (_, value: string) => { |
| 39 | + setSelectedValue(value); |
| 40 | + }, |
| 41 | + }); |
| 42 | + }, [fieldName, registerField]); |
| 43 | + |
| 44 | + return ( |
| 45 | + <Picker |
| 46 | + ref={pickerRef} |
| 47 | + value={selectedValue} |
| 48 | + onValueChange={setSelectedValue} |
| 49 | + items={items} |
| 50 | + {...rest} |
| 51 | + /> |
| 52 | + ); |
| 53 | +}; |
| 54 | + |
| 55 | +export default RNPickerSelect; |
| 56 | +``` |
| 57 | + |
| 58 | +## Usage example |
| 59 | +```tsx lineNumbers=true |
| 60 | +const App: React.FC = () => { |
| 61 | + const formRef = useRef<FormHandles>(null); |
| 62 | + |
| 63 | + const pickerOptions = [ |
| 64 | + { value: 'diego3g', label: 'Diego Fernandes' }, |
| 65 | + { value: 'EliasGcf', label: 'Elias Gabriel' }, |
| 66 | + ]; |
| 67 | + |
| 68 | + function handleSubmit(data) { |
| 69 | + /** |
| 70 | + * Out example when 'Diego Fernandes' marked: { user: "diego3g" } |
| 71 | + * You get a string with the value |
| 72 | + */ |
| 73 | + console.log(data); |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <View style={{ flex: 1, justifyContent: 'center', alignContent: 'center' }}> |
| 78 | + <Form |
| 79 | + ref={formRef} |
| 80 | + onSubmit={handleSubmit} |
| 81 | + initialData={{ user: pickerOptions[0].value }} |
| 82 | + > |
| 83 | + <RNPickerSelect name="user" items={pickerOptions} /> |
| 84 | + |
| 85 | + <Button |
| 86 | + onPress={() => formRef.current.submitForm()} |
| 87 | + title="console.log" |
| 88 | + /> |
| 89 | + </Form> |
| 90 | + </View> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +export default App; |
| 95 | +``` |
0 commit comments