improve tag demo

This commit is contained in:
然则 2016-05-06 22:03:05 +08:00
parent 0dbf42a59c
commit 227ea44010

View File

@ -20,6 +20,15 @@ const App = React.createClass({
], ],
}; };
}, },
onClose(key, e) {
e.preventDefault();
if (key === 1) {
console.log('can not close it', this.state.tags);
this.setState({ tags: [...this.state.tags] });
return;
}
this.handleClose(key);
},
handleClose(key) { handleClose(key) {
const tags = [...this.state.tags].filter(tag => (tag.key !== key) && tag); const tags = [...this.state.tags].filter(tag => (tag.key !== key) && tag);
console.log(tags); console.log(tags);
@ -35,7 +44,8 @@ const App = React.createClass({
return ( return (
<div> <div>
{this.state.tags.map(tag => {this.state.tags.map(tag =>
<Tag key={tag.key} closable afterClose={() => this.handleClose(tag.key)}>{tag.name}</Tag> <Tag key={tag.key} closable onClose={(e) => this.onClose(tag.key, e)}
afterClose={() => this.handleClose(tag.key)}>{tag.name}</Tag>
)} )}
<div> <div>
<Button onClick={this.addTag}>添加标签</Button> <Button onClick={this.addTag}>添加标签</Button>