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

Allow toasts in EuiGlobalToastList to override toastLifeTimeMs #1720

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Allow toasts in `EuiGlobalToastList` to override `toastLifeTimeMs` ([#1720](https://github.com/elastic/eui/pull/1720))

**Bug fixes**

- Fixed `EuiSuperDatePicker` time selection jumping on focus ([#1704](https://github.com/elastic/eui/pull/1704))
Expand Down
11 changes: 11 additions & 0 deletions src-docs/src/views/toast/toast_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
} from 'react';

import {
EuiCode,
EuiGlobalToastList,
EuiLink,
} from '../../../../src/components';
Expand Down Expand Up @@ -97,6 +98,16 @@ export default class extends Component {
Sorry. We’ll try not to let it happen it again.
</p>
),
}, {
title: 'Long toast',
color: 'warning',
iconType: 'clock',
toastLifeTimeMs: 15000,
text: (
<p>
This toast overrides the default <EuiCode>toastLifeTimeMs</EuiCode> value and will be around for 15 seconds.
</p>
),
}];

return {
Expand Down
13 changes: 11 additions & 2 deletions src/components/toast/global_toast_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
import classNames from 'classnames';

import { Timer } from '../../services/time';
import { ICON_TYPES } from '../icon';
import { EuiGlobalToastListItem } from './global_toast_list_item';
import { EuiToast } from './toast';

Expand Down Expand Up @@ -32,7 +33,14 @@ export class EuiGlobalToastList extends Component {

static propTypes = {
className: PropTypes.string,
toasts: PropTypes.array,
toasts: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
title: PropTypes.string,
text: PropTypes.node,
color: PropTypes.string,
iconType: PropTypes.oneOf(ICON_TYPES),
toastLifeTimeMs: PropTypes.number,
}).isRequired),
dismissToast: PropTypes.func.isRequired,
toastLifeTimeMs: PropTypes.number.isRequired,
};
Expand Down Expand Up @@ -111,7 +119,7 @@ export class EuiGlobalToastList extends Component {
scheduleToastForDismissal = (toast) => {
// Start fading the toast out once its lifetime elapses.
this.toastIdToTimerMap[toast.id] =
new Timer(this.dismissToast.bind(this, toast), this.props.toastLifeTimeMs);
new Timer(this.dismissToast.bind(this, toast), toast.toastLifeTimeMs != null ? toast.toastLifeTimeMs : this.props.toastLifeTimeMs);
};

dismissToast = (toast) => {
Expand Down Expand Up @@ -201,6 +209,7 @@ export class EuiGlobalToastList extends Component {
const renderedToasts = toasts.map(toast => {
const {
text,
toastLifeTimeMs, // eslint-disable-line no-unused-vars
...rest
} = toast;

Expand Down
26 changes: 26 additions & 0 deletions src/components/toast/global_toast_list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,32 @@ describe('EuiGlobalToastList', () => {
done();
}, TOAST_LIFE_TIME_MS + TOAST_FADE_OUT_MS + 10);
});

test('toastLifeTimeMs is overrideable by individidual toasts', done => {
const TOAST_LIFE_TIME_MS = 10;
const TOAST_LIFE_TIME_MS_OVERRIDE = 100;
const dismissToastSpy = sinon.spy();
mount(
<EuiGlobalToastList
toasts={[{
'data-test-subj': 'b',
id: 'b',
toastLifeTimeMs: TOAST_LIFE_TIME_MS_OVERRIDE
}]}
dismissToast={dismissToastSpy}
toastLifeTimeMs={TOAST_LIFE_TIME_MS}
/>
);

// The callback is invoked once the toast fades from view.
setTimeout(() => {
expect(dismissToastSpy.called).toBe(false);
}, TOAST_LIFE_TIME_MS + TOAST_FADE_OUT_MS + 10);
setTimeout(() => {
expect(dismissToastSpy.called).toBe(true);
done();
}, TOAST_LIFE_TIME_MS_OVERRIDE + TOAST_FADE_OUT_MS + 10);
});
});
});
});