Skip to content

Commit 959b506

Browse files
authored
Merge pull request #1477 from buildo/1476_RadioGroup_doesnt_support_custom_values
1476 radio group doesnt support custom values
2 parents 684b3db + ee9bad1 commit 959b506

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

src/FormField/RadioGroupField.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import * as cx from 'classnames';
33
import RadioGroup from '../RadioGroup';
44
import { FormField } from './FormField';
55

6-
type DefaultProps = {
6+
type DefaultProps<T> = {
77
/** an optional custom renderer for RadioGroupField */
8-
radioGroupRenderer: (props: RadioGroup.Props) => JSX.Element;
8+
radioGroupRenderer: (props: RadioGroup.Props<T>) => JSX.Element;
99
};
1010

11-
type NonDefaultProps = {
11+
type NonDefaultProps<T> = {
1212
/** the label for the field */
1313
label: FormField.Props['label'];
1414
/** whether the field is required */
@@ -22,17 +22,17 @@ type NonDefaultProps = {
2222
/** an optional id passed to the input component */
2323
id?: string;
2424
/** the properties of the radio group */
25-
radioGroupProps: RadioGroup.Props;
25+
radioGroupProps: RadioGroup.Props<T>;
2626
};
2727

28-
type InternalProps = NonDefaultProps & DefaultProps;
28+
type InternalProps<T> = NonDefaultProps<T> & DefaultProps<T>;
2929

3030
export namespace RadioGroupField {
31-
export type Props = NonDefaultProps & Partial<DefaultProps>;
31+
export type Props<T> = NonDefaultProps<T> & Partial<DefaultProps<T>>;
3232
}
3333

34-
export class RadioGroupField extends React.PureComponent<InternalProps> {
35-
static defaultProps: DefaultProps = {
34+
export class RadioGroupField<T> extends React.PureComponent<InternalProps<T>> {
35+
static defaultProps: DefaultProps<unknown> = {
3636
radioGroupRenderer: props => (
3737
<RadioGroup {...props} style={{ marginTop: '16px', ...props.style }} />
3838
)

src/RadioGroup/Examples.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,26 @@ const onChange = value => setState({ value });
3434
const options = [
3535
{
3636
label: 'First',
37-
value: 'first'
37+
value: { position: 'first' }
3838
},
3939
{
4040
label: 'Second',
41-
value: 'second'
41+
value: { position: 'second' }
4242
},
4343
{
4444
label: 'Other',
45-
value: 'other'
45+
value: { position: 'other' }
4646
}
4747
];
4848

4949
<form className="ui form">
50-
<RadioGroup horizontal value={state.value} onChange={onChange} options={options} />
51-
<div style={{ marginTop: 20 }}>Selected: {state.value}</div>
50+
<RadioGroup
51+
horizontal
52+
value={state.value}
53+
onChange={onChange}
54+
getValueKey={value => value.position}
55+
options={options}
56+
/>
57+
<div style={{ marginTop: 20 }}>Selected: {state.value ? state.value.position : state.value}</div>
5258
</form>;
5359
```

src/RadioGroup/RadioGroup.tsx

+16-11
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ import * as React from 'react';
22
import * as cx from 'classnames';
33
import FlexView from 'react-flexview';
44

5-
export type RadioOption = {
5+
export type RadioOption<T> = {
66
label: string;
7-
value: string;
7+
value: T;
88
};
99

10-
export type RadioGroupRequiredProps = {
10+
export type RadioGroupRequiredProps<T> = {
1111
/** value */
12-
value?: string;
12+
value?: T;
1313
/** onChange */
14-
onChange: (value: string) => void;
14+
onChange: (value: T) => void;
15+
getValueKey?: (value: T) => string;
1516
/** text displayed on the right of the checkbox */
16-
options: Array<RadioOption>;
17+
options: Array<RadioOption<T>>;
1718
className?: string;
1819
id?: string;
1920
style?: React.CSSProperties;
@@ -27,16 +28,16 @@ export type RadioGroupDefaultProps = {
2728
};
2829

2930
export namespace RadioGroup {
30-
export type Props = RadioGroupRequiredProps & Partial<RadioGroupDefaultProps>;
31+
export type Props<T> = RadioGroupRequiredProps<T> & Partial<RadioGroupDefaultProps>;
3132
}
3233

33-
export class RadioGroup extends React.PureComponent<RadioGroup.Props> {
34+
export class RadioGroup<T> extends React.PureComponent<RadioGroup.Props<T>> {
3435
static defaultProps: RadioGroupDefaultProps = {
3536
disabled: false,
3637
horizontal: false
3738
};
3839

39-
onChange = (option: RadioOption): React.EventHandler<any> => {
40+
onChange = (option: RadioOption<T>): React.EventHandler<any> => {
4041
return e => {
4142
e.stopPropagation();
4243
if (!this.props.disabled) {
@@ -47,6 +48,9 @@ export class RadioGroup extends React.PureComponent<RadioGroup.Props> {
4748

4849
render() {
4950
const { id, className, style, disabled, options, value, horizontal } = this.props;
51+
const defaultedGetValueKey = this.props.getValueKey
52+
? this.props.getValueKey
53+
: (value: T) => value;
5054
return (
5155
<FlexView
5256
shrink={false}
@@ -65,10 +69,11 @@ export class RadioGroup extends React.PureComponent<RadioGroup.Props> {
6569
>
6670
{options.map(option => (
6771
<FlexView
68-
key={option.value}
72+
key={option.label}
6973
vAlignContent="center"
7074
className={cx('radio-group-option', {
71-
'is-checked': option.value === value
75+
'is-checked':
76+
value && defaultedGetValueKey(option.value) === defaultedGetValueKey(value)
7277
})}
7378
>
7479
<svg viewBox="0 0 16 16" onClick={this.onChange(option)}>

0 commit comments

Comments
 (0)