react antd快速搭建crud页面

日常的crud页面分为筛选、表格、分页

使用react和antd快速搭建页面,只需简单配置json即可

对按钮级别进行二次封装,主要是点击弹出modal

里面是常见add,delete,edit三种按钮进行弹窗封装

ini 复制代码
import { Button, Modal } from "antd";
import React, { useState } from "react";

type XtButtonProps = {
	type: string;
	onOk: () => void;
	getRowData?: any;
	className?: string;
	children?: React.ReactNode;
};

const XtButton: React.FC<XtButtonProps> = ({ type, onOk, getRowData, children, className, ...rest }) => {
	const [modalVisible, setModalVisible] = useState(false);

	const handleCloseModal = () => {
		setModalVisible(false);
	};

	const handleOpenModal = () => {
		getRowData();
		setModalVisible(true);
	};

	const handleOk = () => {
		onOk();
		setModalVisible(false);
	};

	const handleDelete = () => {
		Modal.confirm({
			title: "Delete Item",
			content: "Are you sure you want to delete this item?",
			onOk: handleOk,
			onCancel: handleCloseModal
		});
	};

	return (
		<>
			{type === "delete" ? (
				<Button className={className} onClick={handleDelete}>
					Delete
				</Button>
			) : (
				<Button className={className} type="primary" onClick={handleOpenModal}>
					{type === "add" ? "Add" : "Edit"}
				</Button>
			)}
			{type !== "delete" && (
				<Modal
					visible={modalVisible}
					onOk={handleOk}
					onCancel={handleCloseModal}
					title={type === "add" ? "Add Item" : "Edit Item"}
					{...rest}
				>
					{children}
				</Modal>
			)}
		</>
	);
};

export default XtButton;

然后是table的二次封装,主要二次封装是获取数据加载loading

typescript 复制代码
import { Table } from "antd";
import React, { useEffect, useState } from "react";
type XtTableProps = {
	columns: any;
	dataSource: any;
	getData: () => void;
};
const XtTable: React.FC<XtTableProps> = ({ columns, dataSource, getData, ...rest }) => {
	const [loading, setLoading] = useState(true);
	// 获取table数据
	const getTableData = async () => {
		setLoading(true);
		getData();
		setLoading(false);
	};

	useEffect(() => {
		getTableData();
	}, []);

	return (
		<>
			<Table columns={columns} dataSource={dataSource} loading={loading} rowKey="id" {...rest} />
		</>
	);
};
export default XtTable;

其次就是弹窗组件的children的组件封装,这里是模仿table的columns的json配置进行一个form封装,render方法是进行一个子组件的渲染

typescript 复制代码
import React from "react";

type DynamicFormProps = {
	columns: any[]; // 配置数组
	width?: string;
};

const DynamicForm: React.FC<DynamicFormProps> = ({ columns, width = "80px" }) => {
	return (
		<div>
			{columns.map((item: any) => (
				<div key={item.title} className="flex-ct" style={{ marginBottom: "16px" }}>
					<div style={{ width: width, marginRight: "16px" }}>{item.title}:</div>
					<div style={{ flex: 1 }}>{item.render()}</div>
				</div>
			))}
		</div>
	);
};

export default DynamicForm;

封装的form组件,可以渲染Input、select等组件 ,具体的可以在render里面去配置

typescript 复制代码
import DynamicForm from "@/components/DynamicForm";
import XtSelect from "@/components/Select";
import { Input } from "antd";
import React from "react";

type FormProps = {
	data: any;
	setData: any;
};
const roleList = ["超级管理员", "客户"];
const form: React.FC<FormProps> = ({ data, setData }) => {
	const { account, password, role, devices } = data;
	const columns = [
		{
			title: "account",
			render: () => <Input value={account} onChange={e => handleChange(e.target.value, "account")}></Input>
		},
		{
			title: "password",
			render: () => <Input value={password} onChange={e => handleChange(e.target.value, "password")}></Input>
		},
		{
			title: "role",
			render: () => <XtSelect options={roleList} value={role} onChange={e => handleChange(e, "role")} />
		},
		{
			title: "devices",
			render: () => <XtSelect mode="multiple" options={roleList} value={devices} onChange={e => handleChange(e, "devices")} />
		}
	];
	const handleChange = (e: any, field: string) => {
		setData((prevData: any) => ({
			...prevData,
			[field]: e
		}));
		console.log("🚀 ~ file: form.tsx:37 ~ handleChange ~ e:", e);
	};
	return <DynamicForm columns={columns} />;
};
export default form;

遇到crud页面,就只需要配置table的json、筛选项以及modal的form,就可以快速生成页面进行接口联调,当然这个是比较通用的,筛选项等都是自己后续去书写,如果你的项目有其他的需求可以按照这个去再进行封装

相关推荐
To_OC1 小时前
从一头雾水到跑通全流程:我用一个周末啃透了JWT登录鉴权
前端·后端·http
excel3 小时前
研究 Vue 3 源码的收获
前端·vue.js
可乐鸡翅yeah_5 小时前
业务中 M3U8 水印相关坑,硬水印和动态水印区别
前端·网络·数据库·ffmpeg·m3u8在线
lerhxx5 小时前
AI 应用如何高效优雅地恢复中断?—— "连接解耦 + 状态持久化"
前端·javascript
计算机魔术师5 小时前
METR 演示 AI 智能体如何篡改 Inspect 评估记录以掩盖不当行为
前端
前端snow6 小时前
ai agent --- 异步处理之 Rabbit MQ
前端
念何架构之路6 小时前
zap扩展生态与总结
java·前端·数据库
独孤九剑打醒他6 小时前
【原创开源】【概念设计】源 - 栅 - 漏 - 栅 - 源 横向双栅 MOS,低压交流多值逻辑芯片探索
前端·其他·架构·开源·硬件工程
mantou1326 小时前
我给 AI Agent 做了个「油猴」:让 Claude Code / Codex 直接用你已登录的浏览器
前端·javascript·后端
默_笙6 小时前
🍕 一个主编、三个工种、两本手册:搭一支 AI 调研队
前端·javascript