Skip to content

Commit

Permalink
feat(ffe-system-message-react): rewrite to ts
Browse files Browse the repository at this point in the history
* custom animmation lenght removed
* onClose -> onCloseRest

BREAKING CHANGE: changed props
  • Loading branch information
pethel authored and Peter Hellstrand committed May 21, 2024
1 parent c15ab02 commit 63e1f7f
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 301 deletions.
5 changes: 0 additions & 5 deletions packages/ffe-system-message-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ npm install --save @sb1/ffe-system-message-react

Full documentation on system message usage is available at https://design.sparebank1.no/komponenter/meldinger/#systemmessage.

## TypeScript definition files

This component supports TypeScript - please update `index.d.ts` if you change any
of the external methods or properties in this component.

## Development

To start a local development server, run the following from the designsystem root folder:
Expand Down
10 changes: 5 additions & 5 deletions packages/ffe-system-message-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
"url": "ssh://[email protected]:SpareBank1/designsystem.git"
},
"scripts": {
"build": "ffe-buildtool babel",
"build": "ffe-buildtool tsc",
"watch": "ffe-buildtool babel-watch",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"lint": "eslint src --ext ts,tsx",
"lint:fix": "eslint src --fix --ext ts,tsx",
"test": "ffe-buildtool jest",
"test:watch": "ffe-buildtool jest --watch"
},
"dependencies": {
"@sb1/ffe-collapse-react": "^4.0.2",
"@sb1/ffe-grid-react": "^13.2.15",
"@sb1/ffe-icons-react": "^10.0.1",
"@sb1/ffe-system-message": "^8.0.17",
"classnames": "^2.3.1",
"prop-types": "^15.7.2"
"classnames": "^2.3.1"
},
"devDependencies": {
"@sb1/ffe-buildtool": "^0.6.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react';
import { func, string, number, node, oneOf, bool } from 'prop-types';
import { Icon } from '@sb1/ffe-icons-react';
import texts from './texts';
import { texts } from './texts';
import { SystemMessage, SystemMessageProps } from './SystemMessage';

import SystemMessage from './SystemMessage';

export default function SystemErrorMessage(props) {
const {
alert = true,
locale = 'nb',
animationLengthMs = 300,
onClose = f => f,
...rest
} = props;
export interface SystemErrorMessageProps
extends Omit<SystemMessageProps, 'icon' | 'modifier'> {
/** When false, role is not set to alert, avoids message from being read up immediately after page load. Default value is true. */
alert?: boolean;
}

export const SystemErrorMessage: React.FC<SystemErrorMessageProps> = ({
locale = 'nb',
alert,
...rest
}) => {
const priorityHighIconSmall =
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjAiIHZpZXdCb3g9IjAgLTk2MCA5NjAgOTYwIiB3aWR0aD0iMjAiPjxwYXRoIGQ9Ik00NzkuNzg4LTE4Ny4wOHEtMjEuNTM3IDAtMzYuNjYyLTE1LjMzN3QtMTUuMTI1LTM2Ljg3NHEwLTIxLjUzNyAxNS4zMzctMzYuNjYyIDE1LjMzNy0xNS4xMjQgMzYuODc0LTE1LjEyNCAyMS41MzcgMCAzNi42NjIgMTUuMzM3dDE1LjEyNSAzNi44NzRxMCAyMS41MzctMTUuMzM3IDM2LjY2MS0xNS4zMzcgMTUuMTI1LTM2Ljg3NCAxNS4xMjVabS0uMDYyLTE5Ni4xNTFxLTE5LjM0MSAwLTMyLjg0LTEzLjcwOS0xMy41LTEzLjcwOS0xMy41LTMyLjk2di0zMTYuNjg1cTAtMTkuMjUxIDEzLjc3NC0zMi43OTQgMTMuNzczLTEzLjU0MiAzMy4xMTQtMTMuNTQyIDE5LjM0MSAwIDMyLjg0IDEzLjcwOSAxMy41IDEzLjcwOSAxMy41IDMyLjk2djMxNi42ODVxMCAxOS4yNTEtMTMuNzc0IDMyLjc5NC0xMy43NzMgMTMuNTQyLTMzLjExNCAxMy41NDJaIi8+PC9zdmc+';
return (
Expand All @@ -23,28 +23,7 @@ export default function SystemErrorMessage(props) {
locale={locale}
icon={<Icon fileUrl={priorityHighIconSmall} size="sm" />}
role={alert ? 'alert' : 'group'}
animationLengthMs={animationLengthMs}
onClose={onClose}
{...rest}
/>
);
}

SystemErrorMessage.propTypes = {
animationLengthMs: number,
/** The content of the system message */
children: node.isRequired,
/** Additional classes added to the surrounding div */
className: string,
/** 'nb', 'nn', or 'en' */
locale: oneOf(['en', 'nb', 'nn']),
/**
* The type of system message. Used internally only-
* @ignore
**/
modifier: oneOf(['error', 'info', 'success', 'news']),
/** Callback for when the system message has been closed (after animation ends) */
onClose: func,
/** When false, role is not set to alert, avoids message from being read up immediately after page load. Default value is true. */
alert: bool,
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React from 'react';
import { Icon } from '@sb1/ffe-icons-react';
import texts from './texts';
import SystemMessage from './SystemMessage';
import { oneOf } from 'prop-types';
import { texts } from './texts';
import { SystemMessage, SystemMessageProps } from './SystemMessage';
export interface SystemInfoMessageProps
extends Omit<SystemMessageProps, 'icon' | 'modifier'> {}

export default function SystemInfoMessage({ locale = 'nb', ...rest }) {
export const SystemInfoMessage: React.FC<SystemInfoMessageProps> = ({
locale = 'nb',
...rest
}) => {
const infoIconSmall =
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjAiIHZpZXdCb3g9IjAgLTk2MCA5NjAgOTYwIiB3aWR0aD0iMjAiPjxwYXRoIGQ9Ik00NzkuNzg4LTY3MnEtMjUuOTQyIDAtNDMuODY0LTE4LjEzNS0xNy45MjMtMTguMTM2LTE3LjkyMy00NC4wNzd0MTguMTM1LTQzLjg2NHExOC4xMzUtMTcuOTIzIDQ0LjA3Ni0xNy45MjMgMjUuOTQyIDAgNDMuODY0IDE4LjEzNiAxNy45MjMgMTguMTM1IDE3LjkyMyA0NC4wNzZ0LTE4LjEzNSA0My44NjRRNTA1LjcyOS02NzIgNDc5Ljc4OC02NzJabS4yNTcgNTA3Ljk5OXEtMjAuODE0IDAtMzUuNDI5LTE0LjU4NC0xNC42MTUtMTQuNTgzLTE0LjYxNS0zNS40MTZ2LTI5Ni42MTRxMC0yMC44MzMgMTQuNTctMzUuNDE2IDE0LjU3LTE0LjU4MyAzNS4zODQtMTQuNTgzdDM1LjQyOSAxNC41ODNxMTQuNjE1IDE0LjU4MyAxNC42MTUgMzUuNDE2djI5Ni42MTRxMCAyMC44MzMtMTQuNTcgMzUuNDE2LTE0LjU3IDE0LjU4NC0zNS4zODQgMTQuNTg0WiIvPjwvc3ZnPg==';
return (
Expand All @@ -16,9 +20,4 @@ export default function SystemInfoMessage({ locale = 'nb', ...rest }) {
{...rest}
/>
);
}

SystemInfoMessage.propTypes = {
/** 'nb', 'nn', or 'en' */
locale: oneOf(['en', 'nb', 'nn']),
};
110 changes: 0 additions & 110 deletions packages/ffe-system-message-react/src/SystemMessage.spec.js

This file was deleted.

120 changes: 120 additions & 0 deletions packages/ffe-system-message-react/src/SystemMessage.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import React from 'react';
import {
SystemErrorMessage,
SystemErrorMessageProps,
} from './SystemErrorMessage';
import { SystemInfoMessage, SystemInfoMessageProps } from './SystemInfoMessage';
import { SystemNewsMessage, SystemNewsMessageProps } from './SystemNewsMessage';
import {
SystemSuccessMessage,
SystemSuccessMessageProps,
} from './SystemSuccessMessage';
import { render, screen } from '@testing-library/react';

const defaultProps = {
children: <span>Message</span>,
};

const TEST_ID_ERROR = 'TEST_ID_ERROR';
const TEST_ID_INFO = 'TEST_ID_INFO';
const TEST_ID_NEWS = 'TEST_ID_NEWS';
const TEST_ID_SUCCESS = 'TEST_ID_SUCCESS';

const renderSystemErrorMessage = (props?: SystemErrorMessageProps) =>
render(
<SystemErrorMessage
{...defaultProps}
{...props}
data-testid={TEST_ID_ERROR}
/>,
);
const renderSystemInfoMessage = (props?: SystemInfoMessageProps) =>
render(
<SystemInfoMessage
{...defaultProps}
{...props}
data-testid={TEST_ID_INFO}
/>,
);
const renderSystemNewsMessage = (props?: SystemNewsMessageProps) =>
render(
<SystemNewsMessage
{...defaultProps}
{...props}
data-testid={TEST_ID_NEWS}
/>,
);
const renderSystemSuccessMessage = (props?: SystemSuccessMessageProps) =>
render(
<SystemSuccessMessage
{...defaultProps}
{...props}
data-testid={TEST_ID_SUCCESS}
/>,
);

describe('<SystemMessage />', () => {
it('applies the correct modifier classes to each type', () => {
renderSystemErrorMessage();
const error = screen.getByTestId(TEST_ID_ERROR);
expect(error.classList.contains('ffe-system-message-wrapper')).toBe(
true,
);
expect(
error.classList.contains('ffe-system-message-wrapper--error'),
).toBe(true);

renderSystemInfoMessage();
const info = screen.getByTestId(TEST_ID_INFO);
expect(info.classList.contains('ffe-system-message-wrapper')).toBe(
true,
);
expect(
info.classList.contains('ffe-system-message-wrapper--info'),
).toBe(true);

renderSystemNewsMessage();
const news = screen.getByTestId(TEST_ID_NEWS);
expect(news.classList.contains('ffe-system-message-wrapper')).toBe(
true,
);
expect(
news.classList.contains('ffe-system-message-wrapper--news'),
).toBe(true);

renderSystemSuccessMessage();
const success = screen.getByTestId(TEST_ID_SUCCESS);
expect(success.classList.contains('ffe-system-message-wrapper')).toBe(
true,
);
expect(
success.classList.contains('ffe-system-message-wrapper--success'),
).toBe(true);
});
it('renders with correct aria-label', () => {
renderSystemInfoMessage();
const info = screen.getByTestId(TEST_ID_INFO);
renderSystemSuccessMessage();
const success = screen.getByTestId(TEST_ID_SUCCESS);
expect(info.getAttribute('aria-label')).toBe('Infomelding');
expect(success.getAttribute('aria-label')).toBe('Suksessmelding');
});
it('renders with role group on container', () => {
renderSystemInfoMessage();
const info = screen.getByTestId(TEST_ID_INFO);
expect(info.getAttribute('role')).toBe('group');
});

it('renders a Norwegian aria label on the close button by default', () => {
renderSystemInfoMessage();
expect(
screen.queryByRole('button', { name: 'Lukk' }),
).toBeInTheDocument();
});
it('renders an English aria-label if locale is "en"', () => {
renderSystemInfoMessage({ locale: 'en' });
expect(
screen.queryByRole('button', { name: 'Close' }),
).toBeInTheDocument();
});
});
Loading

0 comments on commit 63e1f7f

Please sign in to comment.