forked from stackworx/formik-mui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checkbox.tsx
34 lines (30 loc) · 847 Bytes
/
Checkbox.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import * as React from 'react';
import MuiCheckbox, {
CheckboxProps as MuiCheckboxProps,
} from '@material-ui/core/Checkbox';
import { FieldProps } from 'formik';
export interface CheckboxProps
extends FieldProps,
Omit<
MuiCheckboxProps,
'form' | 'checked' | 'defaultChecked' | 'name' | 'onChange' | 'value'
> {}
export const fieldToCheckbox = ({
field,
form: { isSubmitting },
disabled,
...props
}: CheckboxProps): MuiCheckboxProps => {
return {
disabled: disabled ?? isSubmitting,
...props,
...field,
// TODO handle indeterminate
checked: field.value,
value: field.value ? 'checked' : '',
};
};
export const Checkbox: React.ComponentType<CheckboxProps> = (
props: CheckboxProps
) => <MuiCheckbox {...fieldToCheckbox(props)} />;
Checkbox.displayName = 'FormikMaterialUICheckbox';