Skip to content

Commit 60495b3

Browse files
authored
feat: add support for differing key and value (#233)
* feat: add support for differing key and value * docs: update storybook * fix: additional text BREAKING: keep additionalText in options array
1 parent a5482f4 commit 60495b3

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

src/components/radio/radio-group.tsx

+17-10
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import Radio, { RadioProps } from './index';
66
// Utils
77
import randomString from '../../utils/randomString';
88

9+
export type RadioGroupOption = {
10+
key: string;
11+
text: string;
12+
additionalText?: string;
13+
};
914
export interface RadioGroupProps
1015
extends Omit<RadioProps, 'label' | 'onChange' | 'options' | 'value'> {
1116
value: string | null;
12-
options: string[];
13-
additionalTexts?: string[];
17+
options: string[] | RadioGroupOption[];
1418
onChange: (value: string) => void;
1519
tooltipMessages?: {
1620
[key: string]: string;
@@ -22,7 +26,6 @@ const RadioGroup: FC<RadioGroupProps> = ({
2226
options,
2327
value,
2428
onChange,
25-
additionalTexts,
2629
tooltipMessages = {},
2730
flexDirection = 'column',
2831
...props
@@ -36,20 +39,24 @@ const RadioGroup: FC<RadioGroupProps> = ({
3639

3740
const name = randomString();
3841

42+
const mappedOptions = options.map((val) =>
43+
typeof val === 'string' ? { key: val, text: val } : val,
44+
);
45+
3946
return (
4047
<Flex flexDirection={flexDirection}>
41-
{options?.map((option, idx) => (
48+
{mappedOptions?.map(({ key, text, additionalText }) => (
4249
<Radio
4350
{...props}
44-
key={option}
51+
key={key}
4552
mt={flexDirection === 'column' ? 2 : 'initial'}
4653
mr={flexDirection === 'row' ? '20px' : 'initial'}
47-
checked={value === option}
48-
label={option}
54+
checked={value === key}
55+
label={text}
4956
name={name}
50-
addtionalText={additionalTexts?.[idx]}
51-
onChange={handleChange(option)}
52-
tooltip={tooltipMessages[option]}
57+
addtionalText={additionalText}
58+
onChange={handleChange(key)}
59+
tooltip={tooltipMessages[key]}
5360
/>
5461
))}
5562
</Flex>

src/components/radio/radio.stories.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ const GroupTemplate: Story<RadioGroupProps> = (props) => {
1818
value={value}
1919
onChange={setValue}
2020
mr="auto"
21-
options={['nullable', 'required', 'email', 'url']}
22-
additionalTexts={['text1', 'text2', 'text3', 'text4']}
21+
options={[
22+
{ key: 'nullable', text: 'nullable', additionalText: 'text1' },
23+
{ key: 'required', text: 'REQUIRED', additionalText: 'text2' },
24+
{ key: 'email', text: 'email' },
25+
{ key: 'url', text: 'url' },
26+
]}
2327
/>
2428
);
2529
};
@@ -56,7 +60,7 @@ Group.argTypes = {
5660
},
5761
options: {
5862
type: {
59-
summary: 'Array of strings',
63+
summary: 'Array of strings or Array of { key, text }',
6064
required: true,
6165
},
6266
},

src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ export type { ITheme, IThemeColors, IThemeIconSizes } from './theme/types';
263263
export type TooltipProps = import('./components/tooltip').TooltipProps;
264264
export type BadgeProps = import('./components/badges').BadgeProps;
265265
export type SliderProps = import('./components/slider').SliderProps;
266-
export type RangeSliderProps = import('./components/range-slider').RangeSliderProps;
266+
export type RangeSliderProps =
267+
import('./components/range-slider').RangeSliderProps;
267268
export type EditableSelectProps =
268269
import('./components/editableSelect').EditableSelectProps;
269270
export type Select2Props = import('./components/select2').SelectProps;
@@ -290,6 +291,11 @@ export type { CardHeaderProps } from './components/card-header';
290291
export type { FileSystemExplorerProps } from './components/file-system-explorer';
291292
export type { FileExplorData } from './components/file-system-explorer/types';
292293
export type { GetIconProps } from './components/icon/GetIcon';
294+
export type { RadioProps } from './components/radio';
295+
export type {
296+
RadioGroupProps,
297+
RadioGroupOption,
298+
} from './components/radio/radio-group';
293299

294300
// Rebass components
295301
export { Flex, Box } from 'rebass';

0 commit comments

Comments
 (0)