diff --git a/src/Select.tsx b/src/Select.tsx index 290e27b9..93bbdc7d 100644 --- a/src/Select.tsx +++ b/src/Select.tsx @@ -11,14 +11,35 @@ export interface SelectProps export const fieldToSelect = ({ field, - form: { isSubmitting }, + form: { isSubmitting, setFieldValue }, disabled = false, ...props }: SelectProps): MuiSelectProps => { + const onChange = React.useCallback( + (event: React.ChangeEvent<{ value: unknown }>) => { + // Special case for multiple and native + if (props.multiple && props.native) { + const { options } = event.target as HTMLSelectElement; + const value: string[] = []; + for (let i = 0, l = options.length; i < l; i += 1) { + if (options[i].selected) { + value.push(options[i].value); + } + } + + setFieldValue(field.name, value); + } else { + field.onChange(event); + } + }, + [field.name, props.multiple, props.native] + ); + return { disabled: isSubmitting || disabled, ...props, ...field, + onChange, }; }; diff --git a/stories/Select.story.tsx b/stories/Select.story.tsx index 6c5e3505..f18744d5 100644 --- a/stories/Select.story.tsx +++ b/stories/Select.story.tsx @@ -29,80 +29,121 @@ interface Values { pets: string[]; } -export default withStyles(styles)(({ classes }: WithStyles) => ( - - - validationSchema={schema} - initialValues={{ age: '', pets: [] }} - onSubmit={(values, { setSubmitting }) => { - setTimeout(() => { - setSubmitting(false); - action('submit')(values); - }, 2000); - }} - render={({ submitForm, values }) => ( -
- - Age - - Ten - Twenty - Thirty - - -
- - - Age (Empty) - - - - None - - Ten - Twenty - Thirty - - -
- - Pets - - Dogs - Cats - Rats - Snakes - - -
- -
- - - )} - /> -
-)); +export default withStyles(styles)(({ classes }: WithStyles) => { + return ( + + + validationSchema={schema} + initialValues={{ age: '', pets: [] }} + onSubmit={(values, { setSubmitting }) => { + setTimeout(() => { + setSubmitting(false); + action('submit')(values); + }, 2000); + }} + render={({ submitForm, values }) => ( +
+ + Age + + Ten + Twenty + Thirty + + +
+ + + Age (Empty) + + + + None + + Ten + Twenty + Thirty + + +
+ + Pets + + Dogs + Cats + Rats + Snakes + + +
+ + + Native Age + + + + + + + + +
+ + + Native Pets + + + + + + + + +
+ +
+ + + )} + /> +
+ ); +});