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

feat: Add StudioErrorMessage component #13943

Merged
merged 1 commit into from
Oct 31, 2024
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
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;
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} />);
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';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './StudioErrorMessage';
3 changes: 2 additions & 1 deletion frontend/libs/studio-components/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './StudioAlert';
export * from './StudioActionCloseButton';
export * from './StudioAlert';
export * from './StudioAnimateHeight';
export * from './StudioAvatar';
export * from './StudioBetaTag';
Expand All @@ -18,6 +18,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';
Expand Down
Loading