公共筛选组件(二次封装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

相关文章

SpringKafka消息发布之KafkaTemplate与事务支持功能

《SpringKafka消息发布之KafkaTemplate与事务支持功能》通过本文介绍的基本用法、序列化选项、事务支持、错误处理和性能优化技术,开发者可以构建高效可靠的Kafka消息发布系统,事务支... 目录引言一、KafkaTemplate基础二、消息序列化三、事务支持机制四、错误处理与重试五、性能优

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

SpringBoot中封装Cors自动配置方式

《SpringBoot中封装Cors自动配置方式》:本文主要介绍SpringBoot中封装Cors自动配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot封装Cors自动配置背景实现步骤1. 创建 GlobalCorsProperties

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

mss32.dll文件丢失怎么办? 电脑提示mss32.dll丢失的多种修复方法

《mss32.dll文件丢失怎么办?电脑提示mss32.dll丢失的多种修复方法》最近,很多电脑用户可能遇到了mss32.dll文件丢失的问题,导致一些应用程序无法正常启动,那么,如何修复这个问题呢... 在电脑常年累月的使用过程中,偶尔会遇到一些问题令人头疼。像是某个程序尝试运行时,系统突然弹出一个错误提