刘光辉
10 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
export type RowClickSelectionAction = 'deselect' | 'select';
 
interface SelectionColumn {
  field?: string;
  flag?: string;
  type?: string;
}
 
interface RowClickSelectionContext<TRow, TKey> {
  deselectRow: (rowKey: TKey) => void;
  getRowKey: () => ((row: TRow) => TKey) | string;
  getSelectedRowKeys: () => readonly TKey[];
  isEnabled: () => boolean;
  selectRows: (rows: TRow[]) => void;
}
 
interface RowClickSelectionEvent<TRow> {
  column?: null | SelectionColumn;
  row: TRow;
}
 
export function isCheckboxSelectionColumn(column?: null | SelectionColumn) {
  return column?.type === 'checkbox' || column?.flag === 'checkbox';
}
 
export function isActionColumn(column?: null | SelectionColumn) {
  return column?.field === 'action' || column?.flag === 'action';
}
 
export function resolveRowClickSelectionAction<TKey>(selectedRowKeys: readonly TKey[], clickedRowKey: TKey): RowClickSelectionAction {
  return selectedRowKeys.includes(clickedRowKey) ? 'deselect' : 'select';
}
 
export function getDynamicListSelectionConfig(hasBatchSelection: boolean, isTreeTable: boolean) {
  if (!hasBatchSelection) return {};
  const rowSelection = { type: 'checkbox' as const };
  if (!isTreeTable) return { rowSelection };
  return {
    checkboxConfig: { checkStrictly: true, showHeader: true },
    rowSelection,
  };
}
 
export function createRowClickSelectionHandler<TRow, TKey>(context: RowClickSelectionContext<TRow, TKey>) {
  return ({ column, row }: RowClickSelectionEvent<TRow>) => {
    if (!context.isEnabled() || isCheckboxSelectionColumn(column) || isActionColumn(column)) return;
    const rowKey = context.getRowKey();
    const clickedRowKey = typeof rowKey === 'function' ? rowKey(row) : (row as unknown as Record<string, null | TKey | undefined>)[rowKey];
    if (clickedRowKey === undefined || clickedRowKey === null) return;
 
    const action = resolveRowClickSelectionAction(context.getSelectedRowKeys(), clickedRowKey);
    if (action === 'deselect') {
      context.deselectRow(clickedRowKey);
      return;
    }
    context.selectRows([row]);
  };
}