mirror of
https://github.com/pinry/pinry.git
synced 2025-11-17 18:30:39 +01:00
Feature: Add board creation
+ Refactor form-error settings and reset function
This commit is contained in:
committed by
Isaac Bythewood
parent
b3a065a4fb
commit
41cac8e784
52
pinry-spa/src/components/utils/form.js
Normal file
52
pinry-spa/src/components/utils/form.js
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
function createFormModel(fields) {
|
||||
const form = {};
|
||||
fields.forEach(
|
||||
(fieldName) => {
|
||||
form[fieldName] = {
|
||||
value: null,
|
||||
error: null,
|
||||
type: null,
|
||||
};
|
||||
},
|
||||
);
|
||||
return form;
|
||||
}
|
||||
|
||||
|
||||
function FormHelper(form, fields = []) {
|
||||
const self = form;
|
||||
function resetField(fieldName) {
|
||||
self[fieldName].type = 'is-info';
|
||||
self[fieldName].error = null;
|
||||
}
|
||||
function markFieldAsDanger(fieldName, errorMsg) {
|
||||
self[fieldName].error = errorMsg;
|
||||
self[fieldName].type = 'is-danger';
|
||||
}
|
||||
function resetAllFields() {
|
||||
fields.forEach(
|
||||
(fieldName) => {
|
||||
resetField(fieldName);
|
||||
},
|
||||
);
|
||||
}
|
||||
return {
|
||||
form,
|
||||
fields,
|
||||
markFieldAsDanger,
|
||||
resetField,
|
||||
resetAllFields,
|
||||
};
|
||||
}
|
||||
|
||||
function fromFields(fields) {
|
||||
const form = createFormModel(fields);
|
||||
return FormHelper(form, fields);
|
||||
}
|
||||
|
||||
export default {
|
||||
createFormModel,
|
||||
FormHelper,
|
||||
fromFields,
|
||||
};
|
||||
Reference in New Issue
Block a user