Last active
September 15, 2016 09:22
-
-
Save liesislukas/3fdb51189c507c3e236936867bcba87f 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 DropDownMenu from 'material-ui/DropDownMenu'; | |
import MenuItem from 'material-ui/MenuItem'; | |
class DropdownEditOnClick extends React.Component { | |
constructor() { | |
super(); | |
this.state = {show_input: false}; | |
this.renderText = this.renderText.bind(this); | |
this.handleChange = this.handleChange.bind(this); | |
} | |
render() { | |
return (this.state.show_input) ? this.renderInput() : this.renderText(); | |
} | |
handleChange(e, index, value) { | |
this.props.handleSave(value); | |
this.setState({show_input: false}); | |
} | |
renderInput() { | |
return ( | |
<div style={{display: 'flex'}}> | |
<DropDownMenu | |
openImmediately={true} | |
value={this.props.value} | |
onChange={this.handleChange.bind(this)} | |
onClose={() => this.setState({show_input: false})} | |
> | |
{this.props.possible_values.map(val => <MenuItem key={val} value={val} primaryText={val}/>)} | |
</DropDownMenu> | |
</div> | |
); | |
} | |
renderText() { | |
let value = (this.props.use_icons === true) | |
? <img src={`./images/${this.props.value}.png`} style={{width: 32, height: 32}}/> | |
: this.props.value; | |
return ( | |
<div | |
onTouchTap={() => this.setState({show_input: true})} | |
style={Object.assign({}, { | |
lineHeight: '48px', | |
cursor: 'pointer', | |
}, this.props.style)} | |
> | |
{value || '-'} | |
</div> | |
); | |
} | |
} | |
DropdownEditOnClick.PropTypes = { | |
value: React.PropTypes.string.isRequired, | |
style: React.PropTypes.obj, | |
use_icons: React.PropTypes.bool, | |
possible_values: React.PropTypes.array.isRequired, | |
handleSave: React.PropTypes.func.isRequired, | |
}; | |
DropdownEditOnClick.defaultProps = {style: {}}; | |
export default DropdownEditOnClick; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment