同时使用 Ant Design of React 中 Mention 和 Form
使用场景,在一个列表中,点击每一行会弹出一个表单,通过修改表单数据并提交来修改这一行的数据,其中某个数据的填写需要通过Mention实现动态提示及自动补全的功能。
具体效果为:
遇到的问题:
1、希望所需要的提示和自动补全的内容不同,实际场景类似于ide中函数提示(包含参数和返回值以及用法等提示)和函数补全(只补全函数名)的功能。
Ant Design的Mention组件提供了Nav可以实现这个功能,但是实际使用中发现会报错,经查发现为Ant Design的一个bug,升级版本解决。
2、然后遇到问题,发现suggestions使用了Nav数组之后,不能通过输入自动动态查询,具体表现为
经查原因为,将生成suggestions的计算写到了引用Mention的地方:
const suggestions = [
{children:'张三 男 18 会计', value:'张三'},
{children:'李四 女 21 审计', value:'李四'},
{children:'王五 男 33 总监', value:'王五'}
]
<Mention
style={{ width: '100%' }}
suggestions={ suggestions.map( (x) => <Nav {...x} />) }
/>
解决方法,将计算过程提到外面去……
3、点击不同数据行的时候,弹出表单数据不更新,也就是表单的 initialValue 不生效
在 react 生命周期 componentWillReceiveProps(nextProps) 中把 Mention 的数据更新。
需要注意的地方是 Mention 的 value 不是 string,需要在多处通过 toString 和 toContentState 进行转换。
行吧,困扰了两天的bug好像写出来也没什么东西……QAQ怪我蠢……
完整代码:
import React from 'react';
import { Table, Icon, Button, Form, Input, Modal, Mention } from 'antd';
const FormItem = Form.Item;
const { toString, toContentState, Nav } = Mention; const suggestions = [
<Nav children='张三 男 18 会计' value='张三' />,
<Nav children='李四 女 21 审计' value='李四' />,
<Nav children='王五 男 33 总监' value='王五' />
] class EditForm extends React.Component {
onSave = () => {
this.props.form.validateFields((err, values) => {
if (!err) {
this.props.onSave({ ...values, person: toString(values.person) })
this.props.form.resetFields()
this.props.form.setFieldsValue({person: toContentState('')})
console.log(toString(this.props.form.getFieldValue('person')))
}
});
}
onCancel = () => {
this.props.onCancel()
// 重置为初始值 initialValue 防止点击不同区域出现数据不刷新的情况(although...i dont know why...
this.props.form.resetFields()
}
// 在接受 props 时调用 无论 nextProps 和 this.props 是否相等
// 首先要在表单隐藏变为显示的时候赋值 其次 只有当前已经存在显示值的时候才调用 否则没意义 也会存在 getFiledsValue 未注册情况
componentWillReceiveProps(nextProps) {
if (nextProps.visible === true && this.props.visible === false && this.props.record) {
this.props.form.setFieldsValue({person: toContentState(nextProps.record.person)})
}
}
render() {
// console.log(this.props)
const { record, visible, onCancel } = this.props;
if (!record) return null;
const { getFieldDecorator } = this.props.form;
return (
<Modal
visible={visible}
title="编辑事件"
okText="保存"
onCancel={this.onCancel}
onOk={this.onSave}
>
<Form layout="vertical" onSubmit={this.handleSubmit}>
<FormItem>
{getFieldDecorator('event', {
rules: [{ required: true, message: '请输入事件!' }],
initialValue: record.event
})(
<Input />
)}
</FormItem>
<FormItem>
{getFieldDecorator('person', {
initialValue: toContentState(record.person),
rules: [{ required: true, message: '请输入相关人员!' }],
})(
<Mention
style={{ width: '100%' }}
suggestions={ suggestions }
/>
)}
</FormItem>
</Form>
</Modal>
);
}
} const WrappedEditForm = Form.create()(EditForm); class EventTable extends React.Component {
state = {
visible: false,
record: null,
index: 0
}
columns = [
{
title: '操作',
key: 'action',
render: (text, record, index) =>
<div>
<Button onClick={()=>{ this.setState({
visible: true,
record,
index
}) }}>编辑</Button>
</div>,
width: 200
},
{
title: '事件',
dataIndex: 'event',
key: 'event',
render: (text, record, index) =>
<div>
<span>{text}</span>
</div>,
width: 200
},
{
title: '相关人员',
dataIndex: 'person',
key: 'person',
width: 200
}
];
data = [
{
key: '1',
event: '早餐',
person: '@组长',
}, {
key: '2',
event: '午餐',
person: '@组长',
}, {
key: '3',
event: '晚餐',
person: '@组长',
}
];
onCancel = () => {
this.setState({visible: false})
}
onSave = (values) => {
this.setState({visible: false})
this.data[this.state.index].event = values.event
this.data[this.state.index].person = values.person
}
render() {
return (
<div>
<Table columns={this.columns} dataSource={this.data} style={{ width: 600 }}/>
<WrappedEditForm visible={this.state.visible} record={this.state.record}
onCancel={this.onCancel} onSave={this.onSave} />
</div>
)
}
} export default EventTable
同时使用 Ant Design of React 中 Mention 和 Form的更多相关文章
- ElementUI(vue UI库)、iView(vue UI库)、ant design(react UI库)中组件的区别
ElementUI(vue UI库).iView(vue UI库).ant design(react UI库)中组件的区别: 事项 ElementUI iView ant design 全局加载进度条 ...
- Ant Design of React 框架使用总结1
一. 为什么要用UI 框架 统一了样式交互动画 . Ui框架会对样式,交互动画进行统一,保证了系统风格完整统一,不像拼凑起来的. 兼容性 ,不是去兼容IE 6 7 8那些低版本浏览器,而是对主流的标 ...
- Ant Design Pro (中后台系统)教程
一.概念:https://pro.ant.design/docs/getting-started-cn(官方网站) 1.Ant Design Pro 是什么: https://www.cnblogs ...
- button JS篇ant Design of react之二
最近更新有点慢,更新慢的原因最近在看 <css世界>这本书,感觉很不错 <JavaScript高级程序设计> 这本书已经看了很多遍了,主要是复习前端的基础知识,基础知识经常会过 ...
- button JS篇ant Design of react
这篇看ant Desgin of react的button按钮的js代码,js代码部分是typescript+react写的. button组件里面引用了哪些组件: import * as React ...
- React中使用Ant Table组件
一.Ant Design of React http://ant.design/docs/react/introduce 二.建立webpack工程 webpack+react demo下载 项目的启 ...
- React + Ant Design网页,配置
第一个React + Ant Design网页(一.配置+编写主页) 引用博主的另外一篇VUE2.0+ElementUI教程, 请移步: https://blog.csdn.net/u0129070 ...
- 2017.11.6 - ant design table等组件的使用,以及 chrome 中 network 的使用
一.今日主要任务 悉尼小程序后台管理开发: 景点管理页面: 获取已有数据列表,选取部分数据呈现在表格中,根据景点名称.分类过滤出对应的景点. 二.难点 1. 项目技术选取: ant design. ...
- Ant Design React按需加载
Ant Design是阿里巴巴为React做出的组件库,有统一的样式及一致的用户体验 官网地址:https://ant.design 1.安装: npm install ant --save 2.引用 ...
随机推荐
- Spring MVC 自动为对象注入枚举类型
原文地址:http://1358440610-qq-com.iteye.com/blog/2079048 如果一个对象里面有枚举类型的话,则Spring MVC是不能够直接进行注入的,因为它只实现了一 ...
- python的arp扫描
python的arp扫描 from optparse import *from scapy.all import *parser = OptionParser()parser.add_option(& ...
- 890. Find and Replace Pattern找出匹配形式的单词
[抄题]: You have a list of words and a pattern, and you want to know which words in words matches the ...
- [leetcode]50. Pow(x, n)求幂
Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Ou ...
- Spring事务管理的四种方式(以银行转账为例)
Spring事务管理的四种方式(以银行转账为例) 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败. 原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不 ...
- Ueditor 前后端分离实现文件上传到独立服务器
关于Ueditor 前后端分离实现文件上传到独立服务器,在网上搜索确实遇到大坑,不过还好遇到了 虚若影 最终实现了,在此感谢!虚若影的原文博客网址:http://www.cnblogs.com/hpn ...
- Spring事务管理的配置
spring-datasource-config.xml配置事务 <bean id="txManager" class="org.springframework.j ...
- 20172325 2018-2019-2 《Java程序设计》第七周学习总结
20172325 2018-2019-2 <Java程序设计>第七周学习总结 教材学习内容总结 二叉查找树 二叉查找树:是含附加属性的二叉树,即其左孩子小于父节点,而父节点又小于或等于右孩 ...
- caoni大业 spring boot 跳坑记
IDEA环境 win10 跑得刚刚,到xp系统就戈壁 报错 Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.g ...
- 1.5eigen中高级初始化
1.5 高级初始化 这一节讨论一些初始化矩阵的高级方法. 1.The comma initializer eigen提供一个简单设定所有矩阵系数.向量.数组系数的初始化语法,从左上角开始到右下角结束. ...