Skip to content

Commit 075e48b

Browse files
authored
feat(CheckboxGroup): added new input CheckboxGroup (#214)
1 parent c938dc5 commit 075e48b

File tree

44 files changed

+520
-26
lines changed

Some content is hidden

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

44 files changed

+520
-26
lines changed

docs/spec.md

Lines changed: 34 additions & 26 deletions
Large diffs are not rendered by default.

src/lib/core/types/specs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export interface ArraySpec<
4343
};
4444
inputProps?: InputComponentProps;
4545
layoutProps?: LayoutComponentProps;
46+
checkboxGroupParams?: {
47+
placement?: 'horizontal' | 'vertical';
48+
disabled?: Record<string, boolean>;
49+
};
4650
};
4751
}
4852

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@import '../../../styles/variables';
2+
3+
.#{$ns}checkbox-group {
4+
display: flex;
5+
align-items: center;
6+
height: 28px;
7+
8+
& > *:not(:last-child) {
9+
margin-right: 6px;
10+
}
11+
12+
&_vertical {
13+
flex-direction: column;
14+
align-items: flex-start;
15+
margin-top: 8px;
16+
height: auto;
17+
18+
& > *:not(:last-child) {
19+
margin-right: 0px;
20+
margin-bottom: 6px;
21+
}
22+
}
23+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React from 'react';
2+
3+
import {ArrayInput, FieldArrayValue, transformArrIn, transformArrOut} from '../../../../core';
4+
import {Checkbox, type CheckboxProps as CheckboxBaseProps} from '@gravity-ui/uikit';
5+
import {block} from '../../../utils';
6+
7+
import './CheckboxGroup.scss';
8+
9+
const b = block('checkbox-group');
10+
11+
export interface CheckboxGroupProps
12+
extends Omit<
13+
CheckboxBaseProps,
14+
'checked' | 'onChange' | 'onBlur' | 'onFocus' | 'disabled' | 'qa' | 'content'
15+
> {}
16+
17+
export const CheckboxGroup: ArrayInput<CheckboxGroupProps> = ({name, input, spec, inputProps}) => {
18+
const {value, onBlur, onChange, onFocus} = input;
19+
20+
const _value: string[] | undefined = React.useMemo(
21+
() => transformArrOut<FieldArrayValue, string[]>(value),
22+
[value],
23+
);
24+
25+
const options = React.useMemo(
26+
() =>
27+
spec.enum?.map((id) => ({
28+
value: id,
29+
text: spec.description?.[id] || id,
30+
})),
31+
[spec.enum, spec.description],
32+
);
33+
34+
const handleUpdate = React.useCallback(
35+
(optionValue: string, selected: boolean) => {
36+
let newValue = _value || [];
37+
38+
if (selected) {
39+
newValue.push(optionValue);
40+
} else {
41+
newValue = newValue.filter((id) => id !== optionValue);
42+
}
43+
44+
onChange(transformArrIn<string[], FieldArrayValue>(newValue));
45+
},
46+
[_value, onChange],
47+
);
48+
49+
return (
50+
<div
51+
className={b({vertical: spec.viewSpec.checkboxGroupParams?.placement === 'vertical'})}
52+
data-qa={name}
53+
>
54+
{options?.map(({value: optionValue, text}) => (
55+
<Checkbox
56+
{...inputProps}
57+
qa={name && `${name}-${optionValue}`}
58+
key={optionValue}
59+
checked={_value?.includes(optionValue)}
60+
onUpdate={(selected: boolean) => handleUpdate(optionValue, selected)}
61+
disabled={
62+
spec.viewSpec.disabled ||
63+
spec.viewSpec.checkboxGroupParams?.disabled?.[optionValue]
64+
}
65+
content={text}
66+
onBlur={onBlur}
67+
onFocus={onFocus}
68+
/>
69+
))}
70+
</div>
71+
);
72+
};
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)