Skip to content

Commit

Permalink
Update ErrorSummary component to forward refs (#135)
Browse files Browse the repository at this point in the history
This allows for things like programmatic keyboard focus in order to
fulfil accessibility requirements.

Co-authored-by: Phil Meier <[email protected]>
Co-authored-by: Thomas Judd-Cooper <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2023
1 parent af74fdf commit 4993b4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
23 changes: 13 additions & 10 deletions src/components/error-summary/ErrorSummary.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { HTMLProps } from 'react';
import React, {forwardRef, HTMLProps, PropsWithoutRef, RefAttributes} from 'react';
import classNames from 'classnames';

const ErrorSummaryTitle: React.FC<HTMLProps<HTMLHeadingElement>> = ({ className, ...rest }) => (
Expand All @@ -19,20 +19,23 @@ const ErrorSummaryListItem: React.FC<HTMLProps<HTMLAnchorElement>> = (props) =>
</li>
);

interface ErrorSummary extends React.FC<HTMLProps<HTMLDivElement>> {
interface ErrorSummary extends React.ForwardRefExoticComponent<PropsWithoutRef<HTMLProps<HTMLDivElement>> & RefAttributes<HTMLDivElement>> {
Title: React.FC<HTMLProps<HTMLHeadingElement>>;
Body: React.FC<HTMLProps<HTMLDivElement>>;
List: React.FC<HTMLProps<HTMLUListElement>>;
Item: React.FC<HTMLProps<HTMLAnchorElement>>;
}

const ErrorSummary: ErrorSummary = ({ className, ...rest }) => (
<div className={classNames('nhsuk-error-summary', className)} {...rest} />
);

ErrorSummary.Title = ErrorSummaryTitle;
ErrorSummary.Body = ErrorSummaryBody;
ErrorSummary.List = ErrorSummaryList;
ErrorSummary.Item = ErrorSummaryListItem;
const ErrorSummaryDiv = forwardRef<HTMLDivElement, HTMLProps<HTMLDivElement>>(({className, ...rest}, ref) =>
<div className={classNames('nhsuk-error-summary', className)} ref={ref} {...rest} />
)
ErrorSummaryDiv.displayName = "ErrorSummary"

const ErrorSummary: ErrorSummary = Object.assign(ErrorSummaryDiv, {
Title: ErrorSummaryTitle,
Body: ErrorSummaryBody,
List: ErrorSummaryList,
Item: ErrorSummaryListItem,
})

export default ErrorSummary;
9 changes: 8 additions & 1 deletion src/components/error-summary/__tests__/ErrorSummary.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { shallow } from 'enzyme';
import { mount, shallow } from 'enzyme';
import ErrorSummary from '..';

describe('ErrorSummary', () => {
Expand All @@ -9,6 +9,13 @@ describe('ErrorSummary', () => {
element.unmount();
});

it('forwards refs', () => {
const ref = React.createRef<HTMLDivElement>();
const element = mount(<ErrorSummary ref={ref}/>);
expect(ref.current).not.toBeNull();
element.unmount();
});

describe('ErrorSummary.Title', () => {
it('matches snapshot', () => {
const element = shallow(<ErrorSummary.Title>Title</ErrorSummary.Title>);
Expand Down

0 comments on commit 4993b4e

Please sign in to comment.