Skip to content

Commit d9d049a

Browse files
committed
v1.1.4-test.3
1 parent c0ba9fc commit d9d049a

File tree

7 files changed

+39
-18
lines changed

7 files changed

+39
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nhsuk-react-components",
3-
"version": "1.1.4-test.2",
3+
"version": "1.1.4-test.3",
44
"author": {
55
"email": "[email protected]",
66
"name": "Thomas Judd-Cooper",

src/components/checkboxes/CheckboxContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createContext } from 'react';
22

33
export interface ICheckboxContext {
44
name: string;
5-
getBoxId: () => string | undefined;
5+
getBoxId: (reference: string) => string | undefined;
66
setConditional: (boxReference: string, hasConditional: boolean) => void;
77
leaseReference: () => string;
88
unleaseReference: (reference: string) => void;

src/components/checkboxes/Checkboxes.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Checkboxes extends PureComponent<CheckboxesProps, CheckboxesState> {
1919

2020
private boxReferences: Array<string> = [];
2121

22+
private boxIds: Record<string, string> = {};
23+
2224
constructor(props: {}, ...rest: any[]) {
2325
super(props, ...rest);
2426
this.state = {
@@ -58,10 +60,19 @@ class Checkboxes extends PureComponent<CheckboxesProps, CheckboxesState> {
5860
});
5961
};
6062

61-
getBoxId = (id: string): string => {
63+
getBoxId = (id: string, reference: string): string => {
6264
const { idPrefix } = this.props;
65+
if (reference in this.boxIds) {
66+
return this.boxIds[reference];
67+
}
6368
this.boxCount += 1;
64-
return `${idPrefix || id}-${this.boxCount}`;
69+
this.boxIds[reference] = `${idPrefix || id}-${this.boxCount}`;
70+
return this.boxIds[reference];
71+
};
72+
73+
resetBoxIds = () => {
74+
this.boxCount = 0;
75+
this.boxIds = {};
6576
};
6677

6778
static Box = Box;
@@ -71,11 +82,11 @@ class Checkboxes extends PureComponent<CheckboxesProps, CheckboxesState> {
7182
return (
7283
<FormGroup<CheckboxesProps> inputType="checkboxes" {...rest}>
7384
{({ className, name, id, idPrefix, ...restRenderProps }) => {
74-
this.boxCount = 0;
85+
this.resetBoxIds();
7586
const containsConditional = this.state.conditionalBoxes.length > 0;
7687
const contextValue: ICheckboxContext = {
7788
name,
78-
getBoxId: () => this.getBoxId(id),
89+
getBoxId: reference => this.getBoxId(id, reference),
7990
setConditional: this.setConditional,
8091
leaseReference: this.leaseReference,
8192
unleaseReference: this.unleaseReference,

src/components/checkboxes/components/Box.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,15 @@ const Box: React.FC<BoxProps> = ({
3131
...rest
3232
}) => {
3333
const { getBoxId, name, setConditional, unleaseReference, leaseReference } = useContext<
34-
ICheckboxContext
34+
ICheckboxContext
3535
>(CheckboxContext);
3636
const [boxReference] = useState<string>(leaseReference());
3737
const [showConditional, setShowConditional] = useState<boolean>(!!(checked || defaultChecked));
38-
const inputID = id || getBoxId();
38+
const inputID = id || getBoxId(boxReference);
3939

4040
const { className: labelClassName, ...restLabelProps } = labelProps || {};
4141
const { className: hintClassName, ...restHintProps } = hintProps || {};
42-
const { className: conditionalClassName, ...restConditionalProps } =
43-
conditionalWrapperProps || {};
42+
const { className: conditionalClassName, ...restConditionalProps } = conditionalWrapperProps || {};
4443

4544
useEffect(() => () => unleaseReference(boxReference), []);
4645

src/components/radios/RadioContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createContext } from 'react';
33
export type IRadiosContext = {
44
name: string;
55
selectedRadio: string;
6-
getRadioId: () => string;
6+
getRadioId: (reference: string) => string;
77
setConditional: (radioRef: string, hasConditional: boolean) => void;
88
setSelected: (radioRef: string) => void;
99
leaseReference: () => string;

src/components/radios/Radios.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { HTMLProps, PureComponent } from 'react';
22
import classNames from 'classnames';
33
import { FormElementProps } from '../../util/types/FormTypes';
4-
import { RadiosContext } from './RadioContext';
4+
import { RadiosContext, IRadiosContext } from './RadioContext';
55
import FormGroup from '../../util/FormGroup';
66
import Divider from './components/Divider';
77
import Radio from './components/Radio';
@@ -22,6 +22,8 @@ class Radios extends PureComponent<RadiosProps, RadiosState> {
2222

2323
private radioReferences: Array<string> = [];
2424

25+
private radioIds: Record<string, string> = {};
26+
2527
static defaultProps = {
2628
role: 'radiogroup',
2729
};
@@ -34,10 +36,14 @@ class Radios extends PureComponent<RadiosProps, RadiosState> {
3436
};
3537
}
3638

37-
getRadioId = (id: string): string => {
39+
getRadioId = (id: string, reference: string): string => {
3840
const { idPrefix } = this.props;
41+
if (reference in this.radioIds) {
42+
return this.radioIds[reference];
43+
}
3944
this.radioCount += 1;
40-
return `${idPrefix || id}-${this.radioCount}`;
45+
this.radioIds[reference] = `${idPrefix || id}-${this.radioCount}`;
46+
return this.radioIds[reference];
4147
};
4248

4349
leaseReference = (): string => {
@@ -78,6 +84,11 @@ class Radios extends PureComponent<RadiosProps, RadiosState> {
7884
});
7985
};
8086

87+
resetRadioIds = () => {
88+
this.radioCount = 0;
89+
this.radioIds = {};
90+
};
91+
8192
static Divider = Divider;
8293

8394
static Radio = Radio;
@@ -87,9 +98,9 @@ class Radios extends PureComponent<RadiosProps, RadiosState> {
8798
return (
8899
<FormGroup<RadiosProps> inputType="radios" {...rest}>
89100
{({ className, inline, name, id, error, ...restRenderProps }) => {
90-
this.radioCount = 0;
91-
const contextValue = {
92-
getRadioId: () => this.getRadioId(id),
101+
this.resetRadioIds();
102+
const contextValue: IRadiosContext = {
103+
getRadioId: reference => this.getRadioId(id, reference),
93104
selectedRadio: this.state.selectedRadio,
94105
setConditional: this.setConditional,
95106
setSelected: this.setSelected,

src/components/radios/components/Radio.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const Radio: React.FC<RadioProps> = ({
3838
unleaseReference,
3939
} = useContext<IRadiosContext>(RadiosContext);
4040
const [radioReference] = useState<string>(leaseReference());
41-
const inputID = id || getRadioId();
41+
const inputID = id || getRadioId(radioReference);
4242

4343
useEffect(() => () => unleaseReference(radioReference));
4444

0 commit comments

Comments
 (0)