Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit e187cf1

Browse files
EliasGcfdiego3g
andauthored
docs: Add react-native-picker-select example
Co-authored-by: Diego Fernandes <diego.schell.f@gmail.com>
1 parent af7d1bf commit e187cf1

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

docs/config/sidebar.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
link: '/examples/react-input-mask'
7272
- label: 'React Native Input Mask'
7373
link: '/examples/react-native-input-mask'
74+
- label: 'React Native Picker Select'
75+
link: '/examples/react-native-picker-select'
7476

7577
- label: More
7678
items:
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)