AntDesign getFieldDecorator 获取自定义组件的值
AntDesign getFieldDecorator 获取自定义组件的值
1.自定义或第三方的表单控件,也可以与 Form 组件一起使用。只要该组件遵循以下的约定:
(1)提供受控属性 value 或其它与 valuePropName 的值同名的属性。
(2)提供 onChange 事件或 trigger 的值同名的事件。
(3)不能是函数式组件。
2.创建组件
这里通过自定义的搜索输入框来展示
let timeout;
let currentValue;
function fetch(value, callback) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
currentValue = value;
function getData() {
const params = {
size: 10,
name: value
};
apiSubwayList(params).then(
d => {
if (currentValue === value) {
const result = d.list;
const data = [];
result.forEach(r => {
data.push({
value: r.subwayId,
text: r.name,
});
});
callback(data);
}
}
);
}
timeout = setTimeout(getData, 300);
}
class SearchInput extends PureComponent {
state = {
data: [],
value: undefined
};
handleSearch = value => {
fetch(value, data => this.setState({data}));
};
handleChange = value => {
this.setState({value});
this.props.onChange(value)
};
render() {
const options = this.state.data.map(d => <Option key={d.value}>{d.text}</Option>);
return (
<div>
<Select
showSearch
value={this.state.value}
placeholder={this.props.placeholder}
// style={this.props.style}
defaultActiveFirstOption={false}
showArrow={false}
filterOption={false}
onSearch={this.handleSearch}
onChange={this.handleChange}
notFoundContent={null}
>
{options}
</Select>
<span>输入并选择对应选项,否则无效</span>
</div>
)
}
}
SearchInput.propTypes = {
data: PropTypes.array,
value: PropTypes.string,
onChange: PropTypes.func
};
export default SearchInput;
可以看到,使用getFieldDecorator时,会通过props的形式向自定义的组件传入onChange回调方法,使用这个回调函数会通知getFieldDecorator所绑定字段的值的变化。
3.使用组件
<FormItem
{...formItemLayout}
label={<span>所在地铁站</span>}
>
{getFieldDecorator('owner', {
rules: [{
required: true,
message: '请选择所在地铁站',
},
],
})(
<SearchInput placeholder="输入并选择所属地铁"/>)}
</FormItem>
使用getFieldDecorator会隐式的传入onChange回调方法,当然,如果想传入其他参数,也可以像placeholder那样显示的传入。
AntDesign getFieldDecorator 获取自定义组件的值的更多相关文章
- ant-design getFieldDecorator 无法获取自定义组件的值
1.自定义或第三方的表单控件,也可以与 Form 组件一起使用.只要该组件遵循以下的约定: (1)提供受控属性 value 或其它与 valuePropName 的值同名的属性. (2)提供 onCh ...
- Vue 父组件主动获取子组件的值,子组件主动获取父组件的值
父组件主动获取子组件的值 1. 在调用子组件的时候定义一个ref-> ref="header"2. 在父组件中通过this.$refs.header.属性,调用子组件的属性, ...
- vue3 template refs dom的引用、组件的引用、获取子组件的值
介绍 通过 ref() 还可以引用页面上的元素或组件. DOM 的引用 <template> <div> <h3 ref="h3Ref">Tem ...
- jsp获取单选按钮组件的值
jsp获取单选按钮组件的值 1.首先,写一个带有单选按钮组件的前台页 1 <%@ page language="java" contentType="text/ht ...
- Vue自定义组件插入值
我们自定义组件的时候有时候需要往组件里面插一些内容: //定义一个组件test,插值内容用slog来代替 export default { name: 'test', template:` <d ...
- antd 父组件获取子组件中form表单的值
还是拿代码来讲吧,详情见注释 子组件 import React, { Component } from 'react'; import { Form, Input } from 'antd'; con ...
- Android开发之自定义组件和接口回调
说到自定义控件不得不提的就是接口回调,在Android开发中接口回调用的还是蛮多的.在这篇博客开始的时候呢,我想聊一下iOS的自定义控件.在iOS中自定义控件的思路是继承自UIView, 在UIVie ...
- 微信小程序页面调用自定义组件内的事件
微信小程序页面调用自定义组件内的事件 page page.json { "usingComponents": { "my-component": ". ...
- vue子组件改变父组件的值
1 在父组件的coment绑定事件 <template> <div :class="classObj" class="app-wrapper" ...
随机推荐
- laravel 5.6 请教邮件中的cc,bcc是什么意思,有什么用?
cc指抄送 bcc指暗送. cc:carbon copy bcc:blind carbon copy
- Python--day21--复习
序列化模块总结: jison格式化输出: Serialize obj to a JSON formatted str.(字符串表示的json对象) Skipkeys:默认值是False,如果dict的 ...
- Vue中computed与method的区别
转载于:https://segmentfault.com/a/1190000014478664?utm_source=tag-newest 1.computed区别于method的两个核心 在官方文档 ...
- H3C 配置NAT Server
- pip安装python包时报字符编码错
比如安装scikit-learn时报错: django ascii’ codec can’t encode character 原因是用户目录或用户名存在中文,ascii不能解码,解决办法是在Pyth ...
- win10下,cmd,power shell设置默认编码为‘UTF-8’?
这个问题可以终结了,最新版 Windows 10 支持 UTF-8 了.打开这个选项,cmd 和 powershell 默认就是 UTF-8 了.在控制面板-时钟和区域-区域-管理-更改系统区域设置( ...
- 安装node-sass时出现的错误解决方案(Mac自用,也可以借鉴)
安装node-sass时出现一下错误: gyp ERR! configure error gyp ERR! stack Error: EACCES: permission denied, mkdir ...
- SpringDataJPA+QueryDSL玩转态动条件/投影查询
在本文之前,本应当专门有一篇博客讲解SpringDataJPA使用自带的Specification+JpaSpecificationExecutor去说明如何玩条件查询,但是看到新奇.编码更简单易懂的 ...
- openmp的g++并行执行
#include <omp.h>#include <stdio.h>#include <stdlib.h>void Test(int n) { for(int ...
- linux内核指针和错误值
很多内部内核函数返回一个指针值给调用者. 许多这些函数也可能失败. 大部分情况, 失 败由返回一个 NULL 指针值来指示. 这个技术是能用的, 但是它不能通知问题的确切特性. 一些接口确实需要返回一 ...