Skip to content

Commit 787b8f6

Browse files
Make sure headingLevel automatically assumes isPageHeading
1 parent 71e9f13 commit 787b8f6

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

src/components/form-elements/label/Label.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import classNames from 'classnames';
22
import { type ComponentPropsWithoutRef, type FC } from 'react';
33

4+
import { HeadingLevel, type HeadingLevelProps } from '#components/utils/HeadingLevel.js';
45
import { type NHSUKSize } from '#util/types/NHSUKTypes.js';
56

6-
export interface LabelProps extends ComponentPropsWithoutRef<'label'> {
7+
export interface LabelProps
8+
extends ComponentPropsWithoutRef<'label'>,
9+
Pick<HeadingLevelProps, 'headingLevel'> {
710
isPageHeading?: boolean;
811
size?: NHSUKSize;
912
}
@@ -16,16 +19,18 @@ const LabelComponent: FC<Omit<LabelProps, 'isPageHeading'>> = ({ className, size
1619
/>
1720
);
1821

19-
export const Label: FC<LabelProps> = ({ isPageHeading, children, ...rest }) => {
22+
export const Label: FC<LabelProps> = (props) => {
23+
const { children, isPageHeading, headingLevel = 'h1', ...rest } = props;
24+
2025
if (!children) {
2126
return null;
2227
}
2328

24-
if (isPageHeading) {
29+
if (isPageHeading || props.headingLevel) {
2530
return (
26-
<h1 className="nhsuk-label-wrapper">
31+
<HeadingLevel className="nhsuk-label-wrapper" headingLevel={headingLevel}>
2732
<LabelComponent {...rest}>{children}</LabelComponent>
28-
</h1>
33+
</HeadingLevel>
2934
);
3035
}
3136

src/components/form-elements/label/__tests__/Label.test.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { render } from '@testing-library/react';
22

3-
import { Label } from '..';
3+
import { Label, type LabelProps } from '..';
44

55
import { type NHSUKSize } from '#util/types';
66

@@ -50,6 +50,21 @@ describe('Label', () => {
5050
},
5151
);
5252

53+
it.each<LabelProps>([
54+
{ headingLevel: 'h1' },
55+
{ headingLevel: 'h2' },
56+
{ headingLevel: 'h3' },
57+
{ headingLevel: 'h4' },
58+
])('renders as page heading with custom heading level $headingLevel', (props) => {
59+
const { container } = render(<Label {...props}>Text</Label>);
60+
61+
const headingEl = container.querySelector('.nhsuk-label-wrapper');
62+
const labelEl = headingEl?.querySelector('.nhsuk-label');
63+
64+
expect(headingEl?.tagName).toBe(props?.headingLevel?.toUpperCase());
65+
expect(labelEl).toHaveTextContent('Text');
66+
});
67+
5368
it('renders null with no children', () => {
5469
const { container } = render(<Label />);
5570

src/components/form-elements/legend/Legend.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@ export interface LegendProps
1111
size?: NHSUKSize;
1212
}
1313

14-
export const Legend: FC<LegendProps> = ({
15-
className,
16-
children,
17-
isPageHeading,
18-
headingLevel = 'h1',
19-
size,
20-
...rest
21-
}) => {
14+
export const Legend: FC<LegendProps> = (params) => {
15+
const { className, children, isPageHeading, headingLevel = 'h1', size, ...rest } = params;
16+
2217
if (!children) {
2318
return null;
2419
}
@@ -32,7 +27,7 @@ export const Legend: FC<LegendProps> = ({
3227
)}
3328
{...rest}
3429
>
35-
{isPageHeading ? (
30+
{isPageHeading || params.headingLevel ? (
3631
<HeadingLevel className="nhsuk-fieldset__heading" headingLevel={headingLevel}>
3732
{children}
3833
</HeadingLevel>

src/components/form-elements/legend/__tests__/Legend.test.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { render } from '@testing-library/react';
22

3-
import { Legend } from '..';
3+
import { Legend, type LegendProps } from '..';
44

55
import { type NHSUKSize } from '#util/types/NHSUKTypes';
66

@@ -40,6 +40,21 @@ describe('Legend', () => {
4040
},
4141
);
4242

43+
it.each<LegendProps>([
44+
{ headingLevel: 'h1' },
45+
{ headingLevel: 'h2' },
46+
{ headingLevel: 'h3' },
47+
{ headingLevel: 'h4' },
48+
])('renders as page heading with with custom heading level $headingLevel', (props) => {
49+
const { container } = render(<Legend {...props}>Text</Legend>);
50+
51+
const legendEl = container.querySelector('.nhsuk-fieldset__legend');
52+
const headingEl = legendEl?.querySelector('.nhsuk-fieldset__heading');
53+
54+
expect(legendEl).toHaveTextContent('Text');
55+
expect(headingEl?.tagName).toBe(props?.headingLevel?.toUpperCase());
56+
});
57+
4358
it('renders null with no children', () => {
4459
const { container } = render(<Legend />);
4560

0 commit comments

Comments
 (0)