I am using react naitve with UI-kitten
and formik
to build a registration form. I am using select component of ui kitten
inside formik
. I am not been able to get the selected value from the component. It gives me the index of the selected item (in console.warn())
and the selected item does't appear in the field. It works fine with input and datePicker
component.
const formSchema = yup.object({
name: yup.string().required("*Full Name is required"),
gender: yup.string().required("*Gender is required"),
dob: yup.string().required("*Date of birth is required"),})
export default class Registration extends Component {
render() {
return (
<Formik
validationSchema={formSchema}
initialValues={{
name: '',
gender: '',
dob: '',
}}
onSubmit={(values) => {
console.log(values)
}}
>
{(props) => (
<ScrollView style={styles.containerStyle}>
<LinearGradient style={{ height: '100%' }}
colors={["#0C1248", "#D03737"]}>
<ScrollView contentContainerStyle={styles.scrollViewStyle}>
<Text style={styles.textStyle} category='h2'>Registration</Text>
<Input
style={styles.inputView}
value={props.values.name}
status='primary'
placeholder="Full Name"
onChangeText={props.handleChange('name')}
/>
<Text style={styles.errorText}>{props.touched.name && props.errors.name}</Text>
<Datepicker
style={styles.inputView}
date={props.values.dob}
placeholder="Date of birth"
onSelect={(dob) => { props.setFieldValue('dob', dob) }}
/>
<Text style={styles.errorText}>{props.touched.dob && props.errors.dob}</Text>
<Select
style={styles.inputView}
value={props.values.gender}
placeholder='Gender'
onSelect={item => props.setFieldValue(
'gender', item.value
)}
>
<SelectItem title='Male' />
<SelectItem title='Female' />
</Select>
<TouchableOpacity style={{ alignItems: "center", justifyContent: 'center' }}>
<LinearGradient style={{ width: width / 1.25, padding: 12, borderRadius: 30 }}
colors={["#D03737", "#0C1248"]}>
<Text style={{ color: "white", textAlign: "center" }} onPress={props.handleSubmit}>Next</Text>
</LinearGradient>
</TouchableOpacity>
</ScrollView>
</LinearGradient>
</ScrollView>
)}
</Formik>
);
}
}
You should refer the documentation https://akveo.github.io/react-native-ui-kitten/docs/components/select/overview#select
The select component doenst have a value prop, it has a selectedIndex props which will chose the item on the given index.
In your case you can do something like this
<Select
style={styles.inputView}
value={props.values.gender}
placeholder='Gender'
onSelect={item => props.setFieldValue(
'gender', item.row == 0 ? 'Male' : 'Female'
)}
>
<SelectItem title='Male' />
<SelectItem title='Female' />
</Select>
Based on the state value you can find the index and vise versa. This will make sure that Select always getting an index value and your state always having a string.