Skip to content

Commit

Permalink
feat: 自由设置不可拖动列,支持最后一列可拖动
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 1, 2021
1 parent 8a54977 commit 34a1189
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ yarn add use-antd-resizable-header

- **columns 为常量时,提到组件外,或使用 `React.useMemo`, `React.Ref` 包裹常量**
- **默认拖动颜色为`#000`,可通过`global`或设置css变量`--atrh-color`设置颜色**
- **最后一列不能拖动,[请保持最后一列的自适应](https://ant-design.gitee.io/components/table-cn/#components-table-demo-fixed-columns),若最后一列传入宽度,会把传入的宽度作为最小宽度(默认 120)**
- **行合并暂未找到合适方案**
- **至少一列不能拖动(width不设置即可),[请保持最后至少一列的自适应](https://ant-design.gitee.io/components/table-cn/#components-table-demo-fixed-columns)**

## Example

Expand Down
12 changes: 5 additions & 7 deletions src/ResizableHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { ThHTMLAttributes } from 'react';
import { Resizable, ResizeCallbackData } from 'react-resizable';
import classnames from 'classnames';

Expand All @@ -7,13 +7,12 @@ import './index.css';
type ComponentProp = {
onResize: (width: number) => void;
onMount: (width: number) => void;
isLast: boolean;
triggerRender: number;
width: number;
minWidth: number;
maxWidth: number;
titleTip?: string;
} & Record<string, any>;
} & ThHTMLAttributes<HTMLTableCellElement>;

const ResizableHeader: React.FC<ComponentProp> = (props) => {
const {
Expand All @@ -22,7 +21,6 @@ const ResizableHeader: React.FC<ComponentProp> = (props) => {
maxWidth,
onResize,
onMount,
isLast,
triggerRender,
className,
style,
Expand All @@ -39,19 +37,19 @@ const ResizableHeader: React.FC<ComponentProp> = (props) => {
const [resizeWidth, setResizeWidth] = React.useState<number>(0);

React.useEffect(() => {
if (width && !isLast) {
if (width) {
setResizeWidth(width);
onMount?.(width);
}
}, [triggerRender]);

React.useEffect(() => {
if (width && !isLast) {
if (width) {
setResizeWidth(width);
}
}, [width]);

if (!width || Number.isNaN(Number(width)) || isLast) {
if (!width || Number.isNaN(Number(width))) {
return (
<th
{...rest}
Expand Down
9 changes: 2 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ function useTableResizableHeader<ColumnType extends Record<string, any>>(
(list: ColumnType[]) => {
const c = list
?.filter((item) => !isEmpty(item))
.map((col, index) => {
const isLast = index === list.length - 1;
.map((col) => {
return {
...col,
children: col?.children?.length ? getColumns(col.children) : undefined,
Expand All @@ -120,13 +119,9 @@ function useTableResizableHeader<ColumnType extends Record<string, any>>(
minWidth: minConstraints,
maxWidth: maxConstraints,
triggerRender,
isLast,
};
},
width:
isLast && !col?.fixed
? undefined
: widthCache.current?.get(col[getKey])?.width || col?.width,
width: widthCache.current?.get(col[getKey])?.width || col?.width,
[getKey]: col[getKey] || uniqueId,
};
}) as ColumnType[];
Expand Down

0 comments on commit 34a1189

Please sign in to comment.