【RaETable】🚀🚀🚀告别Form,RaETable表格列宽度支持拖动调整了,附带原理说明

raETablereact antd Easy Table 的缩写。旨在让开发者在react中使用 antdTable时更 easy。

Github: github.com/mmdctjj/rae...

npm: www.npmjs.com/package/rae...

文档:mmdctjj.github.io/raetable

往期回顾

🚀 变更内容

最近工作的需求变动,antd表格的宽度需要支持拖动调整。

调研了一圈,呼声最高的方案是使用react-resizable库实现。

实现之后发现这个功能特别适合 RaETable 于是,我也给这个组件增加了这个功能。不过,不用担心,这个功能默认是关闭的,当你设置resizetrue 时才会开启该功能。

效果可以直接看这个示例

录制的工具限制,只有15帧动画,感兴趣的话可以到官网实际操作下。

mmdctjj.github.io/raetable/co...

🚀 实现原理

实现原理:

  • react-resizable提供了Resizable组件用于包括th元素,成为可以拖拽的元素。
  • 采用antd``Table组件提供的components属性,覆盖表头元素
  • 每次拖拽之后将最新的列宽度值设置为列的真实宽度值

github.com/react-compo...

js 复制代码
export interface TableComponents<RecordType> {
  table?: CustomizeComponent;
  header?: {
    wrapper?: CustomizeComponent;
    row?: CustomizeComponent;
    cell?: CustomizeComponent;
  };
  body?:
    | CustomizeScrollBody<RecordType>
    | {
        wrapper?: CustomizeComponent;
        row?: CustomizeComponent;
        cell?: CustomizeComponent;
      };
}

🚀 实现过程

💎 重写表头组件

js 复制代码
import { Resizable, ResizeCallbackData } from 'react-resizable';
import 'react-resizable/css/styles.css';

const ResizableTitle = (props: {
  [x: string]: any;
  onResize: ((e: React.SyntheticEvent, data: ResizeCallbackData) => any) | undefined;
  width: number;
}) => {
  const { onResize, width, ...restProps } = props;

  if (!width) {
    return <th {...restProps} />;
  }

  return (
    <Resizable width={width} height={0} onResize={onResize}>
      <th {...restProps} />
    </Resizable>
  );
};

💎 覆盖表头组件

js 复制代码
const ResizeTable = (
  props: JSX.IntrinsicAttributes &
    TableProps<any> & { children?: React.ReactNode } & {
      ref?: React.Ref<HTMLDivElement> | undefined;
    },
) => {
  const [columns, setColumns] = useState(props.columns ?? []);

  return (
    <Table
      {...props}
      columns={columns}
      components={{
        header: {
          cell: ResizableTitle,
        },
      }}
      scroll={{ x: 'max-content' }} // 添加滚动条以适应自适应宽度
    />
  );
};

💎 动态的更新表格列宽度

js 复制代码
  const handleResize =
    (index: number) =>
    (event: any, { size }: any) => {
      setColumns((prevColumns) => {
        const nextColumns = [...prevColumns];
        nextColumns[index] = {
          ...nextColumns[index],
          width: size.width,
        };
        return nextColumns;
      });
    };

  const resizableColumns = columns?.map((col: any, index: number) => ({
    ...col,
    onHeaderCell: (column: { width: number }) => ({
      width: column.width,
      onResize: handleResize(index),
    }),
  }));

完整代码如下

js 复制代码
import { Table, TableProps } from 'antd';
import React, { useState } from 'react';
import { Resizable, ResizeCallbackData } from 'react-resizable';
import 'react-resizable/css/styles.css';

const ResizableTitle = (props: {
  [x: string]: any;
  onResize: ((e: React.SyntheticEvent, data: ResizeCallbackData) => any) | undefined;
  width: number;
}) => {
  const { onResize, width, ...restProps } = props;

  if (!width) {
    return <th {...restProps} />;
  }

  return (
    <Resizable width={width} height={0} onResize={onResize}>
      <th {...restProps} />
    </Resizable>
  );
};

const ResizeTable = (
  props: JSX.IntrinsicAttributes &
    TableProps<any> & { children?: React.ReactNode } & {
      ref?: React.Ref<HTMLDivElement> | undefined;
    },
) => {
  const [columns, setColumns] = useState(props.columns ?? []);

  const handleResize =
    (index: number) =>
    (event: any, { size }: any) => {
      setColumns((prevColumns) => {
        const nextColumns = [...prevColumns];
        nextColumns[index] = {
          ...nextColumns[index],
          width: size.width,
        };
        return nextColumns;
      });
    };

  const resizableColumns = columns?.map((col: any, index: number) => ({
    ...col,
    onHeaderCell: (column: { width: number }) => ({
      width: column.width,
      onResize: handleResize(index),
    }),
  }));

  return (
    <Table
      {...props}
      columns={resizableColumns}
      components={{
        header: {
          cell: ResizableTitle,
        },
      }}
      scroll={{ x: 'max-content' }} // 添加滚动条以适应自适应宽度
    />
  );
};

export default ResizeTable;

🎉 最后

RaETable 是我独立开发的antd基于table组件的一揽子生态集合,常常用于B端业务处理,在敏捷开发场景事半功倍,效果显著,希望可以帮助更多的开发者。

如果你在使用中有任何的问题,都可以联系我。

好了,今天的分享就这些,文章中错误的地方欢迎指正。

相关推荐
墨白曦煜几秒前
智能体架构范式总结(React、Plan-And-Solve、Reflection)
前端·react.js·前端框架
qq_426003964 分钟前
多语言新增语种全量测试策略的测试范围
前端·javascript·python·自动化
frjc15 分钟前
Node.js 与 npm 极简安装教程
前端·npm·node.js
IMPYLH16 分钟前
HTML 的 <main> 元素
前端·html
深念Y18 分钟前
微服务抽取路线图:从胖单体到 ARM 集群
前端·arm开发·数据库·后端·微服务·云原生·架构
阮小贰29 分钟前
干支编码的确定性实现:节气切分 + 1000 条溯源断语(附 JS 源码)
开发语言·javascript·ecmascript
雪芽蓝域zzs35 分钟前
Vue3 + Vite本地模拟数据(读取 JSON 数据)
前端
亿元程序员37 分钟前
还有高手?用Shader让图片中的3D圆环转起来!
前端
灯澜忆梦39 分钟前
【基于GO的Web开发14】gin请求重定向
前端·后端·golang·gin
qq_4523962340 分钟前
第十一篇:《前端性能优化体系:从加载到交互的全链路》
前端·性能优化·交互