Open Diego-Romero opened 5 years ago
Any update on this?
I have also encountered this as an issue. In Typescript you essentially cannot have a multi-select form field in Formik.
Formik doesn't have anything to do with this. You can have a multi-select form field in Formik.
https://codesandbox.io/s/blissful-wilson-j0sy4?file=/index.tsx
You can also create your own form fields in Formik, which could apply strong typing to your select box. Unfortunately, it doesn't seem that the library that @Diego-Romero is using maps the values properly.
Docs showing multi select:
https://formik.org/docs/api/field#example
You can do something like:
interface SelectOption<Value> {
label: string,
value: Value,
};
const MultiSelect = <Value extends string>(props: {
name: string,
options: SelectOption<Value>
}) => {
const [field] = React.useField<Value[]>({
name: props.name,
// values has proper types
validate: (values) => values.every(value => !!options.find(option => option.value === value)),
});
return <select name={props.name} value={field.value} multiple>
{props.options.map(option => (
<option key={value} value={value}>{option.label ?? option.value}</option>
))}
</select>;
}
Ah I see thank you! This link for formik migration is helpful too: https://formik.org/docs/migrating-v2
I had the same issue and I found a solution to my problem. I didn't use the above way that was suggested but I put a single parameter in my code and the error disappeared!
example code:
problematic code:
<select ... multiple >
<option ... >
....
</option>
</select>
solution:
<select ... multiple value={ value }>
<option ... >
....
</option>
</select>
where value is the value you get from yup validation via .test() case
I hope this helps
🐛 Bug report
Current Behavior
React Bootstrap Select multiple showing error in Formik as it is expecting a string but returns array. Currently the code won't compile the way it currently is, Nor it would allow me to select multiple options or get any when the form submits.
Error: Warning: The value prop supplied to must be an array if multiple is true.
Check the render method of FormControl.
I believe that the error is on FormControl.d.ts inside Formik because it only accepts a string as a type
This is the Interface that Formik exposes
StackOverflow issue: https://stackoverflow.com/questions/57149598/react-bootstrap-select-multiple-showing-error-in-formik-as-it-is-expecting-a-str
Expected behavior
I should be able to pass an array of string as that is what I'm expecting back
Reproducible example
Suggested solution(s)
Update the interface to accept string[] too?
Your environment