@@ -35,7 +35,6 @@ const CheckboxesItem: FC<CheckboxesItemProps> = ({
3535 conditional,
3636 defaultChecked,
3737 checked,
38- onChange,
3938 inputRef,
4039 forceShowConditional,
4140 conditionalWrapperProps,
@@ -47,8 +46,8 @@ const CheckboxesItem: FC<CheckboxesItemProps> = ({
4746 useContext < ICheckboxContext > ( CheckboxContext ) ;
4847
4948 const [ boxReference ] = useState < string > ( leaseReference ( ) ) ;
50- const [ showConditional , setShowConditional ] = useState < boolean > ( ! ! ( checked || defaultChecked ) ) ;
5149 const inputID = id || getBoxId ( boxReference ) ;
50+ const shouldShowConditional = ! ! ( checked || defaultChecked ) ;
5251
5352 const { className : labelClassName , ...restLabelProps } = labelProps || { } ;
5453 const { className : hintClassName , ...restHintProps } = hintProps || { } ;
@@ -57,12 +56,6 @@ const CheckboxesItem: FC<CheckboxesItemProps> = ({
5756
5857 useEffect ( ( ) => ( ) => unleaseReference ( boxReference ) , [ ] ) ;
5958
60- useEffect ( ( ) => {
61- if ( checked !== undefined ) {
62- setShowConditional ( checked ) ;
63- }
64- } , [ checked ] ) ;
65-
6659 const inputProps : HTMLAttributesWithData < HTMLInputElement > = rest ;
6760
6861 if ( exclusive ) {
@@ -74,17 +67,14 @@ const CheckboxesItem: FC<CheckboxesItemProps> = ({
7467 < div className = "nhsuk-checkboxes__item" >
7568 < input
7669 className = "nhsuk-checkboxes__input"
77- onChange = { ( e ) => {
78- if ( checked === undefined ) setShowConditional ( e . target . checked ) ;
79- if ( onChange ) onChange ( e ) ;
80- } }
81- name = { name }
8270 id = { inputID }
83- checked = { checked }
84- defaultChecked = { defaultChecked }
85- ref = { inputRef }
71+ name = { name }
8672 type = { type }
73+ aria-controls = { conditional ? `${ inputID } --conditional` : undefined }
74+ aria-describedby = { hint ? `${ inputID } --hint` : undefined }
8775 data-checkbox-exclusive-group = { name }
76+ defaultChecked = { checked || defaultChecked }
77+ ref = { inputRef }
8878 { ...inputProps }
8979 />
9080 { children ? (
@@ -100,21 +90,30 @@ const CheckboxesItem: FC<CheckboxesItemProps> = ({
10090 { hint ? (
10191 < HintText
10292 className = { classNames ( 'nhsuk-checkboxes__hint' , hintClassName ) }
93+ id = { `${ inputID } --hint` }
10394 { ...restHintProps }
10495 >
10596 { hint }
10697 </ HintText >
10798 ) : null }
10899 </ div >
109- { conditional && ( showConditional || forceShowConditional ) ? (
100+ { conditional && (
110101 < div
111- className = { classNames ( 'nhsuk-radios__conditional' , conditionalClassName ) }
102+ className = { classNames (
103+ 'nhsuk-checkboxes__conditional' ,
104+ {
105+ 'nhsuk-checkboxes__conditional--hidden' : ! (
106+ shouldShowConditional || forceShowConditional
107+ ) ,
108+ } ,
109+ conditionalClassName ,
110+ ) }
112111 id = { `${ inputID } --conditional` }
113112 { ...restConditionalProps }
114113 >
115114 { conditional }
116115 </ div >
117- ) : null }
116+ ) }
118117 </ >
119118 ) ;
120119} ;
0 commit comments