Last active
August 29, 2015 14:27
-
-
Save alexboots/ccd60895950dd1c5856d to your computer and use it in GitHub Desktop.
```
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
import React from 'react' | |
import ampersandMixin from 'ampersand-react-mixin' | |
export default React.createClass({ | |
mixins: [ampersandMixin], | |
displayName: 'Label', | |
getInitialState () { | |
const {name, color} = this.props.label | |
return { | |
name: name, | |
color: color | |
} | |
}, | |
onEditClick () { | |
this.props.label.editing = true | |
}, | |
onCancelClick () { | |
const {label} = this.props | |
if (label.saved) { | |
label.editing = false | |
this.setState(this.getInitialState()) | |
} else { | |
label.collection.remove(label) | |
} | |
}, | |
onNameChange (event) { | |
this.setState({ | |
name: event.target.value | |
}) | |
}, | |
onColorChange (event) { | |
this.setState({ | |
color: event.target.value.slice(1) | |
}) | |
}, | |
onSubmitForm (event) { | |
event.preventDefault() | |
const {label} = this.props | |
if (label.saved) { | |
label.update(this.state) | |
} else { | |
label.save(this.state) | |
} | |
label.editing = false | |
}, | |
onDeleteClick (event) { | |
this.props.label.destroy() | |
}, | |
render () { | |
const {label} = this.props | |
const {name, color} = this.state | |
const cssColor = '#' + color | |
let content | |
if (label.editing) { | |
content = ( | |
<form onSubmit={this.onSubmitForm} className='label'> | |
<span style={{backgroundColor: cssColor}} className='label-color avatar avatar-small avatar-rounded'> </span> | |
<input onChange={this.onNameChange} value={name} name='name'/> | |
<input onChange={this.onColorChange} value={'#' + color} name='color'/> | |
<button type='submit' className='button button-small'>Save</button> | |
<button onClick={this.onCancelClick} type='button' className='button button-small button-unstyled'>cancel</button> | |
</form> | |
) | |
} else { | |
content = ( | |
<div className='label'> | |
<span style={{backgroundColor: cssColor}} className='label-color'> </span> | |
<span>{label.name}</span> | |
<span onClick={this.onEditClick} className='octicon octicon-pencil'></span> | |
<span onClick={this.onDeleteClick} className='octicon octicon-x'></span> | |
</div> | |
) | |
} | |
return <div>{content}</div> | |
} | |
}) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment