BaseDrawer

  1. import React, {
  2. useState, forwardRef, useImperativeHandle, cloneElement, isValidElement
  3. } from 'react';
  4. import {string, func, object, number, bool, node, oneOfType} from 'prop-types';
  5. import { Drawer, Button } from 'antd';
  6. function DrawerModal(props, ref) {
  7. const {children, element, buttonProps, trigger, onClose, ...rest} = props;
  8. const [visible, setVisible] = useState(false);
  9. useImperativeHandle(ref, () => ({
  10. onShow,
  11. onClose: handleClose,
  12. }))
  13. function onShow() {
  14. setVisible(true)
  15. }
  16. function handleClose() {
  17. if(typeof onClose === 'function') { onClose() }
  18. setVisible(false)
  19. }
  20. function renderAction() {
  21. if(isValidElement(element)) {
  22. return cloneElement(element, {
  23. [trigger]: onShow,
  24. })
  25. }
  26. if(buttonProps) {
  27. return <Button type='primary' {...buttonProps} onClick={onShow} />
  28. }
  29. return null;
  30. }
  31. const attrs = {
  32. width,
  33. title,
  34. closable: false, // 不显示标题右侧的X
  35. // maskClosable: false, // 点击遮罩不关闭抽递
  36. destroyOnClose: true,
  37. bodyStyle: {padding: 16, ...bodyStyle},
  38. };
  39. return (
  40. <>
  41. { renderAction() }
  42. {
  43. visible && (
  44. <Drawer
  45. {...rest}
  46. visible={visible}
  47. onClose={handleClose}
  48. >
  49. {children}
  50. </Drawer>
  51. )
  52. }
  53. </>
  54. );
  55. }
  56. const BaseDrawer = forwardRef(DrawerModal);
  57. BaseDrawer.propTypes = {
  58. onSubmit: func.isRequired,
  59. onClose: func,
  60. title: node,
  61. width: oneOfType([number, string]),
  62. buttonProps: object,
  63. closable: bool,
  64. destroyOnClose: bool,
  65. trigger: string,
  66. };
  67. BaseDrawer.defaultProps = {
  68. width: 560,
  69. buttonProps: { children: '新建' },
  70. closable: false, // 不显示标题右侧的X
  71. destroyOnClose: true, // 关闭销毁子元素
  72. maskClosable: false, // 点击遮罩不关闭抽递,默认 true关闭
  73. trigger: 'onClick',
  74. };
  75. export default BaseDrawer;

DrawerTable

  1. import React, { useRef, useState } from 'react';
  2. import { array } from 'prop-types';
  3. import { Table } from 'antd';
  4. import { BaseDrawer, Progress, FormRender } from '$components';
  5. const columnData = [
  6. { label: '姓名', key: 'username' },
  7. { label: '注册时间', key: 'createdAt' },
  8. ];
  9. const columns = [
  10. {
  11. title: '姓名',
  12. dataIndex: 'username',
  13. },
  14. {
  15. title: '注册时间',
  16. render: (text, data) => data.endTime - data.startTime ? data.endTime - data.startTime : '0',
  17. },
  18. {
  19. title: '更新时间',
  20. dataIndex: 'dur',
  21. },
  22. {
  23. title: 'API',
  24. dataIndex: 'component',
  25. },
  26. ];
  27. TableList.propTypes = {
  28. dataSource: array,
  29. };
  30. function TableList({ dataSource }) {
  31. const modalRef = useRef(null);
  32. const [state, setState] = useState([]);
  33. function rowChange(row) {
  34. const { tags } = row;
  35. const newData = listData.map(it => {
  36. it.value = row[it.key];
  37. return it;
  38. });
  39. if (Array.isArray(tags)) {
  40. const child = tags.map(({ key, value }) => ({ label: key, value }));
  41. newData.push(...child);
  42. }
  43. setState(newData);
  44. modalRef.current.onShow();
  45. }
  46. function onSubmit() {}
  47. return (
  48. <>
  49. <BaseDrawer
  50. ref={modalRef}
  51. title='列表信息'
  52. onSubmit={onSubmit}
  53. >
  54. <FormRender col={24} dataSource={state} />
  55. </BaseDrawer>
  56. <Table
  57. columns={columns},
  58. dataSource={dataSource},
  59. pagination={false},
  60. rowKey='id'
  61. onRow={
  62. record => ({
  63. onClick: () => rowChange(record),
  64. })
  65. }
  66. />
  67. </>
  68. );
  69. }
  70. export default TableList;