Last active
September 11, 2016 13:41
-
-
Save charliehavak/2eb9380347040bce0aa2a90a355f8964 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
.parent { | |
width: 150px; | |
} | |
.position { | |
width: 49px; | |
height: 50px; | |
border: 1px solid #aaa; | |
display: inline-block; | |
padding: 0; | |
margin-right: -1px; | |
margin-bottom: -1px; | |
} | |
.position:hover, .selected { | |
background-color: #ea42b2; | |
cursor: pointer; | |
} | |
.label { | |
margin: 20px; | |
} |
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
var Position = React.createClass({ | |
render: function() { | |
let classes = "position " + (this.props.selected ? 'selected' : ''); | |
return <div className={classes} | |
onClick={() => {this.props.handler(this)}}>{this.props.position} | |
</div>; | |
} | |
}); | |
var PositionPicker = React.createClass({ | |
getInitialState: function() { | |
return {pos: '', rects: []}; | |
}, | |
createChilds: function() { | |
let childs = this.props.positions; | |
let itemHandler = (item) => { | |
this.setState({pos: item.props.position}); | |
this.props.onChange(item.props.position); | |
} | |
let rects = childs.map((item) => { | |
let selected = this.state.pos ? item === this.state.pos : this.props.defaultPosition === item; | |
return ( | |
<Position position={item} | |
key={item} | |
selected={selected} | |
handler={itemHandler}/> | |
) | |
}) | |
this.state.rects = rects; | |
return <div className="parent">{this.state.rects}</div> | |
}, | |
render: function() { | |
return <div>{this.createChilds()}</div> | |
} | |
}); | |
var App = React.createClass({ | |
getInitialState: function() { | |
return {selection: 'BR'}; | |
}, | |
userSelection: function(selection){ | |
this.setState({selection}); | |
}, | |
render: function() { | |
return ( | |
<div> | |
<div className="label">Hello {this.props.name}</div> | |
<PositionPicker onChange={this.userSelection} | |
positions={['TL',2,3,4,5,6,7,8,'BR']} | |
defaultPosition="TL"/> | |
<div className="label">User selection: {this.state.selection}</div> | |
</div> | |
)} | |
}); | |
ReactDOM.render( | |
<div> | |
<App name="Position Picker" /><br/> | |
</div>, | |
document.getElementById('container') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://jsfiddle.net/tm84q9pn/12/