How i get if the date-picker is valid ?
Use native Form data validation.
@wojtekmaj while your answer guides in the right direction it still lack a lot of implementation details.
I have a scenario where I'm validating max/min date and as long as the user selects the date using the UI the validation works correctly however if the user manually input an invalid date then the UI visually reflects an invalid date using a red background however on the internal component state we don't have any reference as whether the date is currently invalid or not all we have is the last valid selected date. (Please correct me if I'm wrong)
So in order to know at any giving point if the date is currently valid I tried something like this:
// initial state
state = {
dateValue: null,
isDateValid: false,
}
.....
// onChange
handleDateChange = (selectedDate) => {
/* Everytime we enter here is because the date-picker has already validated
that the selectedDate is valid so we can assume the date is valid */
this.setState({
dateValue: new Date(selectedDate),
isDateValid: true
});
}
....
const dayCssClassSelector: string = 'react-date-picker__button__input__day'
const monthCssClassSelector: string = 'react-date-picker__button__input__month';
const yearCssClassSelector: string = 'react-date-picker__button__input__year';
// Just in case we have more than one datetime picker on the same UI
const baseDatePickerCssClass: string = 'forecast-date';
....
// When redenring the component
<DatePicker
value={this.state.dateValue}
maxDate={maximumDate}
minDate={minimumDate}
onChange={this.handleDateChange}
className={this.baseDatePickerCssClass}
/>
componenetDidMount() {
// Check addInvalidListener implementation below
this.addInvalidListener(this.dayCssClassSelector);
this.addInvalidListener(this.monthCssClassSelector);
this.addInvalidListener(this.yearCssClassSelector);
}
addInvalidListener = (cssClass) => {
let input = document.querySelector(`.${this.baseDatePickerCssClass} .${cssClass}`);
if (input)
input.addEventListener('invalid', this.handleInvalidUserInput);
}
handleInvalidUserInput = () => {
this.setState({ isDateValid: false })
}
If you know of a simpler way please let me know, finding a way to expose if the date is valid will definitely be helpful, Is this repository still being maintained? I might try to do a pull request to add support for this? did you already have an idea for this?
Interesting approach @Luis-Palacios!
One thing to mention is that React-Date-Picker has also a hidden date field, which is updated only with valid values. So if you're getting change event on one of the visible fields, but not on the hidden one, this also can mean the input is invalid or incomplete. This is because the native date field simply wouldn't accept such values as February 31st, but in React-Date-Picker's UI this might happen if you change the month afterwards.
Yest, this repository is maintained. I don't have an idea on how to make this idea into useful feature 馃
I'm running into a similar issue as @Luis-Palacios . The DatePicker (Calendar) prevents users from clicking anything beyond the minDate and maxDate properly, but if someone types in a date outside of the range, the onChange event is never fired.
This means when the user submits the form and validation happens, the value of the DatePicker element is either:
Is there a current way to handle something like this? My suggestion would be to still fire the onChange event even for invalid dates, but with some value other than null. Maybe false or an actual Invalid Date object?
Most helpful comment
I'm running into a similar issue as @Luis-Palacios . The DatePicker (Calendar) prevents users from clicking anything beyond the minDate and maxDate properly, but if someone types in a date outside of the range, the onChange event is never fired.
This means when the user submits the form and validation happens, the value of the DatePicker element is either:
Is there a current way to handle something like this? My suggestion would be to still fire the onChange event even for invalid dates, but with some value other than null. Maybe
falseor an actual Invalid Date object?