Last active
June 14, 2016 09:56
-
-
Save 0xF013/db7d21ef3e102bd3ac9ce68e117eb6a4 to your computer and use it in GitHub Desktop.
React bootstrap datepicker wrapper
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
import React, { Component, PropTypes } from 'react'; | |
import jQuery from 'jquery'; | |
require('bootstrap-datepicker/dist/css/bootstrap-datepicker.css'); | |
require('bootstrap-datepicker/js/bootstrap-datepicker.js'); | |
export default class DatePicker extends Component { | |
static propTypes = { | |
date: PropTypes.instanceOf(Date).isRequired, | |
onChange: PropTypes.func.isRequired, | |
}; | |
componentDidMount() { | |
const $div = this._getHolder(); | |
$div | |
.datepicker({}) | |
.on('changeDate', e => this.props.onChange(e.date)); | |
this._updateActiveDateFromProps(); | |
} | |
componentDidUpdate() { | |
this._updateActiveDateFromProps(); | |
} | |
_updateActiveDateFromProps() { | |
const $div = this._getHolder(); | |
const { date } = this.props; | |
$div.datepicker('update', date); | |
} | |
_getHolder() { | |
return jQuery(this.refs.dateInput); | |
} | |
render() { | |
return ( | |
<input type="text" className="form-control" ref="dateInput" /> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment