react-form-with-constraints
Version:
Simple form validation for React
37 lines (36 loc) • 1.25 kB
JavaScript
export class IValidityState {
constructor(validity) {
this.badInput = validity.badInput;
this.customError = validity.customError;
this.patternMismatch = validity.patternMismatch;
this.rangeOverflow = validity.rangeOverflow;
this.rangeUnderflow = validity.rangeUnderflow;
this.stepMismatch = validity.stepMismatch;
this.tooLong = validity.tooLong;
this.tooShort = validity.tooShort;
this.typeMismatch = validity.typeMismatch;
this.valid = validity.valid;
this.valueMissing = validity.valueMissing;
}
}
export function isHTMLInput(input) {
return input.props === undefined;
}
export class InputElement {
constructor(input) {
if (isHTMLInput(input)) {
this.name = input.name;
this.type = input.type;
this.value = input.value;
this.validity = new IValidityState(input.validity);
this.validationMessage = input.validationMessage;
}
else {
this.name = input.props.name;
this.type = undefined;
this.value = input.props.value;
this.validity = undefined;
this.validationMessage = undefined;
}
}
}