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

Update ErrorSummary component to forward refs #135

Merged
merged 3 commits into from
Apr 12, 2023
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
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