Skip to content

Commit

Permalink
Merge pull request #250 from sadik-malik/v7
Browse files Browse the repository at this point in the history
fix(RadioButtonGroup): Allow passing custom required error message and validations
  • Loading branch information
dohomi authored Apr 8, 2024
2 parents c21544c + a83fdf3 commit f369cbc
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/rhf-mui/src/RadioButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FieldValues,
PathValue,
useController,
UseControllerProps,
} from 'react-hook-form'
import {
FormControl,
Expand All @@ -27,6 +28,7 @@ export type RadioButtonGroupProps<
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
TValue = unknown
> = {
validation?: UseControllerProps<TFieldValues, TName>['rules']
options: TValue[]
helperText?: ReactNode
name: TName
Expand Down Expand Up @@ -87,19 +89,26 @@ const RadioButtonGroup = forwardRef(function RadioButtonGroup<
disabled,
formLabelProps,
transform,
validation = {},
...rest
} = props
const theme = useTheme()

const errorMsgFn = useFormError()
const customErrorFn = parseError || errorMsgFn

const rules = {
...validation,
...(required &&
!validation.required && {required: 'This field is required'}),
}

const {
field,
fieldState: {error},
} = useController({
name,
rules: required ? {required: 'This field is required'} : undefined,
rules,
disabled,
control,
})
Expand Down

0 comments on commit f369cbc

Please sign in to comment.