Browse Source

refactor: table meta中添加是否允许排序

xyh 2 năm trước cách đây
mục cha
commit
6c8d02895e

+ 7 - 3
packages/app/src/components/table/header-th/index.tsx

@@ -10,18 +10,22 @@ type Props = {
 };
 
 const HeaderTh: FC<Props> = function ({header, useDiv}) {
+  const {fixed, disabledSort} = header.column.columnDef.meta as {
+    fixed: boolean;
+    disabledSort: boolean;
+  };
+
   const {setNodeRef, attributes, listeners} = useSortable({
     id: header.id,
     data: {header},
-    disabled: header.id === 'id',
+    disabled: disabledSort,
   });
 
-  const {fixed} = header.column.columnDef.meta as {fixed: boolean};
   const isResizing = header.column.getIsResizing();
 
   const style: CSSProperties = {
     width: `${header.getSize()}px !important`,
-    cursor: isResizing ? 'col-resize' : fixed ? 'auto' : 'move',
+    cursor: isResizing ? 'col-resize' : disabledSort ? 'auto' : 'move',
     boxShadow: useDiv
       ? '0.5px 0.6px 12.3px rgba(0, 0, 0, 0.059),4px 5px 80px rgba(0, 0, 0, 0.1)'
       : '',

+ 13 - 3
packages/app/src/components/table/hooks.ts

@@ -19,7 +19,11 @@ function parseColumn<T>(columns: ColumnType<T>[]) {
 
   const tableColumns = columns.map(function (val) {
     const {dataIndex, title, render, align, fixed} = val;
-    const meta = {align, fixed: Boolean(fixed)};
+    const meta = {
+      align,
+      fixed: Boolean(fixed),
+      disabledSort: Boolean(fixed) || dataIndex === 'no',
+    };
 
     if (render) {
       return hepler.display({
@@ -51,7 +55,11 @@ function parseColumn<T>(columns: ColumnType<T>[]) {
       cell({row}) {
         return row.index;
       },
-      meta: {align: 'center'},
+      meta: {
+        align: 'center',
+        fixed: false,
+        disabledSort: true,
+      },
     }),
   );
 
@@ -142,8 +150,10 @@ export function useTable<T extends Record<string, any>>(
 
     const {id: activeId} = active,
       {id: overId} = over;
+    const {disabledSort} = (over.data.current as any).header.column.columnDef
+      .meta;
 
-    if (overId === 'id') return;
+    if (disabledSort) return;
 
     if (activeId === overId) return;
     const {setColumnOrder} = table;