前端项目实战38-查询都要单独形成一套组件-优秀呀

父组件

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

子组件

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;
}

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

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}
        >
          {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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值