Skip to content

Commit b7ad07c

Browse files
authored
feat: added new input Radio Group (#292)
1 parent b0bee5a commit b7ad07c

File tree

57 files changed

+450
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+450
-11
lines changed

.storybook/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const config: StorybookConfig = {
1111
actions: false,
1212
backgrounds: false,
1313
controls: false,
14-
measure: false,
14+
measure: true,
1515
outline: false,
1616
toolbars: true,
1717
viewport: false,

docs/input-props-map.md

Lines changed: 11 additions & 10 deletions

docs/spec.md

Lines changed: 8 additions & 0 deletions

docs/styles.md

Lines changed: 9 additions & 0 deletions

src/lib/core/types/specs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ export interface StringSpec<
192192
filterPlaceholder?: string;
193193
meta?: Record<string, string>;
194194
};
195+
radioGroupParams?: {
196+
direction?: 'horizontal' | 'vertical';
197+
disabled?: Record<string, boolean>;
198+
};
195199
inputProps?: InputComponentProps;
196200
layoutProps?: LayoutComponentProps;
197201
generateRandomValueButton?: boolean;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@import '../../../styles/variables.scss';
2+
3+
.#{$ns}radio-group {
4+
align-items: center;
5+
height: var(--df-radio-group-height, 28px);
6+
7+
&_vertical {
8+
align-items: flex-start;
9+
margin-top: var(--df-radio-group-vertical-margin-top, var(--g-spacing-2));
10+
height: auto;
11+
}
12+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import React from 'react';
2+
3+
import {
4+
Flex,
5+
RadioGroup as RadioGroupBase,
6+
type RadioGroupProps as RadioGroupBaseProps,
7+
type RadioGroupOption,
8+
} from '@gravity-ui/uikit';
9+
10+
import type {StringInput} from '../../../../core';
11+
import {block} from '../../../utils';
12+
13+
import './RadioGroup.scss';
14+
15+
const b = block('radio-group');
16+
17+
export interface RadioGroupProps
18+
extends Omit<
19+
RadioGroupBaseProps,
20+
'direction' | 'onChange' | 'onBlur' | 'onFocus' | 'disabled' | 'qa' | 'content'
21+
> {
22+
withCustomOptions?: boolean;
23+
}
24+
25+
export const RadioGroup: StringInput<RadioGroupProps> = ({name, input, spec, inputProps}) => {
26+
const {value, onBlur, onChange, onFocus} = input;
27+
28+
const {withCustomOptions, options: externalOptions} = inputProps || {};
29+
30+
const options: RadioGroupOption[] = React.useMemo(
31+
() =>
32+
withCustomOptions
33+
? externalOptions || []
34+
: spec.enum?.map((id) => ({
35+
value: id,
36+
content: spec.description?.[id] || id,
37+
disabled: spec.viewSpec.radioGroupParams?.disabled?.[id] || false,
38+
})) || [],
39+
[
40+
withCustomOptions,
41+
externalOptions,
42+
spec.enum,
43+
spec.description,
44+
spec.viewSpec.radioGroupParams?.disabled,
45+
],
46+
);
47+
48+
if (options.length === 0) {
49+
return null;
50+
}
51+
52+
return (
53+
<Flex className={b({vertical: spec.viewSpec.radioGroupParams?.direction === 'vertical'})}>
54+
<RadioGroupBase
55+
{...inputProps}
56+
name={name}
57+
qa={name}
58+
disabled={spec.viewSpec.disabled}
59+
onUpdate={onChange}
60+
onBlur={onBlur}
61+
onFocus={onFocus}
62+
value={value}
63+
options={options}
64+
direction={spec.viewSpec.radioGroupParams?.direction || 'horizontal'}
65+
/>
66+
</Flex>
67+
);
68+
};

0 commit comments

Comments
 (0)