Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 73e0e0c

Browse files
committed
feat(formik): add generics to checkbox and radio button groups
The core components for RadioButtonGroup supports a generic to narrow the values (e.g. using an enum). All Formik components should also support this, but it was missing from the FormikRadioButtonGroup. In addition the FormikCheckboxGroupField also supports the same generic constructions as the base FormikCheckboxGroup.
1 parent 96324a1 commit 73e0e0c

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

packages/formik/src/FormikCheckboxGroup.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// Libraries
22
import React, { useMemo } from 'react'
33
import { FieldConfig, useField } from 'formik'
4-
import { Checkbox, spacing, withField } from 'practical-react-components-core'
4+
import {
5+
Checkbox,
6+
FieldProps,
7+
spacing,
8+
withField,
9+
} from 'practical-react-components-core'
510
import styled, { css } from 'styled-components'
611

712
export interface CheckboxGroupOption<V extends string | number> {
@@ -88,4 +93,6 @@ export function FormikCheckboxGroup<V extends string | number>({
8893
)
8994
}
9095

91-
export const FormikCheckboxGroupField = withField(FormikCheckboxGroup)
96+
export const FormikCheckboxGroupField = <V extends string | number>(
97+
props: FieldProps & FormikCheckboxGroupProps<V>
98+
) => withField<FormikCheckboxGroupProps<V>>(FormikCheckboxGroup)(props)
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import React from 'react'
22
import { useField, FieldConfig } from 'formik'
33
import {
4+
FieldProps,
45
RadioButtonGroup,
56
RadioButtonGroupProps,
67
withField,
78
} from 'practical-react-components-core'
89

9-
export interface FormikRadioButtonGroupProps
10-
extends Omit<RadioButtonGroupProps, 'name' | 'value'>,
11-
Partial<Pick<RadioButtonGroupProps, 'value'>>,
10+
export interface FormikRadioButtonGroupProps<V extends string = string>
11+
extends Omit<RadioButtonGroupProps<V>, 'name' | 'value'>,
12+
Partial<Pick<RadioButtonGroupProps<V>, 'value'>>,
1213
Pick<FieldConfig, 'name' | 'validate'> {}
1314

14-
export const FormikRadioButtonGroup: React.FC<FormikRadioButtonGroupProps> = ({
15+
export function FormikRadioButtonGroup<V extends string = string>({
1516
name,
1617
validate,
1718
...props
18-
}) => {
19+
}: FormikRadioButtonGroupProps<V>) {
1920
const [field] = useField({ name, validate })
2021

2122
return <RadioButtonGroup {...field} {...props} />
2223
}
2324

24-
export const FormikRadioButtonGroupField = withField(FormikRadioButtonGroup)
25+
export const FormikRadioButtonGroupField = <V extends string = string>(
26+
props: FieldProps & FormikRadioButtonGroupProps<V>
27+
) => withField<FormikRadioButtonGroupProps<V>>(FormikRadioButtonGroup)(props)

0 commit comments

Comments
 (0)