Created
November 26, 2018 10:43
-
-
Save rla/652d87a2f93f24b6a400d2154fc9715e to your computer and use it in GitHub Desktop.
React-Flatpickr integration
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
const pickr = require('../common/pickr'); | |
// ... | |
const OrderForm = FormHoc(class extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { }; | |
this.setLoadingDateInput = this.setLoadingDateInput.bind(this); | |
this.loadingDateInput = null; | |
this.loadingDateCalendar = null; | |
} | |
setLoadingDateInput(e) { | |
if (this.loadingDateCalendar) { | |
this.loadingDateCalendar.destroy(); | |
} | |
this.loadingDateInput = e; | |
this.loadingDateCalendar = pickr(this.loadingDateInput, (selectedDates, date) => { | |
this.props.valueChange('loading_date', date); | |
}); | |
} | |
componentWillReceiveProps(props) { | |
const date = props.values.loading_date; | |
if (date !== this.props.values.loading_date) { | |
// things that depend on date | |
} | |
} | |
render() { | |
const { | |
errors, | |
values, | |
inputChange | |
} = this.props; | |
return ( | |
<form className='vt-form-full'> | |
<TextInput | |
name='loading_date' | |
id='order-loading_date' | |
label='Laadimise kuupäev' | |
placeholder='Laadimise kuupäev' | |
value={values.loading_date} | |
error={errors.loading_date} | |
onChange={inputChange} | |
inputRef={this.setLoadingDateInput} | |
inputClass='vt-form-control-date'/> | |
</form> | |
); | |
} | |
}); |
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
// Helper to construct a flatpickr instance. | |
module.exports = (element, fn) => { | |
return flatpickr(element, { | |
dateFormat: 'd.m.Y', | |
locale: 'et', | |
onChange: fn | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment