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" ...
随机推荐
- 洛谷 1602 Sramoc问题
Description 话说员工们整理好了筷子之后,就准备将快餐送出了,但是一看订单,都傻眼了:订单上没有留电话号码,只写了一个sramoc(k,m)函数,这什么东西?什么意思?于是餐厅找来了资深顾问 ...
- 微信小程序 view中的image水平垂直居中
当 display: flex 配合 justify-content: center 使用时可以让view水平居中 而配合 align-items: center 用时可以实现垂直居中效果 .card ...
- 【原生JS】图片预加载之有序预加载
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- 2019-9-2-git-需要知道的1000个问题
title author date CreateTime categories git 需要知道的1000个问题 lindexi 2019-09-02 10:12:31 +0800 2018-2-13 ...
- 2018-3-7-VisualStudio-csproj-添加-ItemGroup-的-Service-
title author date CreateTime categories VisualStudio csproj 添加 ItemGroup 的 Service lindexi 2018-3-7 ...
- 降智严重——nowcoder练习赛46&&codeforces #561 Div2
两场比赛降智不停,熬夜爆肝更掉rating nowcoder: https://ac.nowcoder.com/acm/contest/894#question T1:水题 T2:考虑a和b的子区间! ...
- laydate type=time/datetime/date 开始时间和结束时间的输入限制
最近项目中使用了laydate插件,需要限制开始时间和结束时间的输入 1.type=date 要求:周期开始时间和周期结束时间以天为单位,结束时间不能早于开始时间,可以是同一天. 周期开始时间:< ...
- js基础——面向对象(构造函数)
1.面向对象:类的标志,通过类可创建多个具有相同属性和方法的对象 2.创建对象 1)工厂模式方式:避免重复实例化但未能解决识别问题 function boss(name, age) { ...
- setTimeout第三个参数
在廖雪峰大神的官方网站看nodejs的时候,发现自己对Promise函数不甚了解,于是转去看Promise函数,看到了这段代码: function multiply(input) { return n ...
- CSS多余文本省略号显示
CSS多余文本省略号显示 本次案例代码是在 elementui 当中的 table 组件中实际需求 当然使用的是纯 CSS3 代码,所以原生支持度高,兼容性高,所以可多场景应用 对于过长文本进行单行省 ...