Skip to content

Commit d38b811

Browse files
Prevent duplicate initialisation
1 parent 043d559 commit d38b811

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

src/components/content-presentation/tabs/Tabs.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22
import classNames from 'classnames';
3-
import React, { FC, HTMLAttributes, useEffect } from 'react';
3+
import React, { FC, HTMLAttributes, useEffect, useRef, useState } from 'react';
44
import HeadingLevel, { HeadingLevelType } from '@components/utils/HeadingLevel';
55
// @ts-expect-error -- No types available
66
import TabsJs from 'nhsuk-frontend/packages/components/tabs/tabs';
@@ -55,12 +55,23 @@ interface Tabs extends FC<TabsProps> {
5555
}
5656

5757
const Tabs: Tabs = ({ className, children, ...rest }) => {
58+
const moduleRef = useRef<HTMLDivElement>(null);
59+
const [isInitialised, setInitialised] = useState<boolean>(false);
60+
5861
useEffect(() => {
59-
TabsJs();
60-
}, []);
62+
if (!isInitialised && moduleRef.current?.parentElement) {
63+
TabsJs({ scope: moduleRef.current.parentElement });
64+
setInitialised(true);
65+
}
66+
}, [isInitialised, moduleRef]);
6167

6268
return (
63-
<div className={classNames('nhsuk-tabs', className)} data-module="nhsuk-tabs" {...rest}>
69+
<div
70+
className={classNames('nhsuk-tabs', className)}
71+
data-module="nhsuk-tabs"
72+
ref={moduleRef}
73+
{...rest}
74+
>
6475
{children}
6576
</div>
6677
);

src/components/form-elements/character-count/CharacterCount.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use client';
2-
import React, { FC, useEffect } from 'react';
2+
import React, { FC, useEffect, useRef, useState } from 'react';
33
// @ts-expect-error -- No types available
44
import CharacterCountJs from 'nhsuk-frontend/packages/components/character-count/character-count';
55
import { HTMLAttributesWithData } from '@util/types/NHSUKTypes';
@@ -25,9 +25,15 @@ const CharacterCount: FC<CharacterCountProps> = ({
2525
thresholdPercent,
2626
...rest
2727
}) => {
28+
const moduleRef = useRef<HTMLDivElement>(null);
29+
const [isInitialised, setInitialised] = useState<boolean>(false);
30+
2831
useEffect(() => {
29-
CharacterCountJs();
30-
}, []);
32+
if (!isInitialised && moduleRef.current?.parentElement) {
33+
CharacterCountJs({ scope: moduleRef.current.parentElement });
34+
setInitialised(true);
35+
}
36+
}, [isInitialised, moduleRef]);
3137

3238
const characterCountProps: HTMLAttributesWithData<HTMLDivElement> =
3339
countType === CharacterCountType.Characters
@@ -42,6 +48,7 @@ const CharacterCount: FC<CharacterCountProps> = ({
4248
<div
4349
className="nhsuk-character-count"
4450
data-module="nhsuk-character-count"
51+
ref={moduleRef}
4552
{...characterCountProps}
4653
>
4754
<div className="nhsuk-form-group">{children}</div>

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import React, { HTMLProps, useEffect } from 'react';
3+
import React, { HTMLProps, useEffect, useRef, useState } from 'react';
44
import classNames from 'classnames';
55
import { FormElementProps } from '@util/types/FormTypes';
66
import SingleInputFormGroup from '@components/utils/SingleInputFormGroup';
@@ -16,13 +16,19 @@ interface CheckboxesProps extends HTMLProps<HTMLDivElement>, FormElementProps {
1616
}
1717

1818
const Checkboxes = ({ children, idPrefix, ...rest }: CheckboxesProps) => {
19+
const moduleRef = useRef<HTMLDivElement>(null);
20+
const [isInitialised, setInitialised] = useState<boolean>(false);
21+
1922
const _boxReferences: string[] = [];
2023
let _boxCount: number = 0;
2124
let _boxIds: Record<string, string> = {};
2225

2326
useEffect(() => {
24-
CheckboxJs();
25-
}, []);
27+
if (!isInitialised && moduleRef.current?.parentElement) {
28+
CheckboxesJs({ scope: moduleRef.current.parentElement });
29+
setInitialised(true);
30+
}
31+
}, [isInitialised, moduleRef]);
2632

2733
const getBoxId = (id: string, reference: string): string => {
2834
if (reference in _boxIds) {
@@ -65,7 +71,12 @@ const Checkboxes = ({ children, idPrefix, ...rest }: CheckboxesProps) => {
6571
unleaseReference,
6672
};
6773
return (
68-
<div className={classNames('nhsuk-checkboxes', className)} id={id} {...restRenderProps}>
74+
<div
75+
className={classNames('nhsuk-checkboxes', className)}
76+
id={id}
77+
ref={moduleRef}
78+
{...restRenderProps}
79+
>
6980
<CheckboxContext.Provider value={contextValue}>{children}</CheckboxContext.Provider>
7081
</div>
7182
);

src/components/navigation/header/Header.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use client';
2-
import React, { FC, HTMLProps, useContext, useState, useEffect, useMemo } from 'react';
2+
import React, { FC, HTMLProps, useContext, useState, useEffect, useMemo, useRef } from 'react';
33
import classNames from 'classnames';
44
import NHSLogo, { NHSLogoNavProps } from './components/NHSLogo';
55
import OrganisationalLogo, { OrganisationalLogoProps } from './components/OrganisationalLogo';
@@ -47,14 +47,20 @@ const Header = ({
4747
white,
4848
...rest
4949
}: HeaderProps) => {
50+
const moduleRef = useRef<HTMLElement>(null);
51+
5052
const [hasMenuToggle, setHasMenuToggle] = useState(false);
5153
const [hasSearch, setHasSearch] = useState(false);
5254
const [hasServiceName, setHasServiceName] = useState(false);
55+
const [isInitialised, setInitialised] = useState<boolean>(false);
5356
const [menuOpen, setMenuOpen] = useState(false);
5457

5558
useEffect(() => {
56-
HeaderJs();
57-
}, []);
59+
if (!isInitialised && moduleRef.current?.parentElement) {
60+
HeaderJs({ scope: moduleRef.current.parentElement });
61+
setInitialised(true);
62+
}
63+
}, [isInitialised, moduleRef]);
5864

5965
const setMenuToggle = (toggle: boolean): void => {
6066
setHasMenuToggle(toggle);
@@ -114,6 +120,7 @@ const Header = ({
114120
className,
115121
)}
116122
role={role}
123+
ref={moduleRef}
117124
{...rest}
118125
>
119126
<HeaderContext.Provider value={contextValue}>{children}</HeaderContext.Provider>

0 commit comments

Comments
 (0)