useCustomRow.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import type { ComputedRef } from 'vue';
  2. import type { BasicTableProps } from '../types/table';
  3. import { unref } from 'vue';
  4. import { ROW_KEY } from '../const';
  5. import { isString, isFunction } from '/@/utils/is';
  6. interface Options {
  7. setSelectedRowKeys: (keys: string[]) => void;
  8. getSelectRowKeys: () => string[];
  9. clearSelectedRowKeys: () => void;
  10. emit: EmitType;
  11. getAutoCreateKey: ComputedRef<boolean | undefined>;
  12. }
  13. function getKey(
  14. record: Recordable,
  15. rowKey: string | ((record: Record<string, any>) => string) | undefined,
  16. autoCreateKey?: boolean,
  17. ) {
  18. if (!rowKey || autoCreateKey) {
  19. return record[ROW_KEY];
  20. }
  21. if (isString(rowKey)) {
  22. return record[rowKey];
  23. }
  24. if (isFunction(rowKey)) {
  25. return record[rowKey(record)];
  26. }
  27. return null;
  28. }
  29. export function useCustomRow(
  30. propsRef: ComputedRef<BasicTableProps>,
  31. { setSelectedRowKeys, getSelectRowKeys, getAutoCreateKey, clearSelectedRowKeys, emit }: Options,
  32. ) {
  33. const customRow = (record: Recordable, index: number) => {
  34. return {
  35. onClick: (e: Event) => {
  36. e?.stopPropagation();
  37. function handleClick() {
  38. const { rowSelection, rowKey, clickToRowSelect } = unref(propsRef);
  39. if (!rowSelection || !clickToRowSelect) return;
  40. const keys = getSelectRowKeys() || [];
  41. const key = getKey(record, rowKey, unref(getAutoCreateKey));
  42. if (!key) return;
  43. const isCheckbox = rowSelection.type === 'checkbox';
  44. if (isCheckbox) {
  45. // 找到tr
  46. const tr: HTMLElement = (e as MouseEvent)
  47. .composedPath?.()
  48. .find((dom: HTMLElement) => dom.tagName === 'TR') as HTMLElement;
  49. if (!tr) return;
  50. // 找到Checkbox,检查是否为disabled
  51. const checkBox = tr.querySelector('input[type=checkbox]');
  52. if (!checkBox || checkBox.hasAttribute('disabled')) return;
  53. if (!keys.includes(key)) {
  54. setSelectedRowKeys([...keys, key]);
  55. return;
  56. }
  57. const keyIndex = keys.findIndex((item) => item === key);
  58. keys.splice(keyIndex, 1);
  59. setSelectedRowKeys(keys);
  60. return;
  61. }
  62. const isRadio = rowSelection.type === 'radio';
  63. if (isRadio) {
  64. if (!keys.includes(key)) {
  65. if (keys.length) {
  66. clearSelectedRowKeys();
  67. }
  68. setSelectedRowKeys([key]);
  69. return;
  70. }
  71. clearSelectedRowKeys();
  72. }
  73. }
  74. handleClick();
  75. emit('row-click', record, index, e);
  76. },
  77. onDblclick: (event: Event) => {
  78. emit('row-dbClick', record, index, event);
  79. },
  80. onContextmenu: (event: Event) => {
  81. emit('row-contextmenu', record, index, event);
  82. },
  83. onMouseenter: (event: Event) => {
  84. emit('row-mouseenter', record, index, event);
  85. },
  86. onMouseleave: (event: Event) => {
  87. emit('row-mouseleave', record, index, event);
  88. },
  89. };
  90. };
  91. return {
  92. customRow,
  93. };
  94. }