diff --git a/frontend/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.stories.tsx b/frontend/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.stories.tsx new file mode 100644 index 00000000000..34ac0de950a --- /dev/null +++ b/frontend/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.stories.tsx @@ -0,0 +1,26 @@ +import React from 'react'; +import type { Meta, StoryFn } from '@storybook/react'; +import { StudioErrorMessage } from './StudioErrorMessage'; + +type Story = StoryFn; + +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 => ; + +Preview.args = { + children: 'Lorem ipsum dolor sit amet.', + size: 'sm', +}; +export default meta; diff --git a/frontend/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.test.tsx b/frontend/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.test.tsx new file mode 100644 index 00000000000..9b389e23958 --- /dev/null +++ b/frontend/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.test.tsx @@ -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((ref) => renderErrorMessage({}, ref)); + }); + + it('Accepts custom attributes', () => { + testCustomAttributes(renderErrorMessage); + }); + + it('Applies the class name to the root element', () => { + testRootClassNameAppending((className) => renderErrorMessage({ className })); + }); +}); + +const renderErrorMessage = ( + props: StudioErrorMessageProps = {}, + ref?: ForwardedRef, +) => render(); diff --git a/frontend/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.tsx b/frontend/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.tsx new file mode 100644 index 00000000000..9c368239725 --- /dev/null +++ b/frontend/libs/studio-components/src/components/StudioErrorMessage/StudioErrorMessage.tsx @@ -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( + (props, ref) => , +); + +StudioErrorMessage.displayName = 'StudioErrorMessage'; diff --git a/frontend/libs/studio-components/src/components/StudioErrorMessage/index.ts b/frontend/libs/studio-components/src/components/StudioErrorMessage/index.ts new file mode 100644 index 00000000000..55c5c43446d --- /dev/null +++ b/frontend/libs/studio-components/src/components/StudioErrorMessage/index.ts @@ -0,0 +1 @@ +export * from './StudioErrorMessage'; diff --git a/frontend/libs/studio-components/src/components/index.ts b/frontend/libs/studio-components/src/components/index.ts index 033e84e0424..bea5c89d367 100644 --- a/frontend/libs/studio-components/src/components/index.ts +++ b/frontend/libs/studio-components/src/components/index.ts @@ -1,5 +1,5 @@ -export * from './StudioAlert'; export * from './StudioActionCloseButton'; +export * from './StudioAlert'; export * from './StudioAnimateHeight'; export * from './StudioAvatar'; export * from './StudioBetaTag'; @@ -19,6 +19,7 @@ export * from './StudioDisplayTile'; export * from './StudioDivider'; export * from './StudioDropdownMenu'; export * from './StudioError'; +export * from './StudioErrorMessage'; export * from './StudioExpression'; export * from './StudioFieldset'; export * from './StudioFileUploader';