Created
July 13, 2015 00:30
-
-
Save marr/7a69eaf38beef4c69569 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
import React from 'react'; | |
import { Calendar, DateInput, Popover } from 'react-datepicker'; | |
import moment from 'moment'; | |
require('react-datepicker/dist/react-datepicker.css'); | |
const defaultProps = (props) => (classDef) => classDef.defaultProps = props; | |
@defaultProps({ | |
weekdays: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], | |
locale: 'en', | |
dateFormatCalendar: "MMMM YYYY", | |
moment: moment, | |
onChange: function() {}, | |
disabled: false | |
}) class CreditCardForm extends React.Component { | |
constructor(props, context) { | |
super(props, context); | |
this.state = { | |
focus: false, | |
selected: props.selected | |
}; | |
} | |
hideCalendar() { | |
setTimeout(() => { | |
this.setState({ | |
focus: false | |
}); | |
}, 0); | |
} | |
handleSelect(date) { | |
this.setSelected(date); | |
setTimeout(function(){ | |
this.hideCalendar(); | |
}.bind(this), 200); | |
} | |
setSelected(date) { | |
var moment = date.moment(); | |
this.props.onChange(moment); | |
this.setState({ | |
selected: moment | |
}); | |
} | |
calendar() { | |
if (this.state.focus) { | |
return (<Popover> | |
<Calendar | |
weekdays={this.props.weekdays} | |
locale={this.props.locale} | |
moment={this.props.moment} | |
dateFormat={this.props.dateFormatCalendar} | |
selected={this.state.selected} | |
onSelect={this.handleSelect} | |
hideCalendar={this.hideCalendar} | |
minDate={this.props.minDate} | |
maxDate={this.props.maxDate} | |
excludeDates={this.props.excludeDates} | |
weekStart={this.props.weekStart} /> | |
</Popover>); | |
} | |
} | |
render() { | |
return (<div> | |
<DateInput | |
name={this.props.name} | |
date={this.state.selected} | |
dateFormat={this.props.dateFormat} | |
focus={this.state.focus} | |
onFocus={this.handleFocus} | |
handleClick={this.onInputClick} | |
handleEnter={this.hideCalendar} | |
setSelected={this.setSelected} | |
clearSelected={this.clearSelected} | |
hideCalendar={this.hideCalendar} | |
placeholderText={this.props.placeholderText} | |
disabled={this.props.disabled} | |
className={this.props.className} /> | |
{this.props.disabled ? null : this.calendar()} | |
</div>); | |
} | |
}; | |
CreditCardForm.propTypes= { | |
weekdays: React.PropTypes.arrayOf(React.PropTypes.string), | |
locale: React.PropTypes.string, | |
dateFormatCalendar: React.PropTypes.string, | |
onChange: React.PropTypes.func.isRequired | |
}; | |
export default CreditCardForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment