react--(查询、重置)按钮封装

该博客介绍了如何在Ant Design UI框架下创建并封装一个查询表单组件。组件包括了收起和展开功能,并且能够根据窗口大小自动调整布局。在页面中引入该组件后,可以方便地添加和管理查询表单元素,例如输入框等。博客还展示了如何在实际页面中使用这个组件,以一个包含输入框的实例进行演示。
摘要由CSDN通过智能技术生成
UI框架为ant design
第一步:创建components组件封装文件夹
1》components--ui文件夹下index.js的代码:
import FormSearch from './form'
export {FormSearch, CascaderMul}
2》components--ui--form文件夹下index.js的代码:
import {Form, Row, Col, Button, Space} from 'antd'
import {DownOutlined} from '@ant-design/icons'
import React, {useState, useEffect} from 'react'
import useMediaQuery from 'use-media-antd-query'

/**
 * 默认的查询表单配置
 */
const defaultColConfig = {
  xs: 3,
  sm: 3,
  md: 3,
  lg: 3,
  xl: 4,
  xxl: 6,
}

const getSpanConfig = (span, size) => {
  // if (typeof span === 'number') {
  //   return span
  // }
  const config = {
    ...defaultColConfig,
    ...span,
  }
  return config[size]
}
//收起、展开的代码可以根据你们需求来

const Collapse = ({collapse, onCollapse}) => {
  let text = '收起'

  if (!collapse) {
    text = '展开'
  }

  return (
    <a
      onClick={onCollapse}
      style={{display: 'flex', width: 100, alignItems: 'center'}}
    >
      {text}
      <DownOutlined
        style={{
          marginLeft: '0.5em',
          transition: '0.3s all',
          transform: `rotate(${collapse ? 0 : 0.5}turn)`,
        }}
      />
    </a>
  )
}

const getOffset = (length, span = 4) => {
  const cols = 24 / span
  return (span - 1 - (length % span)) * cols
}

/**
 * 查询表单
 * @param {Object} props 参数
 * @param {Object} props.initialValues 初始化值
 * @param {Function} props.onFinish 查询表单提交回调
 * @param {Function} props.onReset 重置表单
 * @param {Array} props.children 子组件
 * @param {Object} props.formRef 表单引用
 * @param {Boolean} props.collapsed 默认是否收起
 * @param {Number} props.span 列数
 * @returns
 */

function body({
  initialValues,
  onFinish,
  onReset = () => {},
  collapsed = false,
  span = 6,
  children,
  formRef,
}) {
  const [collapse, setCollapse] = useState(collapsed)
  const [form] = formRef ? [formRef] : Form.useForm()
  const windowSize = useMediaQuery()

  const [colSize, setColSize] = useState(span)
  const rowNumber = 24 / colSize || 3

  const onCollapse = () => {
    setCollapse(!collapse)
  }

  useEffect(() => {
    let defSpane = getSpanConfig(span, windowSize)
    setColSize(defSpane > span ? span : defSpane)
  }, [windowSize])

  const childrens = Array.isArray(children) ? children : [children]
  const item = childrens.filter((_, index) =>
    !collapse ? index < (colSize - 1 || 1) : true,
  )

  return (
    <div
      style={{
        background: '#ffffff',
        borderRadius: 4,
      }}
    >
      <Form
        form={form}
        onFinish={onFinish}
        layout="vertical"
        className="searchForm"
        initialValues={initialValues}
      >
        <Row gutter={16} justify="start">
          {item.map((child, index) => (
            <Col key={index} span={rowNumber}>
              {React.cloneElement(child)}
            </Col>
          ))}
          <Col
            span={rowNumber}
            offset={getOffset(item.length, colSize)}
            align="end"
            style={{
              textAlign: childrens.length + 1 > colSize ? 'left' : 'right',
            }}
          >
            <Form.Item label={' '}>
              <Space size="middle">
                <Space>
                  <Button
                    onClick={() => {
                      form.resetFields()
                      onReset()
                    }}
                  >
                    重置
                  </Button>
                  <Button type="primary" htmlType="submit">
                    查询
                  </Button>
                </Space>
                {childrens.length + 1 > colSize && (
                  <Collapse collapse={collapse} onCollapse={onCollapse} />
                )}
              </Space>
            </Form.Item>
          </Col>
        </Row>
      </Form>
    </div>
  )
}

export default body


3》页面引入:
import {FormSearch} from '~/components/ui'
<FormSearch>
 <Form.Item label="前缀类型" name="接口参数">
            <Input placeholder="提示信息" allowClear />
          </Form.Item>
</FormSearch>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值