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 typings for Toast component #3434

Open
wants to merge 3 commits into
base: release-22.x
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions src/Toast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ components:
- Toast
categories:
- Overlays
status: 'New'
status: 'Stable'
designStatus: 'Done'
devStatus: 'Done'
notes: ''
Expand Down Expand Up @@ -39,7 +39,7 @@ notes: ''
Example of a basic Toast.
</Toast>

<Button variant="primary" onClick={() => setShow(true)}>Show Toast</Button>
<Button onClick={() => setShow(true)}>Show Toast</Button>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default variant for the Button component is primary.

</>
);
}
Expand All @@ -64,7 +64,7 @@ notes: ''
Success! Example of a Toast with a button.
</Toast>

<Button variant="primary" onClick={() => setShow(true)}>Show Toast</Button>
<Button onClick={() => setShow(true)}>Show Toast</Button>
</>
);
}
Expand All @@ -89,7 +89,7 @@ notes: ''
Success! Example of a Toast with a link.
</Toast>

<Button variant="primary" onClick={() => setShow(true)}>Show Toast</Button>
<Button onClick={() => setShow(true)}>Show Toast</Button>
</>
);
}
Expand Down
22 changes: 10 additions & 12 deletions src/Toast/Toast.test.jsx → src/Toast/Toast.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { IntlProvider } from 'react-intl';
import userEvent from '@testing-library/user-event';

import Toast from '.';

/* eslint-disable-next-line react/prop-types */
function ToastWrapper({ children, ...props }) {
function ToastWrapper({ children, ...props }: React.ComponentProps<typeof Toast>) {
return (
<IntlProvider locale="en">
<Toast {...props}>
Expand All @@ -17,7 +15,7 @@ function ToastWrapper({ children, ...props }) {
}

describe('<Toast />', () => {
const onCloseHandler = () => {};
const onCloseHandler = jest.fn();
const props = {
onClose: onCloseHandler,
show: true,
Expand All @@ -44,7 +42,7 @@ describe('<Toast />', () => {
{...props}
action={{
label: 'Optional action',
onClick: () => {},
onClick: jest.fn(),
}}
>
Success message.
Expand All @@ -55,19 +53,19 @@ describe('<Toast />', () => {
});
it('autohide is set to false on onMouseOver and true on onMouseLeave', async () => {
render(
<ToastWrapper data-testid="toast" {...props}>
<ToastWrapper {...props}>
Success message.
</ToastWrapper>,
);
const toast = screen.getByTestId('toast');
const toast = screen.getByRole('alert');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the component now does not have an extra role, it is possible to find an element without a test id

image

await userEvent.hover(toast);
setTimeout(() => {
expect(screen.getByText('Success message.')).toEqual(true);
expect(screen.getByText('Success message.')).toBeTruthy();
expect(toast).toHaveLength(1);
}, 6000);
await userEvent.unhover(toast);
setTimeout(() => {
expect(screen.getByText('Success message.')).toEqual(false);
expect(screen.getByText('Success message.')).toBeTruthy();
expect(toast).toHaveLength(1);
}, 6000);
});
Expand All @@ -77,15 +75,15 @@ describe('<Toast />', () => {
Success message.
</ToastWrapper>,
);
const toast = screen.getByTestId('toast');
const toast = screen.getByRole('alert');
toast.focus();
setTimeout(() => {
expect(screen.getByText('Success message.')).toEqual(true);
expect(screen.getByText('Success message.')).toBeTruthy();
expect(toast).toHaveLength(1);
}, 6000);
await userEvent.tab();
setTimeout(() => {
expect(screen.getByText('Success message.')).toEqual(false);
expect(screen.getByText('Success message.')).toBeTruthy();
expect(toast).toHaveLength(1);
}, 6000);
});
Expand Down
40 changes: 0 additions & 40 deletions src/Toast/ToastContainer.jsx

This file was deleted.

3 changes: 2 additions & 1 deletion src/Toast/ToastContainer.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@use "sass:map";
@import "variables";

.toast-container {
Expand All @@ -11,7 +12,7 @@
left: 0;
}

@media only screen and (width <= 768px) {
@media (max-width: map.get($grid-breakpoints, "md")) {
bottom: $toast-container-gutter-sm;
right: $toast-container-gutter-sm;
left: $toast-container-gutter-sm;
Expand Down
32 changes: 32 additions & 0 deletions src/Toast/ToastContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ReactNode, useEffect, useState } from 'react';
import ReactDOM from 'react-dom';

interface ToastContainerProps {
children: ReactNode;
}

const TOAST_ROOT_ID = 'toast-root';

function ToastContainer({ children }: ToastContainerProps) {
const [rootElement, setRootElement] = useState<HTMLElement | null>(null);

useEffect(() => {
if (typeof document !== 'undefined') {
let existingElement = document.getElementById(TOAST_ROOT_ID);

if (!existingElement) {
existingElement = document.createElement('div');
existingElement.id = TOAST_ROOT_ID;
existingElement.className = 'toast-container';
existingElement.setAttribute('aria-live', 'polite');
existingElement.setAttribute('aria-atomic', 'true');
document.body.appendChild(existingElement);
}
setRootElement(existingElement);
}
}, []);

return rootElement ? ReactDOM.createPortal(children, rootElement) : null;
}

export default ToastContainer;
9 changes: 5 additions & 4 deletions src/Toast/index.scss
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@use "sass:map";
@import "variables";
@import "~bootstrap/scss/toasts";

.toast {
background-color: $toast-background-color;
box-shadow: $toast-box-shadow;
margin: 0;
padding: 1rem;
padding: $spacer;
position: relative;
border-radius: $toast-border-radius;
z-index: 2;
Expand Down Expand Up @@ -38,15 +39,15 @@
}

& + .btn {
margin-top: 1rem;
margin-top: $spacer;
}
}

@media only screen and (width <= 768px) {
@media (max-width: map.get($grid-breakpoints, "md")) {
max-width: 100%;
}

@media only screen and (width >= 768px) {
@media (min-width: map.get($grid-breakpoints, "md")) {
min-width: $toast-max-width;
max-width: $toast-max-width;
}
Expand Down
33 changes: 27 additions & 6 deletions src/Toast/index.jsx → src/Toast/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useState } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';

import BaseToast from 'react-bootstrap/Toast';
import { useIntl } from 'react-intl';

Expand All @@ -14,16 +13,40 @@ import IconButton from '../IconButton';
export const TOAST_CLOSE_LABEL_TEXT = 'Close';
export const TOAST_DELAY = 5000;

interface ToastAction {
label: string;
href?: string;
onClick?: () => void;
}

interface ToastProps {
children: string;
onClose: () => void;
show: boolean;
action?: ToastAction;
closeLabel?: string;
delay?: number;
className?: string;
}

function Toast({
action, children, className, closeLabel, onClose, show, ...rest
}) {
action,
children,
className,
closeLabel,
onClose,
show,
...rest
}: ToastProps) {
const intl = useIntl();
const [autoHide, setAutoHide] = useState(true);

const intlCloseLabel = closeLabel || intl.formatMessage({
id: 'pgn.Toast.closeLabel',
defaultMessage: 'Close',
description: 'Close label for Toast component',
});

return (
<ToastContainer>
<BaseToast
Expand All @@ -37,9 +60,7 @@ function Toast({
show={show}
{...rest}
>
<div
className="toast-header"
>
<div className="toast-header">
<p className="small">{children}</p>
<div className="toast-header-btn-container">
<IconButton
Expand Down
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export { default as ModalDialog, MODAL_DIALOG_CLOSE_LABEL } from './Modal/ModalD
export { default as ModalLayer } from './Modal/ModalLayer';
export { default as Overlay, OverlayTrigger } from './Overlay';
export { default as Portal } from './Modal/Portal';
export { default as Toast, TOAST_CLOSE_LABEL_TEXT, TOAST_DELAY } from './Toast';
export { default as Tooltip } from './Tooltip';
export { default as useWindowSize, type WindowSizeData } from './hooks/useWindowSizeHook';
export { default as useToggle, type Toggler, type ToggleHandlers } from './hooks/useToggleHook';
Expand Down Expand Up @@ -163,7 +164,6 @@ export const
// from './Tabs';
/** @deprecated Replaced by `Form.Control`. */
export const TextArea: any; // from './TextArea';
export const Toast: any, TOAST_CLOSE_LABEL_TEXT: string, TOAST_DELAY: number; // from './Toast';
/** @deprecated Replaced by `Form.Group`. */
export const ValidationFormGroup: any; // from './ValidationFormGroup';
export const TransitionReplace: any; // from './TransitionReplace';
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export { default as ModalDialog, MODAL_DIALOG_CLOSE_LABEL } from './Modal/ModalD
export { default as ModalLayer } from './Modal/ModalLayer';
export { default as Overlay, OverlayTrigger } from './Overlay';
export { default as Portal } from './Modal/Portal';
export { default as Toast, TOAST_CLOSE_LABEL_TEXT, TOAST_DELAY } from './Toast';
export { default as Tooltip } from './Tooltip';
export { default as useWindowSize } from './hooks/useWindowSizeHook';
export { default as useToggle } from './hooks/useToggleHook';
Expand Down Expand Up @@ -163,7 +164,6 @@ export {
} from './Tabs';
/** @deprecated Replaced by `Form.Control`. */
export { default as TextArea } from './TextArea';
export { default as Toast, TOAST_CLOSE_LABEL_TEXT, TOAST_DELAY } from './Toast';
/** @deprecated Replaced by `Form.Group`. */
export { default as ValidationFormGroup } from './ValidationFormGroup';
export { default as TransitionReplace } from './TransitionReplace';
Expand Down