import React from 'react'; import {Tag,Input,Button,Tooltip} from 'antd'; const KeyWordTagGroup = React.createClass({ getInitialState() { return { tags: [], inputVisible: false, inputValue: '' }; }, handleClose(removedTag) { const tags = this.state.tags.filter(tag => tag !== removedTag); this.props.tagsArr(tags); this.setState({ tags }); }, showInput() { this.setState({ inputVisible: true }, () => this.input.focus()); }, handleInputChange(e) { this.setState({ inputValue: e.target.value }); }, handleInputConfirm() { const state = this.state; const inputValue = state.inputValue; let tags = state.tags; if (inputValue && tags.indexOf(inputValue) === -1 && inputValue.replace(/(^\s*)|(\s*$)/g, "").length != 0) { tags = [...tags, inputValue.replace(/(^\s*)|(\s*$)/g, "")]; } this.props.tagsArr(tags); this.setState({ tags, inputVisible: false, inputValue: '', }); }, componentWillMount() { this.state.tags = this.props.keyWordArr; }, componentWillReceiveProps(nextProps) { if (this.props.keyWordArr != nextProps.keyWordArr) { this.state.tags = nextProps.keyWordArr; }; }, render() { const { tags, inputVisible, inputValue } = this.state; return (
{tags.map((tag, index) => { const isLongTag = tag.length > 10; const tagElem = ( this.handleClose(tag)}> {isLongTag ? `${tag.slice(0, 10)}...` : tag} ); return isLongTag ? {tagElem} : tagElem; })} {inputVisible && ( this.input = input} type="text" style={{ width: 78 }} value={inputValue} onChange={this.handleInputChange} onBlur={this.handleInputConfirm} onPressEnter={this.handleInputConfirm} /> )} {!inputVisible && }
); } }); export default KeyWordTagGroup;