observeTagsClick(type, text){ // 将选中的标签插入输入行
let pre_text = this.props.getFieldsValue(['inspection']).inspection;
console.log("pre_text",pre_text)
this.setState({ value: text });
if(type === 'add' && text){
// console.log("111",add);
if(pre_text){
if(!pre_text.includes(text)){
var lastChar = pre_text.substr(pre_text.length - 1, 1);
if(lastChar === ',' || lastChar == '。' || lastChar === '、' || lastChar === ';' || lastChar === ',' || lastChar === '.' || lastChar === '/'){
pre_text += text; // 如果最后一个字符包含符号则直接添加字符串
}else{
pre_text += '、' + text;
}
this.props.setFieldsValue({inspection: pre_text});
this.setState({ value: pre_text });
}
}else{
this.props.setFieldsValue({inspection: text});
this.setState({ value: text });
}
}else{
if(pre_text.includes(text) && text){ // 字符串匹配
for(var i=0; i < pre_text.length; i++){
var searchResult = pre_text.startsWith(text, i); // 判断是否存在如果存在
if(searchResult){
pre_text = pre_text.replace(text, '');
if(pre_text[i] === ',' || pre_text[i] == '。' || pre_text[i] === '、' || pre_text[i] === ';' || pre_text[i] === ',' || pre_text[i] === '.' || pre_text[i] === '/'){
let prefix = pre_text.substr(0, i);
pre_text = prefix + pre_text.substr(i+1);
}
let lastChar = pre_text.substr(pre_text.length - 1, 1);
if(lastChar === ',' || lastChar == '。' || lastChar === '、' || lastChar === ';' || lastChar === ',' || lastChar === '.' || lastChar === '/'){ // 最后一个字符不允许是范围内的字符
pre_text = pre_text.substr(0, pre_text.length - 1);
}
this.props.setFieldsValue({inspection: pre_text});
this.setState({ value: pre_text });
// this.props.setInitData({inspection:pre_text});
}
}
}
}
其实用ui库跟容易(antd就有这种组件)。自己实现也行。你选中之后,把值存起来。然后获取input赋值给它的value.