Skip to content

Commit c0faca1

Browse files
Update checkboxes for NHS.UK frontend v10.0.0
1 parent e49847b commit c0faca1

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

src/components/form-elements/checkboxes/Checkboxes.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const CheckboxesComponent = ({ children, idPrefix, ...rest }: CheckboxesProps) =
7373
return (
7474
<div
7575
className={classNames('nhsuk-checkboxes', className)}
76+
data-module="nhsuk-checkboxes"
7677
id={id}
7778
ref={moduleRef}
7879
{...restRenderProps}

src/components/form-elements/checkboxes/__tests__/__snapshots__/Checkboxes.test.tsx.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ exports[`Checkboxes Matches the snapshot with an exclusive checkbox 1`] = `
88
>
99
<div
1010
class="nhsuk-checkboxes"
11+
data-module="nhsuk-checkboxes"
1112
data-nhsuk-checkboxes-init=""
1213
id="example"
1314
>
@@ -107,6 +108,7 @@ exports[`Checkboxes matches snapshot 1`] = `
107108
>
108109
<div
109110
class="nhsuk-checkboxes"
111+
data-module="nhsuk-checkboxes"
110112
data-nhsuk-checkboxes-init=""
111113
id="example"
112114
>
@@ -182,6 +184,7 @@ exports[`Checkboxes matches snapshot with boolean error 1`] = `
182184
<div
183185
aria-describedby="example--error-message"
184186
class="nhsuk-checkboxes"
187+
data-module="nhsuk-checkboxes"
185188
data-nhsuk-checkboxes-init=""
186189
id="example"
187190
>
@@ -268,6 +271,7 @@ exports[`Checkboxes matches snapshot with string error 1`] = `
268271
<div
269272
aria-describedby="example--error-message"
270273
class="nhsuk-checkboxes"
274+
data-module="nhsuk-checkboxes"
271275
data-nhsuk-checkboxes-init=""
272276
id="example"
273277
>

src/components/form-elements/checkboxes/components/Item.tsx

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)