画布上有几个点,单击一个应显示允许编辑所选点的x和y坐标的工具栏。
我试过:
/**
* @jsx React.DOM
*/
var EditorCanvas = React.createClass({
getInitialState: function() {
return {
points: [
{
x: 50,
y: 100
},
{
x: 200,
y: 50
}
],
selected: null
};
},
render: function() {
return <div>
<Toolbar point={this.state.selected}/>
<div id="canvas">
{this.state.points.map(this.renderPoint)}
</div>
</div>;
},
renderPoint: function(point) {
return <Point x={point.x} y={point.y} onClick={this.selectPoint.bind(this, point)}/>
},
selectPoint: function(point) {
this.setState({selected: point});
},
renderToolbar: function() {
var selected = this.state.selected;
if (!selected) {
return <div/>
}
return <div>
x:<input type="number" value={selected.x} onInput={this.xChange}/>
·
y:<input type="number" value={selected.y} onInput={this.yChange}/>
</div>
}
});
var Toolbar = React.createClass({
render: function() {
var point = this.props.point;
if (!point) {
return <div id="toolbar"/>;
}
return <div id="toolbar">
x:<input type="number" value={point.x} onInput={this.xChange}/>
·
y:<input type="number" value={point.y} onInput={this.yChange}/>
</div>;
},
xChange: function(e) {
this.setState({selected: {x: e.target.value}});
},
yChange: function(e) {
this.setState({selected: {y: e.target.value}});
}
});
var Point = React.createClass({
render: function() {
var style = {
left: this.props.x + 'px',
top: this.props.y + 'px'
};
return <div style={style} className="point" onClick={this.props.onClick}/>
}
});
React.renderComponent(
<EditorCanvas/>,
document.body
);
它不起作用,因为它state.selected
是点对象的副本,而不是指向它的指针。
我怎样才能让它工作?