首页 > 其他分享 >React+hook+ts+ant design封装一个input和select搜索的组件

React+hook+ts+ant design封装一个input和select搜索的组件

时间:2022-09-29 15:36:35浏览次数:48  
标签:React const name ts label ant item props onSearch

前言

我是歌谣 我有个兄弟 巅峰的时候排名c站总榜19 叫前端小歌谣 曾经我花了三年的时间创作了他 现在我要用五年的时间超越他 今天又是接近兄弟的一天人生难免坎坷 大不了从头再来 歌谣的意志是永恒的 放弃很容易 但是坚持一定很酷 微信公众号前端小歌谣

需求分析

首先 我们需要实现一个带有搜索功能的搜索框 本次只实现两种框的搜索功能 一种input 一种select

功能思维

React+hook+ts+ant design封装一个input和select搜索的组件_html

第一步 初始版本

先写出一个input和一个render 还有两个按钮

<Form.Item
label="测试数据"
key="1"
name="测试数据"
rules={xxx}
style={xxx}
>
{true ? <Select/> : <Input />}
</Form.Item>
<Form.Item>
<Button htmlType="submit">查询</Button>
</Form.Item>
<Form.Item>
<Button htmlType="reset" onClick={reset}>
重置
</Button>
</Form.Item>

开始升级版本(动态渲染搜索框)

接下来可以将搜索的数据改为动态渲染 因为按钮可以固定了 值从父级传入

 {props.formList.map((item: SearchFormItem) => (
<Form.Item
label={props.showLabel !== false && item.label ? item.label : ''}
key={item.name}
name={item.name}
rules={item.rules}
style={{ width: `${item.width}px` }}
>
{item.render ? item.render : <Input placeholder={item.placeholder} />}
</Form.Item>
))}

继续升级(方法通过子传父)

function SearchForm(props: SearchFormProps) {
const [form] = Form.useForm();

const reset = () => {
form.resetFields();
props.onSearch({});
};

const onSearch = () => {
form.validateFields().then(res => {
props.onSearch(res);
});
};

return (
<Form className="layout__search" form={form} layout="inline" onFinish={onSearch}>
{props.formList.map((item: SearchFormItem) => (
<Form.Item
label={props.showLabel !== false && item.label ? item.label : ''}
key={item.name}
name={item.name}
rules={item.rules}
style={{ width: `${item.width}px` }}
>
{item.render ? item.render : <Input placeholder={item.placeholder} />}
</Form.Item>
))}
<Form.Item>
<Button htmlType="submit">查询</Button>
</Form.Item>

<Form.Item>
<Button htmlType="reset" onClick={reset}>
重置
</Button>
</Form.Item>
{
props.actions.map((action: SearchFormAction, index: number) => (
<Form.Item key={action.name}>
<Button type={action.type} onClick={() => props.onClick(index)}>
{action.name}
</Button>
</Form.Item>
))
}
</Form >
);
}

继续升级(ts限定数据类型)

import React, { memo } from 'react';
import { Button, Input, Form } from 'antd';
import { ButtonType } from 'antd/es/button/button';
import './index.less';

export interface SearchFormAction {
name: string;
type?: ButtonType;
}

export interface SearchFormItem {
name: string;
label: string;
placeholder?: string;
rules?: object[];
render?: React.ReactElement;
width?: any
}

interface SearchFormProps {
formList: SearchFormItem[];
onSearch: (values: any) => void;
actions: SearchFormAction[];
onClick: (index: number) => void;
showLabel?: boolean;
width?: any
}

function SearchForm(props: SearchFormProps) {
const [form] = Form.useForm();

const reset = () => {
form.resetFields();
props.onSearch({});
};

const onSearch = () => {
form.validateFields().then(res => {
props.onSearch(res);
});
};

return (
<Form className="layout__search" form={form} layout="inline" onFinish={onSearch}>
{props.formList.map((item: SearchFormItem) => (
<Form.Item
label={props.showLabel !== false && item.label ? item.label : ''}
key={item.name}
name={item.name}
rules={item.rules}
style={{ width: `${item.width}px` }}
>
{item.render ? item.render : <Input placeholder={item.placeholder} />}
</Form.Item>
))}
<Form.Item>
<Button htmlType="submit">查询</Button>
</Form.Item>

<Form.Item>
<Button htmlType="reset" onClick={reset}>
重置
</Button>
</Form.Item>
{
props.actions.map((action: SearchFormAction, index: number) => (
<Form.Item key={action.name}>
<Button type={action.type} onClick={() => props.onClick(index)}>
{action.name}
</Button>
</Form.Item>
))
}
</Form >
);
}

export default memo(SearchForm);

看看父组件的使用

<Card>
<SearchForm
formList={formList}
actions={actions}
onSearch={onSearch}
onClick={onAddMenu}
showLabel={true}
></SearchForm>
</Card>

formList搜索表单值

    const formList = useMemo<SearchFormItem[]>(
() => [
{
width: 280,
name: 'factoryId',
placeholder: '请选择所属工厂',
label: '所属工厂',
render: (
<Select
style={{ width: '100%' }}
placeholder="请选择所属工厂"
optionFilterProp="children"
>
{factoryDataList && factoryDataList.map((item: any) => (
<Option value={item.id}>{item.name}</Option>
))}
</Select>
)
},
],
[],
);

actions按钮值

const actions = useMemo<SearchFormAction[]>(
() => [
{
name: '新建',
type: 'primary',
},
],
[],
);

onSearch子传父方法值

 const onSearch = useCallback(
(params: MenuSearchParams) => {
initPageList(params);
},
[page],
);

onAddMenu 控制弹框开启的值

 const onAddMenu = useCallback(() => {
setCurrentMenu(null);
setEditVisible(true);
}, []);

结果

React+hook+ts+ant design封装一个input和select搜索的组件_html_02

我是歌谣 放弃很容易 坚持一定很酷 关注前端小歌谣带你进入前端巅峰交流群

标签:React,const,name,ts,label,ant,item,props,onSearch
From: https://blog.51cto.com/u_14476028/5722996

相关文章

  • React+hook+ts+ant design封装一个table的组件
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......
  • 二进制部署k8s集群v1.23.9版本-21-安装LTS任务调度
    21.1、准备镜像192.168.1.200服务器操作lts-jobtracker镜像dockerpullharbor.qgutech.com/qx-apaas/lts-jobtracker:v1dockertag8f1e3d395515harbor.qgutech.com/......
  • vue +echarts+ docxtemplater导出word和图表图片
    1.下载插件//--安装docxtemplaternpminstalldocxtemplaterpizzip--save//--安装jszip-utilsnpminstalljszip-utils--save//--安装jszipnpminstallj......
  • 四、react-redux(demo可对比redux)
    react-redux调用关系: react-reducdemo1.安装插件:npminstall--savereact-redux2.创建项目:demo效果还是和redux的demo是一样的 demo目录结构:src下新增了containers文......
  • 一、react基本应用
    一、helloworld(可直接用浏览器打开文件即可)cdn:bootcdn有各种js的cdn;一般提供开发版(js未压缩)和生产版(js压缩过了)需要引入3个js核心库:<scriptsrc="./node_modules/reac......
  • jdbc 数据的增删改查的Statement Resultset PreparedStatement
    完成数据库的连接,就马上要对数据库进行增删改查操作了;先来了解一下Statement通过JDBC插入数据(这里提供一个查找和插入方法)Statement:用于执行sql语句的对象;*1.通过C......
  • react 代码编辑器react-ace
    github:https://github.com/securingsincity/react-acestar:3.6knpm:weeklydownloads280,045DEMOofReactAce:https://securingsincity.github.io/react-ace/i......
  • MySQL:The used SELECT statements have a different number of columns
    我们在SQL语句中使用了 UNION 连接两张表时,查询字段数量不一致导致......
  • Vue2,Vue3,React,vue-cli和Vite创建和启动
    vue2-project创建项目vuecreatexxx名字下依赖包npminstall启动项目npmrunserve项目打包npmrunbuildvue3-project创建项目vuecreatexxx名字下依......
  • mysql grant
    目录mysqlgrant基本语法基本权限设置高级权限、细分权限查询权限回收权限授予权限mysqlgrant基本语法grant权限on数据库对象to用户identifiedby密码用户一......