index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import React, { useState, useLayoutEffect, useRef } from 'react';
  2. import { Select, Button, Modal, Toast, ConfigProvider, Notification } from '@douyinfe/semi-ui';
  3. import en_GB from '@douyinfe/semi-ui/locale/source/en_GB';
  4. import { createPortal } from "react-dom";
  5. const Option = Select.Option;
  6. let ind = 0;
  7. const ReachableContext = React.createContext();
  8. const useCreatePortalInBody = () => {
  9. const wrapperRef = useRef(null);
  10. if (wrapperRef.current === null && typeof document !== 'undefined') {
  11. const div = document.createElement('div');
  12. div.setAttribute('data-body-portal', '');
  13. wrapperRef.current = div;
  14. }
  15. useLayoutEffect(() => {
  16. const wrapper = wrapperRef.current;
  17. if (!wrapper || typeof document === 'undefined') {
  18. return;
  19. }
  20. document.querySelector('.test').appendChild(wrapper);
  21. return () => {
  22. document.querySelector('.test').appendChild(wrapper);
  23. }
  24. }, [])
  25. return (children => wrapperRef.current && createPortal(children, wrapperRef.current));
  26. }
  27. const DialogComponent = props => {
  28. const [visible, setVisible] = useState(false);
  29. const handleOk = e => {
  30. setVisible(false);
  31. };
  32. const handleCancel = e => {
  33. setVisible(false);
  34. };
  35. return (
  36. <React.Fragment>
  37. <Button onClick={() => setVisible(true)}>show dialog</Button>
  38. <Modal title="对话框标题" visible={visible} onOk={handleOk} onCancel={handleCancel} {...props}>
  39. <Button onClick={handleCancel}>hide dialog</Button>
  40. </Modal>
  41. </React.Fragment>
  42. );
  43. };
  44. const UseModalDemo = () => {
  45. const [modal, contextHolder] = Modal.useModal();
  46. const config = { 'title': 'This is a success message', 'content': `${ind}` }
  47. return (
  48. <ConfigProvider
  49. getPopupContainer={() => document.getElementById("modal-container")}
  50. locale={en_GB}
  51. >
  52. <div id="modal-container" />
  53. <Button
  54. onClick={() => {
  55. modal.confirm(config);
  56. }}
  57. >
  58. Confirm
  59. </Button>
  60. <Button
  61. onClick={() => {
  62. modal.warning(config);
  63. }}
  64. >
  65. Warning
  66. </Button>
  67. <Button
  68. onClick={() => {
  69. modal.info(config);
  70. }}
  71. >
  72. Info
  73. </Button>
  74. <Button
  75. onClick={() => {
  76. modal.error(config);
  77. }}
  78. >
  79. Error
  80. </Button>
  81. {/* `contextHolder` should always under the context you want to access */}
  82. {contextHolder}
  83. </ConfigProvider>
  84. );
  85. };
  86. const UseNotiDemo = () => {
  87. const [noti, holder] = Notification.useNotification();
  88. const config = { 'title': 'This is a success message', position: 'bottomLeft', 'content': `${ind}` }
  89. return (
  90. <div>
  91. <div id="noti-container" >{holder}</div>
  92. <Button
  93. onClick={() => {
  94. let id = noti.success(config);
  95. ind++;
  96. setTimeout(() => {
  97. console.log('closing')
  98. noti.close(id)
  99. }, 3000)
  100. }}
  101. >
  102. useNotification demo - left
  103. </Button>
  104. <Button
  105. onClick={() => {
  106. let id = noti.error({ ...config, position: 'topRight' });
  107. ind++;
  108. }}
  109. >
  110. useNotification demo - topRight
  111. </Button>
  112. </div>
  113. );
  114. }
  115. const UseToastDemo = () => {
  116. const [toast, holder] = Toast.useToast();
  117. const createBodyPortal = useCreatePortalInBody();
  118. const config = { 'content': <ReachableContext.Consumer>{name => `ReachableContext: ${name}-${ind}`}</ReachableContext.Consumer>, duration: 0 }
  119. return (
  120. <ReachableContext.Provider value="Light">
  121. <div>
  122. {/* <div id="noti-container" >{holder}</div> */}
  123. <Button
  124. onClick={() => {
  125. let id = toast.success(config);
  126. ind++
  127. setTimeout(() => {
  128. console.log('closing')
  129. toast.close(id)
  130. }, 3000)
  131. }}
  132. >
  133. useToast demo
  134. </Button>
  135. {createBodyPortal(
  136. <div>
  137. {holder}
  138. </div>
  139. )}
  140. </div>
  141. </ReachableContext.Provider>
  142. );
  143. }
  144. export default () => (
  145. <ConfigProvider getPopupContainer={() => document.querySelector('.test')} locale={en_GB}>
  146. <div className="test">
  147. <Select defaultValue='dy' style={{ width: 120 }}>
  148. <Option value='dy'>抖音</Option>
  149. <Option value='hotsoon'>火山</Option>
  150. <Option value='pipixia' disabled>皮皮虾</Option>
  151. <Option value='xigua'>西瓜视频</Option>
  152. </Select>
  153. <Button
  154. onClick={() => Toast.info({
  155. content: 'Hi, Bytedance dance dance',
  156. duration: 3,
  157. })}
  158. >
  159. Display Toast
  160. </Button>
  161. <DialogComponent />
  162. <UseModalDemo />
  163. <Button
  164. type='primary'
  165. onClick={() => api.success({
  166. title: 'Hi, Bytedance',
  167. content: 'ies dance dance dance',
  168. duration: 0,
  169. })}
  170. style={{ margin: 4 }}
  171. >
  172. Success
  173. </Button>
  174. <br />
  175. <UseNotiDemo />
  176. <br />
  177. <UseToastDemo />
  178. </div>
  179. </ConfigProvider>
  180. );