cleanup types and added isValid function to controls
This commit is contained in:
@@ -101,18 +101,20 @@ const defaultFormControlValidation: FormControlValidation = {
|
|||||||
result: defaultValidationResult,
|
result: defaultValidationResult,
|
||||||
};
|
};
|
||||||
|
|
||||||
type FormClearValidations = () => void;
|
type FormControlClearValidations = () => void;
|
||||||
type FormSetValidation = (
|
type FormControlSetValidation = (
|
||||||
valid: boolean,
|
valid: boolean,
|
||||||
message?: string | Array<string>
|
message?: string | Array<string>
|
||||||
) => ValidationResult;
|
) => ValidationResult;
|
||||||
|
type FormControlIsValid = () => boolean;
|
||||||
|
|
||||||
interface FormControlObject {
|
export interface FormControlObject {
|
||||||
value: string;
|
value: string;
|
||||||
validate: () => ValidationResult;
|
validate: () => ValidationResult;
|
||||||
validation: FormControlValidation;
|
validation: FormControlValidation;
|
||||||
clearValidations: FormClearValidations;
|
clearValidations: FormControlClearValidations;
|
||||||
setValidationResult: FormSetValidation;
|
setValidationResult: FormControlSetValidation;
|
||||||
|
isValid: FormControlIsValid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* eslint-disable indent */
|
/* eslint-disable indent */
|
||||||
@@ -135,11 +137,17 @@ export const FormControl = (
|
|||||||
setValidationResult: createValidationResult,
|
setValidationResult: createValidationResult,
|
||||||
validate: function () {
|
validate: function () {
|
||||||
if (this.validation.validator) {
|
if (this.validation.validator) {
|
||||||
return this.validation.validator(this.value);
|
this.validation.result = this.validation.validator.validate(
|
||||||
|
this.value
|
||||||
|
);
|
||||||
|
return this.validation.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return defaultValidationResult;
|
return defaultValidationResult;
|
||||||
},
|
},
|
||||||
|
isValid: function () {
|
||||||
|
return this.validation.result.valid;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
/* eslint-enable indent */
|
/* eslint-enable indent */
|
||||||
|
|||||||
Reference in New Issue
Block a user