Skip to content

Commit

Permalink
propagate onChange for TextFieldElement closes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
dohomi committed Aug 18, 2022
1 parent 4c57d75 commit b585135
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/TextFieldElement.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { TextField, TextFieldProps } from '@mui/material'
import { Control, Controller, ControllerProps, FieldError, Path } from 'react-hook-form'
import { FieldValues } from 'react-hook-form/dist/types/fields'
import {TextField, TextFieldProps} from '@mui/material'
import {Control, Controller, ControllerProps, FieldError, Path} from 'react-hook-form'
import {FieldValues} from 'react-hook-form/dist/types/fields'

export type TextFieldElementProps<T> = Omit<TextFieldProps,
'name'> & {
validation?: ControllerProps['rules']
name: Path<T>
parseError?: (error: FieldError) => string
control?: Control<T>
'name'> & {
validation?: ControllerProps['rules']
name: Path<T>
parseError?: (error: FieldError) => string
control?: Control<T>
}

export default function TextFieldElement<TFieldValues extends FieldValues>({
Expand Down Expand Up @@ -37,12 +37,17 @@ export default function TextFieldElement<TFieldValues extends FieldValues>({
name={name}
control={control}
rules={validation}
render={({ field: { value, onChange, onBlur }, fieldState: { invalid, error } }) =>
render={({field: {value, onChange, onBlur}, fieldState: {invalid, error}}) =>
<TextField
{...rest}
name={name}
value={value ?? ''}
onChange={onChange}
onChange={(ev) => {
onChange(ev)
if (typeof rest.onChange === 'function') {
rest.onChange(ev)
}
}}
onBlur={onBlur}
required={required}
type={type}
Expand Down

0 comments on commit b585135

Please sign in to comment.