Skip to content
Draft
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
22 changes: 22 additions & 0 deletions src/Toast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ notes: ''
}
```

## With Icon

```jsx live
() => {
const [show, setShow] = useState(false);

return (
<>
<Toast
icon={Settings}
onClose={() => setShow(false)}
show={show}
>
Processing.. Example of a Toast with an icon.
</Toast>

<Button onClick={() => setShow(true)}>Show Toast</Button>
</>
);
}
```

## With Button

```jsx live
Expand Down
16 changes: 16 additions & 0 deletions src/Toast/Toast.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { IntlProvider } from 'react-intl';
import userEvent from '@testing-library/user-event';

import { Info } from '../../icons';
import Toast from '.';

function ToastWrapper({ children, ...props }: React.ComponentProps<typeof Toast>) {
Expand Down Expand Up @@ -51,6 +53,20 @@ describe('<Toast />', () => {
const toastButton = screen.getByRole('button', { name: 'Optional action' });
expect(toastButton.className).toContain('btn');
});
it('renders optional icon', () => {
const { container } = render(
<ToastWrapper
{...props}
icon={Info}
iconClassName="icon-class"
>
Success message.
</ToastWrapper>,
);

const toastIcon = container.querySelector('.__pgn__icon');
expect(toastIcon).toBeInTheDocument();
});
it('autohide is set to false on onMouseOver and true on onMouseLeave', async () => {
render(
<ToastWrapper {...props}>
Expand Down
8 changes: 6 additions & 2 deletions src/Toast/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@
.toast-header {
align-items: center;
border-bottom: 0;
justify-content: space-between;
padding: 0;

.toast-header-icon {
margin-right: .75rem;
}

p {
font-size: var(--pgn-typography-font-size-sm);
margin: 0;
font-weight: var(--pgn-typography-weight-normal);
margin: 0 auto 0 0;
padding-right: .75rem;
}

Expand Down
22 changes: 13 additions & 9 deletions src/Toast/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useIntl } from 'react-intl';
import { Close } from '../../icons';
import ToastContainer from './ToastContainer';
import Button from '../Button';
import Icon from '../Icon';
import Icon, { type IconProps } from '../Icon';
import IconButton from '../IconButton';

export const TOAST_CLOSE_LABEL_TEXT = 'Close';
Expand All @@ -21,6 +21,8 @@ interface ToastAction {

interface ToastProps {
children: string;
icon?: React.ComponentType<IconProps>;
iconClassName?: string;
onClose: () => void;
show: boolean;
action?: ToastAction;
Expand All @@ -36,6 +38,9 @@ function Toast({
closeLabel,
onClose,
show,
icon,
iconClassName,
delay = TOAST_DELAY,
...rest
}: ToastProps) {
const intl = useIntl();
Expand All @@ -58,10 +63,12 @@ function Toast({
onMouseOut={() => setAutoHide(true)}
onMouseOver={() => setAutoHide(false)}
show={show}
delay={delay}
{...rest}
>
<div className="toast-header">
<p className="small">{children}</p>
{icon && <Icon className={classNames('toast-header-icon', iconClassName)} src={icon} />}
<p>{children}</p>
<div className="toast-header-btn-container">
<IconButton
iconAs={Icon}
Expand Down Expand Up @@ -90,13 +97,6 @@ function Toast({
);
}

Toast.defaultProps = {
action: null,
closeLabel: undefined,
delay: TOAST_DELAY,
className: undefined,
};

Toast.propTypes = {
/** A string or an element that is rendered inside the main body of the `Toast`. */
children: PropTypes.string.isRequired,
Expand Down Expand Up @@ -127,6 +127,10 @@ Toast.propTypes = {
delay: PropTypes.number,
/** Class names for the `BaseToast` component */
className: PropTypes.string,
/** Icon that will be shown in the toast */
icon: PropTypes.func,
/** Class names for the `Icon` component */
iconClassName: PropTypes.string,
};

export default Toast;
Loading