react--自定义列表字段

支持react表格自由选择展示的字段,支持全选

1、组件代码如下
复制代码
import type { ModalProps } from "antd";
import type { CheckboxChangeEvent } from "antd/lib/checkbox";
import type { CheckboxValueType } from "antd/lib/checkbox/Group";
import type { FC, ReactNode } from "react";

import { Alert, Checkbox, Modal } from "antd";
import { useEffect, useState } from "react";

import style from "./style.module.less";

/**
 * @description 自定义table column列选择
 * @param title {string|ReactNode} modal title
 * @param alert {string|ReactNode} alert message
 * @param options {Array<CheckboxOptionType|string>} 可选字段
 * @param value {Array<string>} 已选中的值
 * @param onOk {(checked) => void} 确认回调
 * @param className {string} class
 * @param open {boolean} 是否显示modal
 * @param props {ModalType} modal 属性值
 * @param defaultCheckAll {boolean} 是否含有全选
 * @return {ReactNode} Modal
 */

interface CustomColumnsProps extends ModalProps {
  alert?: string | ReactNode;
  options: any[];
  value: CheckboxValueType[];
  defaultCheckAll?: boolean;
  handleOk: (value: CheckboxValueType[]) => void;
}

const CustomColumns: FC<CustomColumnsProps> = ({
  title = "自定义列表字段",
  alert,
  options = [],
  value = [],
  className,
  open,
  handleOk,
  ...props
}) => {
  const [checked, setChecked] = useState<CheckboxValueType[]>(value);
  const [indeterminate, setIndeterminate] = useState(true);
  const [checkAll, setCheckAll] = useState(false);

  const onCheckAllChange = (e: CheckboxChangeEvent) => {
    const result = e.target?.checked;
    const selectValue = options?.map((item) => item.value);
    setIndeterminate(false);
    setCheckAll(result);
    setChecked(
      result
        ? (selectValue as string[])
        : (options?.filter((item) => item?.disabled)?.map((item) => item.value) as string[]),
    );
  };

  const onChange = (e: CheckboxValueType[]) => {
    setChecked(e as string[]);
    if (props?.defaultCheckAll) {
      setIndeterminate(!!e?.length && e?.length < options?.length);
    }
  };
  useEffect(() => {
    if (!checked?.length) {
      setCheckAll(false);
    } else if (checked?.length === options?.length) {
      setCheckAll(true);
    }
  }, [checked]);

  useEffect(() => {
    if (open) {
      setChecked(value);
    }
  }, [open, value]);

  return (
    <Modal
      width={600}
      title={title}
      className={`${className} ${style.modal}`}
      maskClosable={false}
      open={open}
      {...props}
      onOk={() => {
        handleOk && handleOk(checked);
      }}
    >
      <Alert
        type="info"
        message={
          alert || `请选择您想显示的列表表头信息,最多可勾选${options.length}个字段,已勾选${checked.length}个字段`
        }
      />
      {props?.defaultCheckAll && (
        <Checkbox
          indeterminate={indeterminate}
          onChange={onCheckAllChange}
          checked={checkAll}
          style={{ margin: "16px 0 8px 0" }}
        >
          选择所有的字段
        </Checkbox>
      )}
      <Checkbox.Group options={options} value={checked} onChange={onChange} />
    </Modal>
  );
};

export default CustomColumns;
2、组件使用

页面导入 import CustomColumns from "@/components/CustomColumns";

设置默认状态:

复制代码
const preset = useStore("presetStore");
const [customColumnsVisible, setCustomColumnsVisible] = useState(false);
  const [columnsChecked, setColumnsChecked] = useState<CheckboxValueType[]>(
    preset?.customList?.order || [
      "orderNo",
      "account",
      "statusDesc",
      "amount",
      "paid",
      "contractPrice",
      "createdAt",
      "station",
      "chargingMode",
    ],
  );
3、定义tableColumns
4、在table组件中过滤出操作选项和默认项
5、组件调用
复制代码
<CustomColumns
     options={tableColumns
       .map(({ dataIndex, title, disabled }) => ({
        value: dataIndex,
        label: title,
        disabled,
       })).filter(({ value }) => value !== "action")}
        value={columnsChecked}
        defaultCheckAll
        open={customColumnsVisible}
        handleOk={(value) => {
          setColumnsChecked(value);
          setCustomColumnsVisible(false);
          preset.updateCustomList("order", value);
        }}
        onCancel={() => setCustomColumnsVisible(false)}
      />
相关推荐
2601_9583529017 小时前
A-59U 仅 37mm 却集成 USB + 双波束?
前端·回声消除·语音模块·降噪处理·降噪消回音
泯泷1 天前
那段文字是谁删的?Yjs 14 正式版之前,一套删除归属方案的实现与边界
前端·javascript·算法
hold?fish:palm1 天前
34 合并K个升序链表
javascript·算法·链表
野槐1 天前
前端项目优化(有了我,会发生...)
前端
幸运小圣1 天前
PointerEvent 常用属性与事件整理【JavaScript】
开发语言·javascript·ecmascript
aa小小1 天前
【无标题】
前端
计算机魔术师1 天前
还在单线程跟 AI 聊天?Claude Code 并行多会话让你一个人干三个人的活
前端
志尊宝1 天前
Vue3 零基础每日笔记(038):亲手封装 useDebounceFn 与 useThrottleFn——高频事件的流量阀
前端·javascript·vue·html·vue3
JavaGuide1 天前
对标 MinIO!全新一代分布式文件系统正式发布
前端·后端
风骏时光牛马1 天前
AI驱动自动化业务工作流搭建实践
前端