-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Vincent Smedinga <v.smedinga@amsterdam.nl>
- Loading branch information
1 parent
d28b1e3
commit 1dec2ea
Showing
15 changed files
with
293 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- @license CC0-1.0 --> | ||
|
||
# Error Message | ||
|
||
Show an error message when there is a form field validation error. | ||
In the error message explain what went wrong and how to fix it. | ||
|
||
For guidance and examples on using error messages in a form, | ||
refer to the [Field](/docs/components-forms-field--docs) and [Field Set](/docs/components-forms-field-set--docs) documentation. | ||
|
||
Read the documentation by [NL Design System](https://www.nldesignsystem.nl/richtlijnen/formulieren/foutmeldingen) and [Gov.uk](https://design-system.service.gov.uk/components/error-message/) for more information on the contents of error messages and when to show them. |
22 changes: 22 additions & 0 deletions
22
packages/css/src/components/error-message/error-message.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @license EUPL-1.2+ | ||
* Copyright Gemeente Amsterdam | ||
*/ | ||
|
||
@import "../../common/text-rendering"; | ||
|
||
@mixin reset { | ||
box-sizing: border-box; | ||
margin-block: 0; | ||
} | ||
|
||
.ams-error-message { | ||
color: var(--ams-error-message-color); | ||
font-family: var(--ams-error-message-font-family); | ||
font-size: var(--ams-error-message-font-size); | ||
font-weight: var(--ams-error-message-font-weight); | ||
line-height: var(--ams-error-message-line-height); | ||
|
||
@include text-rendering; | ||
@include reset; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import { createRef } from 'react' | ||
import { ErrorMessage } from './ErrorMessage' | ||
import '@testing-library/jest-dom' | ||
|
||
describe('Error message', () => { | ||
it('renders', () => { | ||
render(<ErrorMessage />) | ||
const component = screen.getByRole('paragraph') | ||
|
||
expect(component).toBeInTheDocument() | ||
expect(component).toBeVisible() | ||
}) | ||
|
||
it('renders a design system BEM class name', () => { | ||
render(<ErrorMessage />) | ||
const component = screen.getByRole('paragraph') | ||
|
||
expect(component).toHaveClass('ams-error-message') | ||
}) | ||
|
||
it('renders an additional class name', () => { | ||
render(<ErrorMessage className="extra" />) | ||
const component = screen.getByRole('paragraph') | ||
|
||
expect(component).toHaveClass('ams-error-message extra') | ||
}) | ||
|
||
it('renders a Dutch prefix by default', () => { | ||
render(<ErrorMessage />) | ||
const component = screen.getByText('Invoerfout', { exact: false }) | ||
|
||
expect(component).toBeInTheDocument() | ||
}) | ||
|
||
it('renders a custom prefix', () => { | ||
render(<ErrorMessage prefix="Error" />) | ||
const component = screen.getByText('Error', { exact: false }) | ||
|
||
expect(component).toBeInTheDocument() | ||
}) | ||
|
||
it('supports ForwardRef in React', () => { | ||
const ref = createRef<HTMLParagraphElement>() | ||
|
||
render(<ErrorMessage ref={ref} />) | ||
const component = screen.getByRole('paragraph') | ||
|
||
expect(ref.current).toBe(component) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @license EUPL-1.2+ | ||
* Copyright Gemeente Amsterdam | ||
*/ | ||
|
||
import clsx from 'clsx' | ||
import { forwardRef } from 'react' | ||
import type { ForwardedRef, HTMLAttributes, PropsWithChildren } from 'react' | ||
import { VisuallyHidden } from '../VisuallyHidden' | ||
|
||
export type ErrorMessageProps = { | ||
/** An accessible phrase that screen readers announce before the error message. Should translate to something like ‘input error’. */ | ||
prefix?: string | ||
} & PropsWithChildren<HTMLAttributes<HTMLParagraphElement>> | ||
|
||
export const ErrorMessage = forwardRef( | ||
( | ||
{ children, className, prefix = 'Invoerfout', ...restProps }: ErrorMessageProps, | ||
ref: ForwardedRef<HTMLParagraphElement>, | ||
) => ( | ||
<p {...restProps} ref={ref} className={clsx('ams-error-message', className)}> | ||
<VisuallyHidden> | ||
{prefix} | ||
{': '} | ||
</VisuallyHidden> | ||
{children} | ||
</p> | ||
), | ||
) | ||
|
||
ErrorMessage.displayName = 'ErrorMessage' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<!-- @license CC0-1.0 --> | ||
|
||
# React Error Message component | ||
|
||
[Error Message documentation](../../../css/src/components/error-message/README.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { ErrorMessage } from './ErrorMessage' | ||
export type { ErrorMessageProps } from './ErrorMessage' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
proprietary/tokens/src/components/ams/error-message.tokens.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"ams": { | ||
"error-message": { | ||
"color": { "value": "{ams.color.primary-red}" }, | ||
"font-family": { "value": "{ams.text.font-family}" }, | ||
"font-size": { "value": "{ams.text.level.6.font-size}" }, | ||
"font-weight": { "value": "{ams.text.font-weight.normal}" }, | ||
"line-height": { "value": "{ams.text.level.6.line-height}" } | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
storybook/src/components/ErrorMessage/ErrorMessage.docs.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Canvas, Controls, Markdown, Meta, Primary } from "@storybook/blocks"; | ||
import * as ErrorMessageStories from "./ErrorMessage.stories.tsx"; | ||
import README from "../../../../packages/css/src/components/error-message/README.md?raw"; | ||
|
||
<Meta of={ErrorMessageStories} /> | ||
|
||
<Markdown>{README}</Markdown> | ||
|
||
<Primary /> | ||
|
||
<Controls /> | ||
|
||
## With a custom prefix | ||
|
||
Error messages are automatically prefixed with a visually hidden text, the Dutch word "Invoerfout". | ||
This makes the error message more clear for screen reader users. | ||
If you want to change this prefix, to support another language for example, you can use the `prefix` prop. | ||
|
||
<Canvas of={ErrorMessageStories.WithCustomPrefix} /> |
33 changes: 33 additions & 0 deletions
33
storybook/src/components/ErrorMessage/ErrorMessage.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @license EUPL-1.2+ | ||
* Copyright Gemeente Amsterdam | ||
*/ | ||
|
||
import { ErrorMessage } from '@amsterdam/design-system-react/src' | ||
import { Meta, StoryObj } from '@storybook/react' | ||
|
||
const meta = { | ||
title: 'Components/Forms/Error Message', | ||
component: ErrorMessage, | ||
args: { | ||
children: 'Vul een geldig e-mailadres in, bijvoorbeeld naam@voorbeeld.nl.', | ||
}, | ||
argTypes: { | ||
children: { | ||
table: { disable: false }, | ||
}, | ||
}, | ||
} satisfies Meta<typeof ErrorMessage> | ||
|
||
export default meta | ||
|
||
type Story = StoryObj<typeof meta> | ||
|
||
export const Default: Story = {} | ||
|
||
export const WithCustomPrefix: Story = { | ||
args: { | ||
children: 'Enter an email address in the correct format, like name@example.com', | ||
prefix: 'Error', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters