Skip to content

Commit

Permalink
feat: Add Icon to Error Message (#1746)
Browse files Browse the repository at this point in the history
  • Loading branch information
alimpens authored Nov 6, 2024
1 parent 7b275d0 commit eca5fe4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/css/src/components/error-message/error-message.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

.ams-error-message {
color: var(--ams-error-message-color);
display: inline-flex;
font-family: var(--ams-error-message-font-family);
font-size: var(--ams-error-message-font-size);
font-weight: var(--ams-error-message-font-weight);
gap: var(--ams-error-message-gap);
line-height: var(--ams-error-message-line-height);

@include text-rendering;
Expand Down
14 changes: 14 additions & 0 deletions packages/react/src/ErrorMessage/ErrorMessage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ describe('Error message', () => {

expect(ref.current).toBe(component)
})

it('shows an icon', () => {
const { container } = render(<ErrorMessage />)

const iconWrapper = container.querySelector('.ams-icon')
const icon = container.querySelector('svg')

expect(iconWrapper).toBeInTheDocument()
expect(icon).toBeInTheDocument()
})

// TODO: we can't currently test this, because we can't pass a class or anything to the SVG
// We plan on changing this, so we can test this in the future
it.skip('shows a custom icon', () => {})
})
7 changes: 6 additions & 1 deletion packages/react/src/ErrorMessage/ErrorMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
* Copyright Gemeente Amsterdam
*/

import { AlertIcon } from '@amsterdam/design-system-react-icons'
import clsx from 'clsx'
import { forwardRef } from 'react'
import type { ForwardedRef, HTMLAttributes, PropsWithChildren } from 'react'
import { Icon } from '../Icon'

export type ErrorMessageProps = {
/** An icon to display instead of the default icon. */
icon?: Function
/** 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,
{ children, className, icon, prefix = 'Invoerfout', ...restProps }: ErrorMessageProps,
ref: ForwardedRef<HTMLParagraphElement>,
) => (
<p {...restProps} ref={ref} className={clsx('ams-error-message', className)}>
<Icon svg={icon ? icon : AlertIcon} size="level-6" />
<span className="ams-visually-hidden">
{prefix}
{': '}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"font-family": { "value": "{ams.text.font-family}" },
"font-size": { "value": "{ams.text.level.6.font-size}" },
"font-weight": { "value": "{ams.text.font-weight.normal}" },
"gap": { "value": "{ams.space.xs}" },
"line-height": { "value": "{ams.text.level.6.line-height}" }
}
}
Expand Down
7 changes: 7 additions & 0 deletions storybook/src/components/ErrorMessage/ErrorMessage.docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ 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.CustomPrefix} />

### With custom icon

Replace the icon with another to use Error Message in a different theme or visual identity.
Applications for the City of Amsterdam must use the default icon.

<Canvas of={ErrorMessageStories.WithCustomIcon} />
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { ErrorMessage } from '@amsterdam/design-system-react/src'
import { AnnouncementIcon } from '@amsterdam/design-system-react-icons'
import { Meta, StoryObj } from '@storybook/react'

const meta = {
Expand Down Expand Up @@ -31,3 +32,9 @@ export const CustomPrefix: Story = {
prefix: 'Error',
},
}

export const WithCustomIcon: Story = {
args: {
icon: AnnouncementIcon,
},
}

0 comments on commit eca5fe4

Please sign in to comment.