公共筛选组件(二次封装antd)支持代码提示

2024-06-24 13:52

本文主要是介绍公共筛选组件(二次封装antd)支持代码提示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

如果项目是基于antd组件库为基础搭建,可使用此公共筛选组件
界面样式

使用到的库

npm i antd
npm i lodash-es
npm i @types/lodash-es -D

/components/CommonSearch

index.tsx

import React from 'react';
import { Button, Card, Form } from 'antd';
import styles from './index.module.scss';
import { renderItem, IItem } from './const';
import { debounce, pickBy } from 'lodash-es';interface IProps {items: IItem[];/** 标签位置 默认: inset */labelAlign?: 'left' | 'inset';/** 查询回调 */onSearch: <T>(values: T) => void;/** 重置回调 */onReset: () => void;
}export default function Index(props: IProps) {const { items = [], labelAlign = 'inset', onSearch, onReset } = props;const [form] = Form.useForm();const clasNameHandle = (row: IItem) => {const labelInset = styles[`custom-form-item-${row?.type?.toLowerCase() || 'label'}`];let className = `${styles['form-item-input']} ${row?.props?.className || ''} `;if (labelAlign === 'inset') {className += labelInset;}className = className.trim();return className;};const trimWhitespace = (value) => {if (typeof value === 'string') {return value.trim();}return value;};const searchHandle = debounce(() => {const values = form.getFieldsValue();const format = pickBy(values, (v) => v !== undefined && v !== null && v !== '');const delTrim = Object.keys(format).reduce((pre: any, cur: any) => {pre[cur] = trimWhitespace(format[cur]);return pre;}, {});console.log(delTrim, 'values');onSearch && onSearch(delTrim);}, 100);const resetSearch = debounce(() => {form.resetFields();onReset && onReset();}, 100);if (!items?.length) {return null;}return (<Card className={styles['search-card-box']}><Form layout="inline" form={form}>{items?.map((item: IItem) => {const className = clasNameHandle(item);const itemProps = {options: item.options,onPressEnter: () => {searchHandle();},...(item.props ?? {}),className,};return (<divkey={item.name}className={`${styles['search-item-wrap']} ${labelAlign === 'inset' ? styles['search-item-wrap-inset'] : ''}`}><Form.Item label={item.label} name={item.name}>{renderItem(item.type, itemProps)}</Form.Item></div>);})}</Form><div className={styles['search-btn']}><Button type="primary" onClick={searchHandle}>查询</Button><Button type="default" onClick={resetSearch}>重置</Button></div></Card>);
}

index.module.scss

.search-card-box {:global {.ant-card,.ant-card-body {padding: 16px 0 16px 16px;}}.search-item-wrap {width: 25%;min-width: 240px;margin-bottom: 12px;.form-item-input {width: 100%;}.custom-form-item-timepicker,.custom-form-item-input,.custom-form-item-select,.custom-form-item-inputnumber,.custom-form-item-datepicker,.custom-form-item-label {width: 100%;border-color: transparent;box-shadow: none;&:focus {box-shadow: none;}}}.search-item-wrap-inset {:global {.ant-row {padding-left: 12px;border: 1px solid #d9d9d9;border-radius: 6px;}.ant-row:focus-within {border-color: var(--primary-color);}}}.search-btn {display: flex;gap: 12px;justify-content: flex-end;padding-right: 16px;}
}

const.tsx

import {DatePicker,DatePickerProps,Input,InputNumber,InputNumberProps,InputProps,Select,SelectProps,TimePickerProps,
} from 'antd';interface IOptions {label: string;value: any;
}type IType = 'Input' | 'InputNumber' | 'Select' | 'DatePicker' | 'TimePicker';
type PropsMap = {Input: InputProps;InputNumber: InputNumberProps;Select: SelectProps;DatePicker: DatePickerProps;TimePicker: TimePickerProps;
};export interface IItem<T extends IType = IType> {label: string;name: string;type: T;props?: PropsMap[T];options?: IOptions[];
}export const renderItem = (type, props) => {switch (type) {case 'Input':return <Input placeholder="请输入" {...props} />;case 'TextArea':return <Input.TextArea placeholder="请输入" {...props} />;case 'InputNumber':return <InputNumber placeholder="请输入" controls={false} {...props} />;case 'Select':return <Select placeholder="请选择" {...props} />;case 'DatePicker':return <DatePicker {...props} />;case 'TimePicker':return <DatePicker.TimePicker {...props} />;default:return <Input placeholder="请输入" {...props} />;}
};

这篇关于公共筛选组件(二次封装antd)支持代码提示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1090336

相关文章

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

Python运行中频繁出现Restart提示的解决办法

《Python运行中频繁出现Restart提示的解决办法》在编程的世界里,遇到各种奇怪的问题是家常便饭,但是,当你的Python程序在运行过程中频繁出现“Restart”提示时,这可能不仅仅是令人头疼... 目录问题描述代码示例无限循环递归调用内存泄漏解决方案1. 检查代码逻辑无限循环递归调用内存泄漏2.

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

SpringQuartz定时任务核心组件JobDetail与Trigger配置

《SpringQuartz定时任务核心组件JobDetail与Trigger配置》Spring框架与Quartz调度器的集成提供了强大而灵活的定时任务解决方案,本文主要介绍了SpringQuartz定... 目录引言一、Spring Quartz基础架构1.1 核心组件概述1.2 Spring集成优势二、J

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到