toast.stories.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. </>
  97. );
  98. _Toast.story = {
  99. name: 'toast',
  100. };
  101. const ReachableContext = React.createContext();
  102. /**
  103. * test with cypress
  104. * @returns
  105. */
  106. export const useToastDemo = () => {
  107. const [toast, contextHolder] = Toast.useToast();
  108. const config = {
  109. duration: 0,
  110. title: 'This is a success message',
  111. content: <ReachableContext.Consumer>{name => `ReachableContext: ${name}`}</ReachableContext.Consumer>,
  112. };
  113. return (
  114. <ReachableContext.Provider value="Light">
  115. <div>
  116. <Button
  117. onClick={() => {
  118. toast.success(config);
  119. toast.info(config);
  120. toast.error(config);
  121. toast.warning(config);
  122. const id = toast.open(config);
  123. setTimeout(() => {
  124. toast.close(id);
  125. }, 100);
  126. }}
  127. >
  128. Hook Toast
  129. </Button>
  130. </div>
  131. <div data-cy="context-holder">
  132. {contextHolder}
  133. </div>
  134. </ReachableContext.Provider>
  135. );
  136. };
  137. useToastDemo.storyName = "useToast";