React Hook Form with Select component on React Native #8187
-
Hello react-hook-form folks, I have used React Hook form on my React Native app for quite a while without any issues until recently I tried to implement a Select component. DemoHere's an expo snack on the example. ContextThis select component comes from a component library called UI Kittens, and its Select component doesn't come with a onChange method like the other usual components. It takes in props I assumed the Questions
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
https://snack.expo.dev/@bluebill1049/react-hook-form-with-ui-kitten-select import { IndexPath, Select, SelectItem, Text } from '@ui-kitten/components';
import React from 'react';
import { Controller, useForm } from 'react-hook-form';
import { StyleSheet, TouchableOpacity, View } from 'react-native';
const timeUnitOptions = ['days', 'hours', 'weeks'];
const ReminderForm = ({ onSubmit }) => {
const [selectedIndex, setSelectedIndex] = React.useState(new IndexPath(2));
const {
control,
handleSubmit,
formState: { errors },
} = useForm({
mode: 'onBlur',
defaultValues: {
timeUnit: 'weeks',
},
});
return (
<View style={styles.container}>
<Text category={'label'}>Add a reminder</Text>
<View style={styles.row}>
<View style={styles.formField}>
<Controller
name="timeUnit"
control={control}
rules={{
required: true,
}}
render={({ field: { onChange, value } }) => {
console.log(value)
return (
<Select
accessibilityLabel="time unit"
value={value}
onSelect={(index) => {
onChange(timeUnitOptions[index.row]);
}}>
{timeUnitOptions.map((timeUnit) => (
<SelectItem
key={`select-option-${timeUnit}`}
title={timeUnit}
/>
))}
</Select>
);
}}
/>
</View>
<TouchableOpacity onPress={handleSubmit(onSubmit)}>
Submit
</TouchableOpacity>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
height: '100%',
},
formField: {
width: 130,
marginEnd: 8,
},
row: {
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
paddingVertical: 16,
},
});
export default ReminderForm; are the above works for you? |
Beta Was this translation helpful? Give feedback.
https://snack.expo.dev/@bluebill1049/react-hook-form-with-ui-kitten-select