toast.stories.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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={{ margin: '10px' }}>
  73. <Button onClick={() => {
  74. const id = 'toastid'
  75. Toast.error({ id, content: 'error' })
  76. setTimeout(() => Toast.info({ id, content: 'info' }), 2000)
  77. setTimeout(() => Toast.success({ id, content: 'success' }), 4000)
  78. setTimeout(() => Toast.info({ id, content: 'duration 3 -> 0', duration: 0 }), 6000)
  79. }}>
  80. update content by id
  81. </Button>
  82. </div>
  83. <div style={{ width: '300px', height: '300px', background: '#cccccc' }} id="popup-container">
  84. popup-container
  85. </div>
  86. <div>
  87. <Button
  88. type="primary"
  89. onClick={() => {
  90. ToastInContainer.info({ content: 'Toast in popup-container' });
  91. }}
  92. >
  93. Toast in popup-container
  94. </Button>
  95. </div>
  96. <div style={{ margin: '10px' }}>
  97. <Button
  98. type="primary"
  99. onClick={() => {
  100. Toast.error({ ...opts, content: 'I have a very long word testLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWord' });
  101. }}
  102. >
  103. test overflow wrap
  104. </Button>
  105. </div>
  106. </>
  107. );
  108. _Toast.story = {
  109. name: 'toast',
  110. };
  111. const ReachableContext = React.createContext();
  112. /**
  113. * test with cypress
  114. * @returns
  115. */
  116. export const useToastDemo = () => {
  117. const [toast, contextHolder] = Toast.useToast();
  118. const config = {
  119. duration: 0,
  120. title: 'This is a success message',
  121. content: <ReachableContext.Consumer>{name => `ReachableContext: ${name}`}</ReachableContext.Consumer>,
  122. };
  123. return (
  124. <ReachableContext.Provider value="Light">
  125. <div>
  126. <Button
  127. onClick={() => {
  128. toast.success(config);
  129. toast.info(config);
  130. toast.error(config);
  131. toast.warning(config);
  132. const id = toast.open(config);
  133. setTimeout(() => {
  134. toast.close(id);
  135. }, 100);
  136. }}
  137. >
  138. Hook Toast
  139. </Button>
  140. </div>
  141. <div data-cy="context-holder">
  142. {contextHolder}
  143. </div>
  144. </ReachableContext.Provider>
  145. );
  146. };
  147. useToastDemo.storyName = "useToast";