@@ -11,15 +11,25 @@ export function RadioSelection({
11
11
readOnlyStyle,
12
12
groupName
13
13
} : 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
+
15
20
const currentId = selector . currentId ;
16
21
const isReadOnly = selector . readOnly ;
17
22
const name = groupName ?. value ?? inputId ;
18
23
19
24
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 ) ;
23
33
}
24
34
} ;
25
35
@@ -29,23 +39,24 @@ export function RadioSelection({
29
39
"widget-checkbox-radio-selection-readonly" : isReadOnly ,
30
40
[ `widget-checkbox-radio-selection-readonly-${ readOnlyStyle } ` ] : isReadOnly
31
41
} ) }
32
- role = " radiogroup"
42
+ role = { asSingleCheckbox ? "group" : " radiogroup"}
33
43
aria-labelledby = { `${ inputId } -label` }
34
44
aria-required = { ariaRequired ?. value }
35
45
>
36
46
{ options . map ( ( optionId , index ) => {
37
47
const isSelected = currentId === optionId ;
38
- const radioId = `${ inputId } -radio-${ index } ` ;
48
+ const controlId = `${ inputId } -${ selector . controlType } -${ index } ` ;
49
+
39
50
return (
40
51
< div
41
52
key = { optionId }
42
- className = { classNames ( "widget-checkbox-radio-selection-item" , "radio -item" , {
53
+ className = { classNames ( "widget-checkbox-radio-selection-item" , ` ${ selector . controlType } -item` , {
43
54
"widget-checkbox-radio-selection-item-selected" : isSelected
44
55
} ) }
45
56
>
46
57
< input
47
58
type = { selector . controlType }
48
- id = { radioId }
59
+ id = { controlId }
49
60
name = { name }
50
61
value = { optionId }
51
62
checked = { isSelected }
@@ -59,10 +70,10 @@ export function RadioSelection({
59
70
e . stopPropagation ( ) ;
60
71
e . nativeEvent . stopImmediatePropagation ( ) ;
61
72
if ( ! isReadOnly ) {
62
- selector . setValue ( optionId ) ;
73
+ selector . setValue ( asSingleCheckbox ? ( isSelected ? "false" : "true" ) : optionId ) ;
63
74
}
64
75
} }
65
- htmlFor = { radioId }
76
+ htmlFor = { controlId }
66
77
>
67
78
{ selector . caption . render ( optionId ) }
68
79
</ CaptionContent >
0 commit comments