Skip to content

Commit 99a163b

Browse files
committed
feat(checkbox-radio-selection-web): add checkbox for booleans
1 parent 13f0f05 commit 99a163b

File tree

1 file changed

+21
-10
lines changed
  • packages/pluggableWidgets/checkbox-radio-selection-web/src/components/RadioSelection

1 file changed

+21
-10
lines changed

packages/pluggableWidgets/checkbox-radio-selection-web/src/components/RadioSelection/RadioSelection.tsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,25 @@ export function RadioSelection({
1111
readOnlyStyle,
1212
groupName
1313
}: SelectionBaseProps<SingleSelector>): ReactElement {
14-
const options = selector.options.getAll();
14+
const asSingleCheckbox = selector.controlType === "checkbox";
15+
16+
const allOptions = selector.options.getAll();
17+
const checkboxOption = asSingleCheckbox ? (allOptions.includes("true") ? "true" : allOptions[0]) : undefined;
18+
const options: string[] = asSingleCheckbox ? (checkboxOption ? [checkboxOption] : []) : allOptions;
19+
1520
const currentId = selector.currentId;
1621
const isReadOnly = selector.readOnly;
1722
const name = groupName?.value ?? inputId;
1823

1924
const handleChange = (e: ChangeEvent<HTMLInputElement>): void => {
20-
const selectedItem = e.target.value;
21-
if (!isReadOnly) {
22-
selector.setValue(selectedItem);
25+
if (isReadOnly) {
26+
return;
27+
}
28+
29+
if (asSingleCheckbox) {
30+
selector.setValue(e.target.checked ? "true" : "false");
31+
} else {
32+
selector.setValue(e.target.value);
2333
}
2434
};
2535

@@ -29,23 +39,24 @@ export function RadioSelection({
2939
"widget-checkbox-radio-selection-readonly": isReadOnly,
3040
[`widget-checkbox-radio-selection-readonly-${readOnlyStyle}`]: isReadOnly
3141
})}
32-
role="radiogroup"
42+
role={asSingleCheckbox ? "group" : "radiogroup"}
3343
aria-labelledby={`${inputId}-label`}
3444
aria-required={ariaRequired?.value}
3545
>
3646
{options.map((optionId, index) => {
3747
const isSelected = currentId === optionId;
38-
const radioId = `${inputId}-radio-${index}`;
48+
const controlId = `${inputId}-${selector.controlType}-${index}`;
49+
3950
return (
4051
<div
4152
key={optionId}
42-
className={classNames("widget-checkbox-radio-selection-item", "radio-item", {
53+
className={classNames("widget-checkbox-radio-selection-item", `${selector.controlType}-item`, {
4354
"widget-checkbox-radio-selection-item-selected": isSelected
4455
})}
4556
>
4657
<input
4758
type={selector.controlType}
48-
id={radioId}
59+
id={controlId}
4960
name={name}
5061
value={optionId}
5162
checked={isSelected}
@@ -59,10 +70,10 @@ export function RadioSelection({
5970
e.stopPropagation();
6071
e.nativeEvent.stopImmediatePropagation();
6172
if (!isReadOnly) {
62-
selector.setValue(optionId);
73+
selector.setValue(asSingleCheckbox ? (isSelected ? "false" : "true") : optionId);
6374
}
6475
}}
65-
htmlFor={radioId}
76+
htmlFor={controlId}
6677
>
6778
{selector.caption.render(optionId)}
6879
</CaptionContent>

0 commit comments

Comments
 (0)