@@ -2,18 +2,19 @@ import * as React from 'react';
2
2
import * as cx from 'classnames' ;
3
3
import FlexView from 'react-flexview' ;
4
4
5
- export type RadioOption = {
5
+ export type RadioOption < T > = {
6
6
label : string ;
7
- value : string ;
7
+ value : T ;
8
8
} ;
9
9
10
- export type RadioGroupRequiredProps = {
10
+ export type RadioGroupRequiredProps < T > = {
11
11
/** value */
12
- value ?: string ;
12
+ value ?: T ;
13
13
/** onChange */
14
- onChange : ( value : string ) => void ;
14
+ onChange : ( value : T ) => void ;
15
+ getValueKey ?: ( value : T ) => string ;
15
16
/** text displayed on the right of the checkbox */
16
- options : Array < RadioOption > ;
17
+ options : Array < RadioOption < T > > ;
17
18
className ?: string ;
18
19
id ?: string ;
19
20
style ?: React . CSSProperties ;
@@ -27,16 +28,16 @@ export type RadioGroupDefaultProps = {
27
28
} ;
28
29
29
30
export namespace RadioGroup {
30
- export type Props = RadioGroupRequiredProps & Partial < RadioGroupDefaultProps > ;
31
+ export type Props < T > = RadioGroupRequiredProps < T > & Partial < RadioGroupDefaultProps > ;
31
32
}
32
33
33
- export class RadioGroup extends React . PureComponent < RadioGroup . Props > {
34
+ export class RadioGroup < T > extends React . PureComponent < RadioGroup . Props < T > > {
34
35
static defaultProps : RadioGroupDefaultProps = {
35
36
disabled : false ,
36
37
horizontal : false
37
38
} ;
38
39
39
- onChange = ( option : RadioOption ) : React . EventHandler < any > => {
40
+ onChange = ( option : RadioOption < T > ) : React . EventHandler < any > => {
40
41
return e => {
41
42
e . stopPropagation ( ) ;
42
43
if ( ! this . props . disabled ) {
@@ -47,6 +48,9 @@ export class RadioGroup extends React.PureComponent<RadioGroup.Props> {
47
48
48
49
render ( ) {
49
50
const { id, className, style, disabled, options, value, horizontal } = this . props ;
51
+ const defaultedGetValueKey = this . props . getValueKey
52
+ ? this . props . getValueKey
53
+ : ( value : T ) => value ;
50
54
return (
51
55
< FlexView
52
56
shrink = { false }
@@ -65,10 +69,11 @@ export class RadioGroup extends React.PureComponent<RadioGroup.Props> {
65
69
>
66
70
{ options . map ( option => (
67
71
< FlexView
68
- key = { option . value }
72
+ key = { option . label }
69
73
vAlignContent = "center"
70
74
className = { cx ( 'radio-group-option' , {
71
- 'is-checked' : option . value === value
75
+ 'is-checked' :
76
+ value && defaultedGetValueKey ( option . value ) === defaultedGetValueKey ( value )
72
77
} ) }
73
78
>
74
79
< svg viewBox = "0 0 16 16" onClick = { this . onChange ( option ) } >
0 commit comments