react ts 封装搜索条件

封装

import React, { ReactNode, useImperativeHandle, forwardRef } from 'react';
import { Card, Form, Input, Button, Row, Col } from 'antd';

interface Iprops {
    formItem: any,
    getParams: (params: any) => void,
    reset?: () => void | undefined,
    isButton?: boolean,
    buttons?: ReactNode
}

/**
 *查询条件组件
 *
 * @param  formItem any 条件列表
 * @param  getParams function 查询
 * @param  reset function 重置
 * @param  isButton boolean 是否自定义按钮
 * @param  buttons Html 按钮
 * @return {*}
 */
const SearchFormBar = ({ formItem, getParams, reset, isButton, buttons }: Iprops, ref: any) => {
    const [form] = Form.useForm();


    const setItemStyle = formItem.length >= 4 ? { flexBasis: '30%', marginBottom: 16, marginRight: '1%' } : { flex: 1 };

    const resetForm = () => {
        form.resetFields();
        reset && reset();
    };

    const getFormParams = () => {
        const params = form.getFieldsValue();
        getParams(params);

    };

    useImperativeHandle(ref, () => ({
        getFormParams: () => {
            return form.getFieldsValue();
        }
    }));

    return <>
        <Card>
            <div className="flex">
                <div className="flex flex-1">
                    <Form layout="inline" form={form} style={{ width: '100%' }}>
                        <div style={{ display: 'flex', flexWrap: 'wrap', width: '100%' }}>
                            {formItem.map((item: any, index: number) => (
                                <div key={index} style={{ ...setItemStyle }}>
                                    <Form.Item label={item.label} name={item.name}>
                                        {item.Component}
                                    </Form.Item>
                                </div>
                            ))}
                        </div>
                    </Form>
                </div>
                <div className='flex' style={{ alignItems: 'center' }}>
                    {
                        isButton && <>{buttons}</>
                    }
                    {
                        !isButton && <>
                            <Button onClick={resetForm}>重置</Button>
                            <Button type="primary" onClick={getFormParams} style={{ marginLeft: 10 }}>查询</Button>
                        </>
                    }

                </div>
            </div>
        </Card>
    </>;
};

export default forwardRef(SearchFormBar);

使用

const getSearchFormItem = () => {
        return [
            {
                label: '关键字1',
                name: 'name',

                Component: <Input maxLength={150} allowClear placeholder="请输入" />,

            },
            {
                label: '数据形态1',
                name: 'status',
                Component: <Select allowClear placeholder="请选择" options={[{ label: 1, value: 1 }]} />,
                col: 5
            },
            {
                label: '数据形态2',
                name: 'status',
                Component: <Select allowClear placeholder="请选择" options={[{ label: 1, value: 1 }]} />,

            },
            {
                label: '数据形态3',
                name: 'status',
                Component: <Input maxLength={150} allowClear placeholder="请输入" />,

            }
        ];
    }


<SearchFormBar formItem={getSearchFormItem()} getParams={getParams} />

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值