Skip to content

Commit e49847b

Browse files
Update radios for NHS.UK frontend v10.0.0
1 parent c6b3a3c commit e49847b

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

src/components/form-elements/radios/Radios.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1-
import React, { HTMLProps, useState } from 'react';
1+
import React, { HTMLProps, useEffect, useRef, useState } from 'react';
22
import classNames from 'classnames';
33
import { FormElementProps } from '@util/types/FormTypes';
44
import { RadiosContext, IRadiosContext } from './RadioContext';
55
import SingleInputFormGroup from '@components/utils/SingleInputFormGroup';
66
import RadiosDivider from './components/Divider';
77
import RadiosItem from './components/Item';
88
import { generateRandomName } from '@util/RandomID';
9+
import { Radios } from 'nhsuk-frontend';
910

1011
interface RadiosProps extends HTMLProps<HTMLDivElement>, FormElementProps {
1112
inline?: boolean;
1213
idPrefix?: string;
1314
}
1415

1516
const RadiosComponent = ({ children, idPrefix, ...rest }: RadiosProps) => {
17+
const moduleRef = useRef<HTMLDivElement>(null);
18+
const [instance, setInstance] = useState<Radios>();
19+
const [selectedRadio, setSelectedRadio] = useState<string>();
20+
1621
const _radioReferences: Array<string> = [];
1722
let _radioCount = 0;
1823
let _radioIds: Record<string, string> = {};
19-
const [selectedRadio, setSelectedRadio] = useState<string>();
24+
25+
useEffect(() => {
26+
if (!moduleRef.current || instance) {
27+
return;
28+
}
29+
30+
setInstance(new Radios(moduleRef.current));
31+
}, [moduleRef, instance]);
2032

2133
const getRadioId = (id: string, reference: string): string => {
2234
if (reference in _radioIds) {
@@ -69,7 +81,9 @@ const RadiosComponent = ({ children, idPrefix, ...rest }: RadiosProps) => {
6981
return (
7082
<div
7183
className={classNames('nhsuk-radios', { 'nhsuk-radios--inline': inline }, className)}
84+
data-module="nhsuk-radios"
7285
id={id}
86+
ref={moduleRef}
7387
{...restRenderProps}
7488
>
7589
<RadiosContext.Provider value={contextValue}>{children}</RadiosContext.Provider>

src/components/form-elements/radios/__tests__/Radios.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ describe('Radios', () => {
3535
</Radios>,
3636
);
3737

38-
expect(container.querySelector('.conditional-test')).toBeFalsy();
38+
expect(container.querySelector('.conditional-test')?.parentElement).toHaveClass(
39+
'nhsuk-radios__conditional--hidden',
40+
);
3941
});
4042

4143
it('renders the conditional content if the radio reference = selected radio', () => {

src/components/form-elements/radios/__tests__/__snapshots__/Radios.test.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ exports[`Radios matches snapshot 1`] = `
77
>
88
<div
99
class="nhsuk-radios"
10+
data-module="nhsuk-radios"
11+
data-nhsuk-radios-init=""
1012
id="example"
1113
>
1214
<div

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ const RadiosItem: FC<RadiosItemProps> = ({
5959
className={classNames('nhsuk-radios__input', className)}
6060
id={inputID}
6161
name={name}
62+
type={type}
63+
aria-controls={conditional ? `${inputID}--conditional` : undefined}
6264
aria-describedby={hint ? `${inputID}--hint` : undefined}
6365
checked={checked}
6466
defaultChecked={defaultChecked}
6567
ref={inputRef}
66-
type={type}
6768
{...rest}
6869
/>
6970
{children ? (
@@ -82,15 +83,17 @@ const RadiosItem: FC<RadiosItemProps> = ({
8283
</HintText>
8384
) : null}
8485
</div>
85-
{conditional && (shouldShowConditional || forceShowConditional) ? (
86+
{conditional && (
8687
<div
87-
className="nhsuk-radios__conditional"
88+
className={classNames('nhsuk-radios__conditional', {
89+
'nhsuk-radios__conditional--hidden': !(shouldShowConditional || forceShowConditional),
90+
})}
8891
id={`${inputID}--conditional`}
8992
{...conditionalWrapperProps}
9093
>
9194
{conditional}
9295
</div>
93-
) : null}
96+
)}
9497
</>
9598
);
9699
};

0 commit comments

Comments
 (0)