1
- import React , { FC , useCallback } from 'react' ;
1
+ import React , { useCallback } from 'react' ;
2
2
import { Flex } from 'rebass' ;
3
3
4
4
// Components
5
5
import Radio , { RadioProps } from './index' ;
6
6
// Utils
7
7
import randomString from '../../utils/randomString' ;
8
8
9
- type Value = string | number | boolean ;
10
- export type RadioGroupOption = {
11
- key : Value ;
9
+ export type RadioGroupOption < T = string > = {
10
+ key : T ;
12
11
text : string ;
13
12
additionalText ?: string ;
14
13
} ;
15
- export interface RadioGroupProps
14
+ export interface RadioGroupProps < T = string >
16
15
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 ;
20
19
tooltipMessages ?: {
21
20
[ key : string ] : string ;
22
21
} ;
23
22
flexDirection ?: 'row' | 'column' | null ;
24
23
}
25
24
26
- const RadioGroup : FC < RadioGroupProps > = ( {
25
+ const RadioGroup : < T = string > ( props : RadioGroupProps < T > ) => JSX . Element = ( {
27
26
options,
28
27
value,
29
28
onChange,
30
29
tooltipMessages = { } ,
31
30
flexDirection = 'column' ,
32
31
...props
33
- } : RadioGroupProps ) => {
32
+ } ) => {
34
33
const handleChange = useCallback (
35
- ( newValue : Value ) => ( ) => {
34
+ ( newValue : typeof value ) => ( ) => {
36
35
onChange ( newValue ) ;
37
36
} ,
38
37
[ onChange ] ,
@@ -41,7 +40,13 @@ const RadioGroup: FC<RadioGroupProps> = ({
41
40
const name = randomString ( ) ;
42
41
43
42
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 ,
45
50
) ;
46
51
47
52
return (
0 commit comments