Skip to content

Commit 6db649b

Browse files
authored
fix(radiogroup): fix type (#288)
1 parent 69f841e commit 6db649b

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/components/radio/radio-group.tsx

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
1-
import React, { FC, useCallback } from 'react';
1+
import React, { useCallback } from 'react';
22
import { Flex } from 'rebass';
33

44
// Components
55
import Radio, { RadioProps } from './index';
66
// Utils
77
import randomString from '../../utils/randomString';
88

9-
type Value = string | number | boolean;
10-
export type RadioGroupOption = {
11-
key: Value;
9+
export type RadioGroupOption<T = string> = {
10+
key: T;
1211
text: string;
1312
additionalText?: string;
1413
};
15-
export interface RadioGroupProps
14+
export interface RadioGroupProps<T = string>
1615
extends Omit<RadioProps, 'label' | 'onChange' | 'options' | 'value'> {
17-
value: Value | null;
18-
options: string[] | RadioGroupOption[];
19-
onChange: (value: Value) => void;
16+
value: T;
17+
options: string[] | RadioGroupOption<T>[];
18+
onChange: (value: T) => void;
2019
tooltipMessages?: {
2120
[key: string]: string;
2221
};
2322
flexDirection?: 'row' | 'column' | null;
2423
}
2524

26-
const RadioGroup: FC<RadioGroupProps> = ({
25+
const RadioGroup: <T = string>(props: RadioGroupProps<T>) => JSX.Element = ({
2726
options,
2827
value,
2928
onChange,
3029
tooltipMessages = {},
3130
flexDirection = 'column',
3231
...props
33-
}: RadioGroupProps) => {
32+
}) => {
3433
const handleChange = useCallback(
35-
(newValue: Value) => () => {
34+
(newValue: typeof value) => () => {
3635
onChange(newValue);
3736
},
3837
[onChange],
@@ -41,7 +40,13 @@ const RadioGroup: FC<RadioGroupProps> = ({
4140
const name = randomString();
4241

4342
const mappedOptions = options.map((val) =>
44-
typeof val === 'string' ? { key: val, text: val } : val,
43+
typeof val === 'string'
44+
? {
45+
key: val as unknown as typeof value,
46+
text: val,
47+
additionalText: '',
48+
}
49+
: val,
4550
);
4651

4752
return (

0 commit comments

Comments
 (0)