Skip to content

Commit

Permalink
Validate input manually before creating date out of it
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmaj committed Nov 20, 2020
1 parent a3d257f commit 07b5c0f
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/DateInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ function findInput(element, property) {
return nextElement;
}

function isInputValid(input) {
if (!input.validity.valid) {
return false;
}

const { value } = input;

if (!value) {
return false;
}

const rawValue = Number(value);
const min = Number(input.getAttribute('min'));
const max = Number(input.getAttribute('max'));

if (rawValue < min || rawValue > max) {
return false;
}

return true;
}

function focus(element) {
if (element) {
element.focus();
Expand Down Expand Up @@ -420,9 +442,7 @@ export default class DateInput extends PureComponent {

if (formElements.every((formElement) => !formElement.value)) {
onChange(null, false);
} else if (
formElements.every((formElement) => formElement.value && formElement.validity.valid)
) {
} else if (formElements.every(isInputValid)) {
const year = Number(values.year);
const monthIndex = Number(values.month) - 1 || 0;
const day = Number(values.day || 1);
Expand Down

0 comments on commit 07b5c0f

Please sign in to comment.