解决antd中Povper包裹Input结合Form.Item出现的箭头定位误差

 

如果在Form.Item中使用Popover包裹Input会导致无法触发rules (这里指直接把Popover包裹Input写在Form.Item里面),

只得Popover包Form.Item包Input但是存在标题中提到的rules验证开启时的气泡箭头定位误差(如图)

然后给官方提了Issues——

得知,Popover是官方封装了自己的Tooltip(猜测,毕竟看到了相似的API),然后Tooltip底层便是rc-tooltip

 

虽然还没来得及看完文档测试demo,官方的afc163已经给出了解决办法,

 

最终解决的效果图

最终demo代码

import React from "react";
import ReactDOM from "react-dom";
import { version, Form, Popover, Input, Col, Row } from "antd";
import "antd/dist/antd.css";

const InputWithPopover = props => (
  <Popover
    content={"用户名由3~32位数字/英文/汉字组成"}
    trigger="hover"
    placement="right"
    className="register_help"
  >
    <Input {...props} />
  </Popover>
);

class LoginComponent extends React.Component {
  constructor(props) {}
  register_formRef = React.createRef();
  render() {
    return (
      <div style={{ width: 200 }}>
        <Form
          ref={this.register_formRef}
          labelCol={{ span: 6 }}
          wrapperCol={{ span: 18 }}
          labelAlign={"left"}
        >
          <Form.Item
            name="userName"
            label={<b>用户名:</b>}
            colon={false}
            rules={[
              { required: true, message: "用户名不可为空!" },
              { pattern: /^\S+$/, message: "不可以输入空格" },
              {
                pattern: /^[A-Za-z0-9\u4e00-\u9fa5]{3,32}$/,
                message: "需要3~32位数字/英文/汉字"
              }
            ]}
            hasFeedback
          >
            <InputWithPopover placeholder="请输入用户名" style={{ width: '100%' }} />
          </Form.Item>
        </Form>
      </div>
    );
  }
}
ReactDOM.render(
  <div className="App">
    <h1>antd version: {version}</h1>
    <LoginComponent />
  </div>,
  document.getElementById("root")
);

补充推测:首段有提到,笔者也不是没试过Form.Item包Poppver包Input,没成功应该是Form.Item确实默认只能作用于直接的子元素,所以afc163的解决办法里通过封装组件将外层InputWithPopover 接收到的props参数中转给了最低层的Input

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
antd Form.List ,一个 Form.Item 可以通过使用 getFieldDecorator 方法来获取自身的值,而获取其他 Form.Item 的值则需要通过 Form.List 的方法进行。例如,可以通过调用 form.getFieldValue 方法来获取其他 Form.Item 的值。具体实现步骤如下: 1. 在 Form.List ,给每个 Form.Item 一个唯一的 name 属性,例如 name={`items[${index}].name`},其 index 为当前 Form.Item 的索引。 2. 在 getFieldDecorator 方法,使用 getFieldValue 方法来获取其他 Form.Item 的值。示例代码如下: ```jsx {getFieldDecorator(`items[${index}].name`, { initialValue: item.name, rules: [{ required: true, message: 'Please input name' }], })( <Input placeholder="Enter name" /> )} {getFieldDecorator(`items[${index}].age`, { initialValue: item.age, rules: [{ required: true, message: 'Please input age' }], })( <InputNumber placeholder="Enter age" min={0} /> )} {getFieldDecorator(`items[${index}].email`, { initialValue: item.email, rules: [{ required: true, message: 'Please input email' }], })( <Input placeholder="Enter email" /> )} ``` 3. 在需要获取其他 Form.Item 值的地方,调用 form.getFieldValue 方法。示例代码如下: ```jsx const handleSubmit = e => { e.preventDefault(); form.validateFields((err, values) => { if (!err) { // 获取其他 Form.Item 值 const names = form.getFieldValue('items').map(item => item.name); console.log(names); // 提交表单 console.log('Received values of form: ', values); } }); }; ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

devwolf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值