Does anybody have a working example of a react-native multi-select with formik? I already tried
import SectionedMultiSelect from 'react-native-sectioned-multi-select';
but the value array doesn't get submitted, but instead get an empty array.
Thank you.
CodePudding user response:
there you have, hope you're reading the docs for both libraries. I'm doing that and it was just joining the code. it should work.
// Formik x React Native example
import React from 'react';
import { Button, TextInput, View } from 'react-native';
import { Formik } from 'formik';
import Icon from 'react-native-vector-icons/MaterialIcons';
import SectionedMultiSelect from 'react-native-sectioned-multi-select';
const items = [
{
name: 'Fruits',
id: 0,
// these are the children or 'sub items'
children: [
{ name: 'Apple', id: 10 },
{ name: 'Strawberry', id: 17 },
],
},
];
export const MyReactNativeForm = props => (
<Formik
initialValues={{ items: [] }}
onSubmit={values => console.log(values)}
>
{({ handleChange, handleBlur, handleSubmit, values }) => (
<View>
<SectionedMultiSelect
items={items}
IconRenderer={Icon}
uniqueKey="id"
subKey="children"
selectText="Choose some things..."
showDropDowns={true}
readOnlyHeadings={true}
onSelectedItemsChange={handleChange('items')}
selectedItems={values.items}
/>
<Button onPress={handleSubmit} title="Submit" />
</View>
)}
</Formik>
);
CodePudding user response:
this is the example
import React, { Component } from 'react';
import { View } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
import SectionedMultiSelect from 'react-native-sectioned-multi-select';
const items = [
// this is the parent or 'item'
{
name: 'Fruits',
id: 0,
// these are the children or 'sub items'
children: [
{
name: 'Apple',
id: 10,
},
{
name: 'Strawberry',
id: 17,
},
{
name: 'Pineapple',
id: 13,
},
{
name: 'Banana',
id: 14,
},
{
name: 'Watermelon',
id: 15,
},
{
name: 'Kiwi fruit',
id: 16,
},
],
},
{
// next parent item
...
},
];
export default class App extends Component {
constructor() {
super();
this.state = {
selectedItems: [],
};
}
onSelectedItemsChange = (selectedItems) => {
this.setState({ selectedItems });
};
render() {
return (
<View>
<SectionedMultiSelect
items={items}
IconRenderer={Icon}
uniqueKey="id"
subKey="children"
selectText="Choose some things..."
showDropDowns={true}
readOnlyHeadings={true}
onSelectedItemsChange={this.onSelectedItemsChange}
selectedItems={this.state.selectedItems}
/>
</View>
);
}
}