notificationListFoundation.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* eslint-disable no-useless-constructor */
  2. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  3. import { NoticeInstance, NoticePosition, NoticeProps } from '../notification/notificationFoundation';
  4. import { strings } from './constants';
  5. // eslint-disable-next-line @typescript-eslint/no-empty-interface
  6. export interface NotificationListProps {
  7. }
  8. export interface NotificationListState {
  9. notices: NoticeInstance[];
  10. removedItems: NoticeInstance[];
  11. }
  12. export interface NotificationListAdapter extends DefaultAdapter<NotificationListProps, NotificationListState> {
  13. updateNotices: (notices: NoticeInstance[], removedItems?: NoticeInstance[]) => void;
  14. getNotices: () => NoticeInstance[];
  15. }
  16. export interface ConfigProps {
  17. top?: number | string;
  18. bottom?: number | string;
  19. left?: number | string;
  20. right?: number | string;
  21. duration?: number;
  22. position?: NoticePosition;
  23. zIndex?: number;
  24. direction?: typeof strings.directions[number];
  25. }
  26. export default class NotificationListFoundation extends BaseFoundation<NotificationListAdapter> {
  27. addNotice(opts: NoticeProps) {
  28. // let notices = this._adapter.getNotices();
  29. const notices = this._adapter.getNotices();
  30. // opts = { ...opts, id };
  31. // if (opts.duration) {
  32. // setTimeout(() => {
  33. // this.removeNotice(opts.id);
  34. // }, opts.duration * 1000);
  35. // }
  36. this._adapter.updateNotices([opts, ...notices]);
  37. // return id;
  38. }
  39. removeNotice(id: string) {
  40. let notices = this._adapter.getNotices();
  41. // let notices = this._adapter.getNotices();
  42. const removedItems: NoticeInstance[] = [];
  43. notices = notices.filter(notice => {
  44. if (notice.id === id) {
  45. removedItems.push(notice);
  46. return false;
  47. }
  48. return true;
  49. });
  50. this._adapter.updateNotices(notices, removedItems); // This must be updated at the same time https://github.com/facebook/react/issues/12312
  51. }
  52. destroyAll() {
  53. const notices = this._adapter.getNotices();
  54. if (notices.length > 0) {
  55. this._adapter.updateNotices([], notices);
  56. }
  57. }
  58. }