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" ...
随机推荐
- Codeforces Round #577 (Div 2)
A. Important Exam 水题 #include<iostream> #include<string.h> #include<algorithm> #in ...
- oracle优化EXPORT和IMPORT
使用较大的BUFFER(比如10MB , 10,240,000)可以提高EXPORT和IMPORT的速度. ORACLE将尽可能地获取你所指定的内存大小,即使在内存不满足,也不会报错.这个值至少要和表 ...
- 5分钟了解为什么学习Go
1.什么是Go语言? Google开源 编译型语言 21世纪的C语言(主流编程语言都是单线程环境下发布的) 2.Go语言的特点? 简单易学习(类似python学习难度,自带格式化) 开发效率高 执行性 ...
- H3C NAT Server
- Python--day46--MySQL视图
1,创建一个视图(给临时表起一个别名v1),视图也虚拟出来的,并非真正的物理表,不能直接往里面插数据.它的数据是动态的读出来的. create view v1 as select * from use ...
- Python--day41--递归锁Rlock
1,递归锁Rlock:递归锁是为了解决死锁问题,且递归锁的特点是在同一个线程中可以被acquire()多次 多个acquire()在递归锁中不会阻塞,而互斥锁Lock就会阻塞 代码示例: from t ...
- H3C 端口绑定典型配置举例
- Github上的英文解释
1.AFAIK: As far as I know. 据我所知 2.SPOF: Single point of failure. 单节点崩溃 3.ASAP: As soon as possible. ...
- Common Logging包装设计
类设计 LogFactory根据当前环境加载具体的Log实现: 1.从缓存中加载LogFactory 2.从系统属性org.apache.commons.logging.LogFactory 中加载L ...
- LightOJ - 1265 Island of Survival (概率dp)
You are in a reality show, and the show is way too real that they threw into an island. Only two kin ...