toast.stories.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React from 'react';
  2. import { Toast, ToastFactory } from '../../index';
  3. import Button from '@douyinfe/semi-ui/button/index';
  4. export default {
  5. title: 'Toast',
  6. parameters: {
  7. chromatic: { disableSnapshot: true },
  8. },
  9. }
  10. const ToastInContainer = ToastFactory.create({
  11. getPopupContainer: () => document.getElementById('popup-container'),
  12. });
  13. function getContent() {
  14. let n = Math.random() * 10;
  15. return Array.from({ length: n }, (v, i) => 'dance').join(' ');
  16. }
  17. let opts = {
  18. title: 'Title ies',
  19. content: 'Hi,Bytedance dance dance',
  20. duration: 3000,
  21. };
  22. let multiOpts = Object.assign({}, opts, {
  23. content: 'Hi,Bytedance dance dance, dance dance,dance dance,dance dance',
  24. });
  25. export const _Toast = () => (
  26. <>
  27. <div style={{ margin: '10px' }}>
  28. <Button type="primary" onClick={() => Toast.create({ ...opts })}>
  29. default
  30. </Button>
  31. </div>
  32. <div style={{ margin: '10px' }}>
  33. <Button type="warning" onClick={() => Toast.warning({ ...opts })}>
  34. warning
  35. </Button>
  36. </div>
  37. <div style={{ margin: '10px' }}>
  38. <Button type="warning" onClick={() => Toast.success('here')}>
  39. success
  40. </Button>
  41. </div>
  42. <div style={{ margin: '10px' }}>
  43. <Button type="warning" onClick={() => Toast.error({ ...opts })}>
  44. error
  45. </Button>
  46. </div>
  47. <div style={{ margin: '10px' }}>
  48. <Button type="default" onClick={() => Toast.info({ ...opts, duration: 0 })}>
  49. not auto close
  50. </Button>
  51. </div>
  52. <div style={{ margin: '10px' }}>
  53. <Button type="default" onClick={() => Toast.destroyAll()}>
  54. destroyAll
  55. </Button>
  56. </div>
  57. <div style={{ margin: '10px' }}>
  58. <Button type="default" onClick={() => Toast.info({ ...multiOpts, duration: 0 })}>
  59. not auto close with muti-line content
  60. </Button>
  61. </div>
  62. <div style={{ margin: '10px' }}>
  63. <Button
  64. type="danger"
  65. onClick={() => {
  66. Toast.error({ ...opts, content: 'i am content' });
  67. }}
  68. >
  69. After 3s
  70. </Button>
  71. </div>
  72. <div style={{ width: '300px', height: '300px', background: '#cccccc' }} id="popup-container">
  73. popup-container
  74. </div>
  75. <div>
  76. <Button
  77. type="primary"
  78. onClick={() => {
  79. ToastInContainer.info({ content: 'Toast in popup-container' });
  80. }}
  81. >
  82. Toast in popup-container
  83. </Button>
  84. </div>
  85. </>
  86. );
  87. _Toast.story = {
  88. name: 'toast',
  89. };
  90. const ReachableContext = React.createContext();
  91. /**
  92. * test with cypress
  93. * @returns
  94. */
  95. export const useToastDemo = () => {
  96. const [toast, contextHolder] = Toast.useToast();
  97. const config = {
  98. duration: 0,
  99. title: 'This is a success message',
  100. content: <ReachableContext.Consumer>{name => `ReachableContext: ${name}`}</ReachableContext.Consumer>,
  101. };
  102. return (
  103. <ReachableContext.Provider value="Light">
  104. <div>
  105. <Button
  106. onClick={() => {
  107. toast.success(config);
  108. toast.info(config);
  109. toast.error(config);
  110. toast.warning(config);
  111. const id = toast.open(config);
  112. setTimeout(() => {
  113. toast.close(id);
  114. }, 100);
  115. }}
  116. >
  117. Hook Toast
  118. </Button>
  119. </div>
  120. <div data-cy="context-holder">
  121. {contextHolder}
  122. </div>
  123. </ReachableContext.Provider>
  124. );
  125. };
  126. useToastDemo.storyName = "useToast";