Created
September 11, 2015 00:41
-
-
Save mykelswitzer/ede5fa84d67dcd2ab134 to your computer and use it in GitHub Desktop.
React.js with pickadate.js
This file contains hidden or 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
// How to get the pickadate to mount correcty in React.js component | |
// requires jQuery and pickadate.js (https://github.com/amsul/pickadate.js/) | |
var Component = React.createClass({ | |
getInitialState: function() { | |
return ({value: null}); | |
}, | |
componentDidMount: function() { | |
this.setupDatepicker(); | |
}, | |
componentDidUpdate: function() { | |
this.setupDatepicker(); | |
}, | |
setupDatepicker: function() { | |
// cache this so we can reference it inside the datepicker | |
var comp = this; | |
// the element | |
var el = this.refs.datepicker; | |
$(React.findDOMNode(el)).pickadate({ | |
format: 'yyyy-mm-dd', | |
formatSubmit: 'yyyy-mm-dd', | |
selectMonths: true, | |
selectYears: 5, | |
closeOnSelect: true, | |
onSet: function(e) { | |
// you can use any of the pickadate options here | |
var val = this.get('select', 'yyyy-mm-dd'); | |
el.setState({value: val}); | |
comp.onDateChange({target: {value: el.state.value}}); | |
// auto close on select | |
this.close(); | |
} | |
}); | |
}, | |
onDateChange: function (event) { | |
this.setState({operand: event.target.value}); | |
}, | |
render: function () { | |
return (<input type="date" ref="datepicker" value={this.state.value} onChange={this.onDateChange} />); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I updated this for the latest version of React.
Use like this (both props are optional):