Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default ErrorSummary attributes #229

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions src/components/form-elements/error-summary/ErrorSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ import React, {
RefAttributes,
} from 'react';
import classNames from 'classnames';
import useDevWarning from '@util/hooks/UseDevWarning';

const ErrorSummaryTitle: FC<HTMLProps<HTMLHeadingElement>> = ({ className, ...rest }) => (
<h2 className={classNames('nhsuk-error-summary__title', className)} {...rest} />
const DefaultErrorSummaryTitleID = 'error-summary-title';

const ErrorSummaryTitle: FC<HTMLProps<HTMLHeadingElement>> = ({
className,
id = DefaultErrorSummaryTitleID,
...rest
}) => (
<h2 className={classNames('nhsuk-error-summary__title', className)} id={id} {...rest} />
);


const ErrorSummaryBody: FC<HTMLProps<HTMLDivElement>> = ({ className, ...rest }) => (
<div className={classNames('nhsuk-error-summary__body', className)} {...rest} />
);
Expand All @@ -37,10 +45,31 @@ interface ErrorSummary
}

const ErrorSummaryDiv = forwardRef<HTMLDivElement, HTMLProps<HTMLDivElement>>(
({ className, ...rest }, ref) => (
<div className={classNames('nhsuk-error-summary', className)} ref={ref} {...rest} />
),
);
({
className,
tabIndex = -1,
role = 'alert',
'aria-labelledby': ariaLabelledBy = DefaultErrorSummaryTitleID,
...rest
},
ref
) => {
useDevWarning('The ErrorSummary component should always have a tabIndex of -1', () => tabIndex !== -1)
useDevWarning('The ErrorSummary component should always have a role of alert', () => role !== 'alert')

return (
<div
className={classNames('nhsuk-error-summary', className)}
ref={ref}
tabIndex={tabIndex}
role={role}
aria-labelledby={ariaLabelledBy}
{...rest}
/>
)
});


ErrorSummaryDiv.displayName = 'ErrorSummary';

const ErrorSummary: ErrorSummary = Object.assign(ErrorSummaryDiv, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ describe('ErrorSummary', () => {
expect(ref.current).not.toBeNull();
});

it('has default props', () => {
const { container } = render(<ErrorSummary />);

expect(container.querySelector('div')?.getAttribute('tabindex')).toBe('-1');
expect(container.querySelector('div')?.getAttribute('role')).toBe('alert');
expect(container.querySelector('div')?.getAttribute('aria-labelledby')).toBe('error-summary-title');
})

it('throws a dev warning if tabIndex is not -1', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});
render(<ErrorSummary tabIndex={0} />);
expect(console.warn).toHaveBeenCalledWith('The ErrorSummary component should always have a tabIndex of -1');
})

it('throws a dev warning if role is not alert', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});
render(<ErrorSummary role="status" />);
expect(console.warn).toHaveBeenCalledWith('The ErrorSummary component should always have a role of alert');
})

describe('ErrorSummary.Title', () => {
it('matches snapshot', () => {
const { container } = render(<ErrorSummary.Title>Title</ErrorSummary.Title>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ exports[`ErrorSummary ErrorSummary.Title matches snapshot: ErrorSummary.Title 1`
<div>
<h2
class="nhsuk-error-summary__title"
id="error-summary-title"
>
Title
</h2>
Expand All @@ -43,7 +44,10 @@ exports[`ErrorSummary ErrorSummary.Title matches snapshot: ErrorSummary.Title 1`
exports[`ErrorSummary matches snapshot: ErrorSummary 1`] = `
<div>
Tomdango marked this conversation as resolved.
Show resolved Hide resolved
<div
aria-labelledby="error-summary-title"
class="nhsuk-error-summary"
role="alert"
tabindex="-1"
/>
</div>
`;
8 changes: 4 additions & 4 deletions stories/Form Elements/ErrorSummary.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import { Meta, StoryObj } from '@storybook/react';
*
* const Element = () => {
* return (
* <ErrorSummary aria-labelledby="error-summary-title" role="alert" tabIndex={-1}>
* <ErrorSummary.Title id="error-summary-title">There is a problem</ErrorSummary.Title>
* <ErrorSummary>
* <ErrorSummary.Title>There is a problem</ErrorSummary.Title>
* <ErrorSummary.Body>
* <p>Optional description of the errors and how to correct them</p>
* <ErrorSummary.List>
Expand Down Expand Up @@ -55,8 +55,8 @@ ErrorSummary.Item.displayName = 'ErrorSummary.Item';

export const Standard: Story = {
render: (args) => (
<ErrorSummary aria-labelledby="error-summary-title" role="alert" tabIndex={-1}>
<ErrorSummary.Title id="error-summary-title">There is a problem</ErrorSummary.Title>
<ErrorSummary>
<ErrorSummary.Title>There is a problem</ErrorSummary.Title>
<ErrorSummary.Body>
<p>Optional description of the errors and how to correct them</p>
<ErrorSummary.List>
Expand Down
Loading