Skip to content

Commit 9fe109c

Browse files
authored
feat: support multiple value for setting option type (#1005)
1 parent 66159ce commit 9fe109c

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

src/components/SettingSection/SettingParamInput.tsx

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import {
1010
RcTypography,
1111
RcIcon,
1212
RcTooltip,
13+
RcFormLabel,
14+
RcFormGroup,
15+
RcCheckbox,
1316
css,
1417
} from '@ringcentral/juno';
15-
import { styled } from '@ringcentral/juno/foundation';
18+
import { styled, palette2 } from '@ringcentral/juno/foundation';
1619
import { InfoBorder, Lock } from '@ringcentral/juno-icon';
1720

1821
const StyledAlert = styled(RcAlert)`
@@ -72,6 +75,87 @@ const StyledSwitchLabel = styled(RcTypography)`
7275
font-size: 0.875rem;
7376
`;
7477

78+
const CheckboxSelectWrapper = styled.div`
79+
display: flex;
80+
flex-direction: column;
81+
`;
82+
83+
const StyledFormLabel = styled(RcFormLabel)`
84+
font-size: 0.75rem;
85+
color: ${palette2('neutral', 'f05')};
86+
`;
87+
88+
const StyledFormGroup = styled(RcFormGroup)`
89+
display: flex;
90+
flex-direction: row;
91+
align-items: center;
92+
flex-wrap: wrap;
93+
`;
94+
95+
function CheckboxSelect({
96+
value,
97+
label,
98+
options,
99+
onChange,
100+
required,
101+
className,
102+
readOnly,
103+
multiple,
104+
disabled,
105+
helperText,
106+
}) {
107+
let currentValue = value;
108+
if (multiple && !Array.isArray(value)) {
109+
currentValue = [];
110+
}
111+
return (
112+
<CheckboxSelectWrapper className={className}>
113+
<StyledFormLabel required={required}>{label}</StyledFormLabel>
114+
{helperText && (
115+
<RcTypography variant='caption1' color="neutral.f04" component="span">
116+
{helperText}
117+
</RcTypography>
118+
)}
119+
<StyledFormGroup>
120+
{options.map((option) => {
121+
const checked = multiple ? currentValue.includes(option.id) : currentValue === option.id;
122+
const itemDisabled = disabled || option.disabled;
123+
return (
124+
<RcCheckbox
125+
formControlLabelProps={{
126+
labelPlacement: 'end',
127+
}}
128+
label={option.name}
129+
key={option.id}
130+
id={option.id}
131+
name={option.id}
132+
checked={checked}
133+
disabled={itemDisabled || readOnly}
134+
onChange={(_, checked) => {
135+
if (readOnly) {
136+
return;
137+
}
138+
if (multiple) {
139+
if (checked) {
140+
onChange([...currentValue, option.id]);
141+
} else {
142+
onChange(currentValue.filter((v) => v !== option.id));
143+
}
144+
return;
145+
}
146+
if (checked) {
147+
onChange(option.id);
148+
} else {
149+
onChange(null);
150+
}
151+
}}
152+
/>
153+
)
154+
})}
155+
</StyledFormGroup>
156+
</CheckboxSelectWrapper>
157+
);
158+
}
75159
export function SettingParamInput({
76160
setting,
77161
className,
@@ -177,13 +261,30 @@ export function SettingParamInput({
177261
);
178262
}
179263
if (setting.type === 'option') {
264+
if (setting.checkbox) {
265+
return (
266+
<CheckboxSelect
267+
label={label}
268+
value={setting.value}
269+
options={setting.options}
270+
onChange={onChange}
271+
required={setting.required}
272+
className={className}
273+
readOnly={setting.readOnly}
274+
multiple={setting.multiple}
275+
disabled={setting.disabled}
276+
helperText={setting.helper}
277+
/>
278+
);
279+
}
180280
return (
181281
<StyledSelect
182282
label={label}
183283
value={setting.value}
184284
fullWidth
185285
className={className}
186286
placeholder={setting.placeholder}
287+
multiple={setting.multiple}
187288
onChange={(e) => {
188289
if (setting.readOnly) {
189290
return;

src/components/SettingSection/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ function allRequiredFilled(items) {
4141
item.required && (
4242
item.value === '' ||
4343
item.value === null ||
44-
item.value === undefined
44+
item.value === undefined ||
45+
(item.multiple && item.value.length === 0)
4546
)
4647
) {
4748
allFilled = false;

src/modules/ThirdPartyService/helper.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ function checkSettingSectionItem(item) {
2323
if (!item.options || !Array.isArray(item.options)) {
2424
return false;
2525
}
26-
if (item.value && !item.options.find((option) => option.id === item.value)) {
26+
if (item.multiple) {
27+
if (!Array.isArray(item.value)) {
28+
return false;
29+
}
30+
if (item.value.some((valueItem) => {
31+
return !item.options.find((option) => option.id === valueItem);
32+
})) {
33+
return false;
34+
}
35+
}
36+
if (!item.multiple && item.value && !item.options.find((option) => option.id === item.value)) {
2737
return false;
2838
}
2939
for (const option of item.options) {

0 commit comments

Comments
 (0)