modal.stories.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import React, { useState } from 'react';
  2. import en_GB from '../../locale/source/en_GB';
  3. import { Select, Modal, Button, Tooltip, Popover, ConfigProvider, Tag, Space } from '../../index';
  4. import CollapsibleInModal from './CollapsibleInModal';
  5. import DynamicContextDemo from './DynamicContext';
  6. export default {
  7. title: 'Modal',
  8. parameters: {
  9. chromatic: { disableSnapshot: true },
  10. },
  11. }
  12. export {
  13. CollapsibleInModal,
  14. DynamicContextDemo
  15. }
  16. const Option = Select.Option;
  17. const DialogComponent = props => {
  18. const [visible, setVisible] = useState(false);
  19. const handleOk = e => {
  20. setVisible(false);
  21. };
  22. const handleCancel = e => {
  23. setVisible(false);
  24. };
  25. return (
  26. <React.Fragment>
  27. <Button onClick={() => setVisible(true)}>show dialog</Button>
  28. <Modal
  29. title="对话框标题"
  30. visible={visible}
  31. onOk={handleOk}
  32. onCancel={handleCancel}
  33. {...props}
  34. >
  35. <input autoFocus />
  36. <p>basic modal</p>
  37. <Button onClick={handleCancel}>hide dialog</Button>
  38. </Modal>
  39. </React.Fragment>
  40. );
  41. };
  42. export const Default = () => <DialogComponent />;
  43. Default.story = {
  44. name: 'default',
  45. };
  46. export const ClickMaskClosableFalse = () => <DialogComponent maskClosable={false} />;
  47. ClickMaskClosableFalse.story = {
  48. name: 'click mask closable false',
  49. };
  50. function useLoading() {
  51. const [loading, setLoading] = useState(false);
  52. function startQuery() {
  53. setLoading(true);
  54. }
  55. return [loading, { startQuery }];
  56. }
  57. function DialogComponentWithLoading() {
  58. const [loading, actions] = useLoading();
  59. return <DialogComponent confirmLoading={loading} onOk={actions.startQuery} />;
  60. }
  61. export const WithLoadingTodo = () => <DialogComponentWithLoading />;
  62. WithLoadingTodo.story = {
  63. name: 'with loading todo',
  64. };
  65. function success() {
  66. Modal.success('bla bla bla...');
  67. }
  68. function info() {
  69. Modal.info('info');
  70. }
  71. function error() {
  72. Modal.error({ title: 'Unfortunately, there is an error', content: 'bla bla bla...' });
  73. }
  74. function warning() {
  75. Modal.warning({ title: 'Warning: be cautious ahead', content: 'bla bla bla...' });
  76. }
  77. function confirm() {
  78. Modal.confirm({ title: 'Are you sure ?', content: 'bla bla bla...' });
  79. }
  80. export const ConfirmModal = () => (
  81. <div>
  82. <Button onClick={info}>Info</Button>
  83. <Button onClick={success}>Success</Button>
  84. <Button onClick={error}>Error</Button>
  85. <Button onClick={warning}>Warning</Button>
  86. <Button onClick={confirm}>Confirm</Button>
  87. </div>
  88. );
  89. ConfirmModal.story = {
  90. name: 'confirm modal',
  91. };
  92. const Test = () => {
  93. let modal;
  94. const showModal = () => {
  95. modal = Modal.info({
  96. title: '修改的标题',
  97. content: (
  98. <>
  99. <Button onClick={() => modal && modal.destroy()}>close</Button>
  100. <Button onClick={() => modal && modal.update({ title: 'lalala updating' })}>
  101. update
  102. </Button>
  103. </>
  104. ),
  105. });
  106. };
  107. return <Button onClick={() => showModal()}>Info</Button>;
  108. };
  109. export const ModalDestroy = () => <Test />;
  110. ModalDestroy.story = {
  111. name: 'modal.destroy',
  112. };
  113. const ScrollComponent = props => {
  114. const [visible, setVisible] = useState(true);
  115. const handleOk = e => {
  116. setVisible(false);
  117. };
  118. const handleCancel = e => {
  119. setVisible(false);
  120. };
  121. return (
  122. <div style={{ paddingTop: 900, paddingBotttom: 800 }}>
  123. <Button onClick={() => setVisible(true)}>show dialog</Button>
  124. <Modal
  125. id="modal-test"
  126. title="对话框标题"
  127. visible={visible}
  128. onOk={handleOk}
  129. onCancel={handleCancel}
  130. {...props}
  131. >
  132. <Select>
  133. <Option value={1}>opt1</Option>
  134. <Option value={2}>opt2</Option>
  135. <Option value={3}>opt3</Option>
  136. <Option value={4}>opt4</Option>
  137. <Option value={5}>opt5</Option>
  138. <Option value={6}>opt6</Option>
  139. </Select>
  140. <Tooltip content="fefefefeef">test tooltip in modal</Tooltip>
  141. <Button onClick={handleCancel}>hide dialog</Button>
  142. <Popover content={'1,2,3'}>
  143. <Button>hover</Button>
  144. </Popover>
  145. </Modal>
  146. <Select>
  147. <Option value={1}>opt1</Option>
  148. <Option value={2}>opt2</Option>
  149. <Option value={3}>opt3</Option>
  150. <Option value={4}>opt4</Option>
  151. <Option value={5}>opt5</Option>
  152. <Option value={6}>opt6</Option>
  153. </Select>
  154. </div>
  155. );
  156. };
  157. export const Scroll = () => <ScrollComponent />;
  158. Scroll.story = {
  159. name: 'scroll',
  160. };
  161. const Popup = () => {
  162. const [visible, setVisible] = useState(false);
  163. const getContainer = () => {
  164. return document.querySelector('.modal-container');
  165. };
  166. return (
  167. <div
  168. style={{
  169. height: 320,
  170. overflow: 'hidden',
  171. position: 'relative',
  172. border: '1px solid var(--semi-color-border)',
  173. borderRadius: 2,
  174. padding: 24,
  175. textAlign: 'center',
  176. background: 'var(--semi-color-fill-0)',
  177. }}
  178. className="modal-container"
  179. >
  180. <span>Render in this</span>
  181. <br />
  182. <br />
  183. <Button onClick={() => setVisible(true)}>Open Modal</Button>
  184. <Modal
  185. title="渲染在指定容器内部"
  186. visible={visible}
  187. onCancel={() => setVisible(false)}
  188. width={200}
  189. // centered
  190. // style={{ top: '10px' }}
  191. getPopupContainer={getContainer}
  192. >
  193. <p>This is the content of a basic sidesheet.</p>
  194. <p>Here is more content...</p>
  195. </Modal>
  196. </div>
  197. );
  198. };
  199. export const RenderInCustomContainer = () => <Popup />;
  200. RenderInCustomContainer.story = {
  201. name: 'render in custom container',
  202. };
  203. export const KeepDom = () => <DialogComponent keepDOM />;
  204. KeepDom.story = {
  205. name: 'keepDOM',
  206. };
  207. export const KeepDomNotLazy = () => <DialogComponent keepDOM lazyRender={false} />;
  208. KeepDomNotLazy.story = {
  209. name: 'keepDOM && not lazy',
  210. };
  211. export const UseModalDemo = () => {
  212. const [modal, contextHolder] = Modal.useModal();
  213. const config = { 'title': 'old title', 'content': 'old content' };
  214. return (
  215. <ConfigProvider locale={en_GB}>
  216. <div>
  217. <Button
  218. onClick={() => {
  219. const currentModal = modal.confirm(config);
  220. setTimeout(() => {
  221. currentModal.update({ title: "new title", content: "new content" });
  222. }, 1000);
  223. }}
  224. >
  225. Confirm Modal
  226. </Button>
  227. </div>
  228. {contextHolder}
  229. </ConfigProvider>
  230. );
  231. };
  232. UseModalDemo.storyName = "useModal";
  233. export const UseModalDestroy = () => {
  234. const [modal, contextHolder] = Modal.useModal();
  235. const config = { 'title': 'old title', 'content': 'old content' };
  236. return (
  237. <ConfigProvider locale={en_GB}>
  238. <div>
  239. <Button
  240. onClick={() => {
  241. const currentModal = modal.confirm(config);
  242. setTimeout(() => {
  243. currentModal.destroy();
  244. }, 1000);
  245. }}
  246. >
  247. Confirm Modal
  248. </Button>
  249. </div>
  250. {contextHolder}
  251. </ConfigProvider>
  252. );
  253. };
  254. UseModalDestroy.storyName = "useModal destroy";
  255. export const UseModalAfterClose = () => {
  256. const [modal, contextHolder] = Modal.useModal();
  257. const [closed, setClosed] = React.useState(false);
  258. const [leave, setLeave] = React.useState(false);
  259. const config = {
  260. title: 'old title',
  261. content: 'old content',
  262. afterClose: () => {
  263. setClosed(true);
  264. },
  265. motion: {
  266. didLeave: () => {
  267. console.log('didLeave');
  268. setLeave(true);
  269. }
  270. }
  271. };
  272. return (
  273. <ConfigProvider locale={en_GB}>
  274. <Space>
  275. <Button
  276. onClick={() => {
  277. const currentModal = modal.confirm(config);
  278. setTimeout(() => {
  279. currentModal.destroy();
  280. }, 0);
  281. }}
  282. >
  283. Confirm Modal
  284. </Button>
  285. <Tag>{`closed: ${closed}`}</Tag>
  286. {/* <Tag>{`motion leave: ${leave}`}</Tag> */}
  287. </Space>
  288. {contextHolder}
  289. </ConfigProvider>
  290. );
  291. };
  292. UseModalAfterClose.storyName = "useModal afterClose";