hooks.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {deleteMenu} from '@apis';
  2. import {LDColumnsType, LDTableButton} from '@components';
  3. import {useTableDeleteEvent, useTableModalEvent} from '@hooks';
  4. import {MenuListData} from '@models';
  5. import {TABLE_CELL_WIDTH} from '@utils';
  6. const tableColumns: LDColumnsType<MenuListData>[] = [
  7. {
  8. title: '菜单名称',
  9. dataIndex: 'name',
  10. },
  11. {
  12. title: '菜单路径',
  13. dataIndex: 'url',
  14. },
  15. {
  16. title: '菜单排序',
  17. dataIndex: 'orderBy',
  18. width: TABLE_CELL_WIDTH.small,
  19. },
  20. {
  21. title: '最后修改人',
  22. dataIndex: 'modifyUser',
  23. width: TABLE_CELL_WIDTH.normal,
  24. },
  25. {
  26. title: '最后修改时间',
  27. dataIndex: 'modifyTime',
  28. width: TABLE_CELL_WIDTH.date,
  29. },
  30. ];
  31. export function useColumns(refetch: () => void) {
  32. const [deleteId, onDelete] = useTableDeleteEvent(
  33. deleteMenu,
  34. refetch,
  35. {label: '菜单'},
  36. );
  37. const [{visible, editId}, {onAdd, onEdit, onClose}] = useTableModalEvent();
  38. const columns: LDColumnsType<MenuListData>[] = [
  39. ...tableColumns,
  40. {
  41. dataIndex: 'id',
  42. title: '操作',
  43. width: TABLE_CELL_WIDTH.doubleBtn,
  44. fixed: 'right',
  45. render({id, name}) {
  46. const loading = deleteId === id;
  47. return (
  48. <>
  49. <LDTableButton
  50. disabled={loading}
  51. onClick={onEdit(id)}
  52. >
  53. 修改
  54. </LDTableButton>
  55. <LDTableButton
  56. danger
  57. loading={loading}
  58. onClick={onDelete(id, name)}
  59. >
  60. 删除
  61. </LDTableButton>
  62. </>
  63. );
  64. },
  65. },
  66. ];
  67. return [
  68. {columns, editId, visible},
  69. {onClose, onAdd},
  70. ] as const;
  71. }