-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add StudioErrorMessage component (#13943)
- Loading branch information
Showing
5 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...d/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.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,26 @@ | ||
import React from 'react'; | ||
import type { Meta, StoryFn } from '@storybook/react'; | ||
import { StudioErrorMessage } from './StudioErrorMessage'; | ||
|
||
type Story = StoryFn<typeof StudioErrorMessage>; | ||
|
||
const meta: Meta = { | ||
title: 'Components/StudioErrorMessage', | ||
component: StudioErrorMessage, | ||
argTypes: { | ||
size: { | ||
control: 'select', | ||
options: ['xs', 'sm', 'md', 'lg'], | ||
}, | ||
children: { | ||
control: 'text', | ||
}, | ||
}, | ||
}; | ||
export const Preview: Story = (args): React.ReactElement => <StudioErrorMessage {...args} />; | ||
|
||
Preview.args = { | ||
children: 'Lorem ipsum dolor sit amet.', | ||
size: 'sm', | ||
}; | ||
export default meta; |
36 changes: 36 additions & 0 deletions
36
...tend/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.test.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,36 @@ | ||
import type { ForwardedRef } from 'react'; | ||
import React from 'react'; | ||
import type { StudioErrorMessageProps } from './StudioErrorMessage'; | ||
import { StudioErrorMessage } from './StudioErrorMessage'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { testCustomAttributes } from '../../test-utils/testCustomAttributes'; | ||
import { testRefForwarding } from '../../test-utils/testRefForwarding'; | ||
import { testRootClassNameAppending } from '../../test-utils/testRootClassNameAppending'; | ||
|
||
// Test data: | ||
const errorMessage = 'Test error message'; | ||
const defaultProps: StudioErrorMessageProps = { children: errorMessage }; | ||
|
||
describe('StudioErrorMessage', () => { | ||
it('Renders the given error message', () => { | ||
renderErrorMessage(); | ||
expect(screen.getByText(errorMessage)).toBeInTheDocument(); | ||
}); | ||
|
||
it('Forwards the ref', () => { | ||
testRefForwarding<HTMLParagraphElement>((ref) => renderErrorMessage({}, ref)); | ||
}); | ||
|
||
it('Accepts custom attributes', () => { | ||
testCustomAttributes<HTMLParagraphElement>(renderErrorMessage); | ||
}); | ||
|
||
it('Applies the class name to the root element', () => { | ||
testRootClassNameAppending((className) => renderErrorMessage({ className })); | ||
}); | ||
}); | ||
|
||
const renderErrorMessage = ( | ||
props: StudioErrorMessageProps = {}, | ||
ref?: ForwardedRef<HTMLParagraphElement>, | ||
) => render(<StudioErrorMessage {...defaultProps} {...props} ref={ref} />); |
11 changes: 11 additions & 0 deletions
11
frontend/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.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,11 @@ | ||
import React, { forwardRef } from 'react'; | ||
import type { ErrorMessageProps } from '@digdir/designsystemet-react'; | ||
import { ErrorMessage } from '@digdir/designsystemet-react'; | ||
|
||
export type StudioErrorMessageProps = ErrorMessageProps; | ||
|
||
export const StudioErrorMessage = forwardRef<HTMLParagraphElement, StudioErrorMessageProps>( | ||
(props, ref) => <ErrorMessage size='sm' {...props} ref={ref} />, | ||
); | ||
|
||
StudioErrorMessage.displayName = 'StudioErrorMessage'; |
1 change: 1 addition & 0 deletions
1
frontend/libs/studio-components/src/components/StudioErrorMessage/index.ts
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 @@ | ||
export * from './StudioErrorMessage'; |
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