1
0

AsyncTools.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. export function asyncGet(asyncDom, props, cb, confirm = 'onConfirm', cancel = 'onCancel') {
  4. const NODE = document.createElement('div');
  5. document.body.appendChild(NODE);
  6. function remove(node) {
  7. setTimeout(() => {
  8. ReactDOM.unmountComponentAtNode(node);
  9. document.body.removeChild(node);
  10. }, 100);
  11. }
  12. const params = {
  13. [confirm]: data => {
  14. return cb(data);
  15. },
  16. [cancel]: () => {
  17. remove(NODE);
  18. },
  19. };
  20. return asyncDom().then(({ default: C }) => {
  21. return new Promise(resolve => {
  22. ReactDOM.render(<C ref={ref => ref && resolve(ref)} {...props} {...params} />, NODE);
  23. });
  24. });
  25. }
  26. export function asyncForm(title, itemList, data, cb) {
  27. return asyncGet(() => import('../layouts/FormLayout'), { title, itemList, data, modal: true }, cb);
  28. }
  29. export function asyncDrawerForm(title, itemList, data, cb, drawer = 'right') {
  30. return asyncGet(() => import('../layouts/FormLayout'), { title, itemList, data, drawer }, cb);
  31. }
  32. function getAsyncAntd() {
  33. return import('../components/AsyncAntd');
  34. }
  35. export function asyncSMessage(title, type = 'success') {
  36. return getAsyncAntd().then(({ Message }) => {
  37. switch (type) {
  38. case 'warn':
  39. case 'warning':
  40. Message.warning(title);
  41. break;
  42. case 'error':
  43. Message.error(title);
  44. break;
  45. case 'success':
  46. default:
  47. Message.success(title);
  48. break;
  49. }
  50. });
  51. }
  52. export function asyncModalConfirm(props, cb) {
  53. const params = {
  54. onOk: () => {
  55. const result = cb();
  56. if (result) {
  57. result.catch((err) => {
  58. asyncSMessage(err.message, 'error');
  59. });
  60. }
  61. },
  62. destroyOnClose: true,
  63. };
  64. getAsyncAntd().then(({ Modal }) => {
  65. Modal.confirm(Object.assign(params, props));
  66. });
  67. }
  68. export function asyncConfirm(title, content, cb) {
  69. return asyncModalConfirm({ title, content, maskClosable: true }, cb);
  70. }
  71. export function asyncDelConfirm(title, content, cb) {
  72. return asyncModalConfirm({ title, content, okType: 'danger', maskClosable: true }, cb);
  73. }