Created
June 18, 2016 11:09
-
-
Save masious/1ea4f088148d5c96a9b2476a067e7368 to your computer and use it in GitHub Desktop.
WTF!
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 Note = React.createClass({ | |
getInitialState: function () { | |
return { | |
editing: false, | |
text: this.props.children | |
} | |
}, | |
edit: function () { | |
this.setState({ | |
editing: true | |
}); | |
}, | |
save: function () { | |
this.setState({ | |
editing: false, | |
text: this.refs.newText.getDOMNode().value | |
}) | |
}, | |
remove: function () { | |
this.props.onRemove(this.props.index); | |
}, | |
renderDisplay: function () { | |
return [( | |
<p>{this.state.text}</p> | |
), ( | |
<div className="btn-group"> | |
<button onClick={this.edit} className="btn btn-primary glyphicon glyphicon-pencil"></button> | |
<button onClick={this.remove} className="btn btn-danger glyphicon glyphicon-trash"></button> | |
</div> | |
)]; | |
}, | |
renderForm: function () { | |
return [( | |
<textarea ref="newText" className="form-control" defaultValue={this.state.text}></textarea> | |
), ( | |
<button onClick={this.save} className="btn btn-success btn-sm glyphicon glyphicon-floppy-disk"></button> | |
)] | |
}, | |
render: function () { | |
var dom = this.state.editing ? this.renderForm() : this.renderDisplay(); | |
return ( | |
<div className="col-sm-3"> | |
<div className="panel panel-default"> | |
<div className="panel-body"> | |
{dom[0]} | |
</div> | |
<div className="panel-footer"> | |
{dom[1]} | |
</div> | |
</div> | |
</div> | |
) | |
} | |
}); | |
var Board = React.createClass({ | |
getInitialState: function() { | |
return { | |
notes: [ | |
'Call Bill', | |
'Email Lisa', | |
'Make dentist appointment', | |
'Send proposal' | |
] | |
}; | |
}, | |
//propTypes: { | |
// count: function(props, propName) { | |
// if(typeof props[propName] !== 'number') { | |
// return new Error('The count property must be a number') | |
// } | |
// if(props[propName] > 100) { | |
// return new Error('Making ' + props[propName] + ' notes is ridiculous.') | |
// } | |
// } | |
//}, | |
remove: function(i) { | |
var arr = this.state.notes; | |
arr.splice(i, 1); | |
this.setState({ | |
notes: arr | |
}) | |
}, | |
renderNote: function(note, i) { | |
console.log(note); | |
return ( | |
<Note key={i} index={i} onRemove={this.remove}>{note}</Note> | |
); | |
}, | |
render: function() { | |
return ( | |
<div className="fluid-container"> | |
{this.state.notes.map(this.renderNote)} | |
</div> | |
) | |
} | |
}); | |
React.render(<Board/>, document.getElementById('react-container')); |
mohtada-h
commented
Jun 18, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment