|
1 | 1 | import React from 'react'; |
2 | | -import { FieldRenderProps } from 'react-final-form'; |
| 2 | +import { useField } from 'formik'; |
3 | 3 | import MuiTextField, { TextFieldProps } from '@material-ui/core/TextField'; |
4 | 4 |
|
5 | | -type Props = Partial<FieldRenderProps<any, any>>; |
6 | | - |
7 | | -export const TextFieldComponent: React.FunctionComponent<Props & |
8 | | - TextFieldProps> = props => { |
9 | | - const { input, meta, ...rest } = props; |
10 | | - const textFieldProps = Boolean(input) ? input : rest; |
11 | | - let inputProps = rest.inputProps; |
12 | | - if (input) { |
13 | | - // eslint-disable-next-line @typescript-eslint/no-unused-vars |
14 | | - const { name, onChange, value, ...restInputProps } = input; |
15 | | - inputProps = restInputProps; |
16 | | - } |
17 | | - |
| 5 | +export const TextFieldComponent: React.FunctionComponent<TextFieldProps> = props => { |
| 6 | + const [field, meta] = useField(props.name); |
| 7 | + const textFieldProps = Boolean(field) ? field : props; |
18 | 8 | const hasError = Boolean(meta && meta.touched && meta.error); |
19 | 9 |
|
20 | 10 | return ( |
21 | 11 | <MuiTextField |
22 | | - {...rest} |
| 12 | + {...props} |
23 | 13 | name={textFieldProps.name} |
24 | 14 | onChange={textFieldProps.onChange} |
| 15 | + onBlur={textFieldProps.onBlur} |
25 | 16 | value={textFieldProps.value} |
26 | 17 | error={hasError} |
27 | 18 | helperText={hasError ? meta.error : ''} |
28 | | - inputProps={inputProps} |
29 | 19 | fullWidth={true} |
| 20 | + margin="normal" |
30 | 21 | /> |
31 | 22 | ); |
32 | 23 | }; |
0 commit comments