Skip to content

Commit

Permalink
fix: passing files as value in change handler instead of just input v…
Browse files Browse the repository at this point in the history
…alue when input has files type

(cherry picked from commit 40f64b7)
  • Loading branch information
ZirionNeft committed Aug 2, 2021
1 parent 65fb681 commit 64df097
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions lib/create-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ function isCheckbox(element) {
return element.getAttribute && element.getAttribute('type') === 'checkbox';
}

export const createForm = config => {
function isFileInput(element) {
return element.getAttribute && element.getAttribute('type') === 'file';
}

function resolveValue(element) {
if (isFileInput(element)) {
return element.files;
} else if (isCheckbox(element)) {
return element.checked;
} else {
return element.value;
}
}

export const createForm = (config) => {
let initialValues = config.initialValues || {};

const validationSchema = config.validationSchema;
Expand All @@ -31,12 +45,14 @@ export const createForm = config => {
const isSubmitting = writable(false);
const isValidating = writable(false);

const isValid = derived(errors, $errors => {
const noErrors = util.getValues($errors).every(field => field === NO_ERROR);
const isValid = derived(errors, ($errors) => {
const noErrors = util
.getValues($errors)
.every((field) => field === NO_ERROR);
return noErrors;
});

const modified = derived(form, $form => {
const modified = derived(form, ($form) => {
const object = util.assignDeep($form, false);

for (let key in $form) {
Expand All @@ -46,14 +62,14 @@ export const createForm = config => {
return object;
});

const isModified = derived(modified, $modified => {
const isModified = derived(modified, ($modified) => {
return util.getValues($modified).includes(true);
});

function validateField(field) {
return util
.subscribeOnce(form)
.then(values => validateFieldValue(field, values[field]));
.then((values) => validateFieldValue(field, values[field]));
}

function validateFieldValue(field, value) {
Expand All @@ -65,7 +81,7 @@ export const createForm = config => {
return validationSchema
.validateAt(field, get(form))
.then(() => util.update(errors, field, ''))
.catch(error => util.update(errors, field, error.message))
.catch((error) => util.update(errors, field, error.message))
.finally(() => {
isValidating.set(false);
});
Expand All @@ -75,7 +91,7 @@ export const createForm = config => {
isValidating.set(true);
return Promise.resolve()
.then(() => validateFunction({[field]: value}))
.then(errs =>
.then((errs) =>
util.update(errors, field, !util.isNullish(errs) ? errs[field] : ''),
)
.finally(() => {
Expand All @@ -94,7 +110,7 @@ export const createForm = config => {
function handleChange(event) {
const element = event.target;
const field = element.name || element.id;
const value = isCheckbox(element) ? element.checked : element.value;
const value = resolveValue(element);

return updateValidateField(field, value);
}
Expand All @@ -106,13 +122,13 @@ export const createForm = config => {

isSubmitting.set(true);

return util.subscribeOnce(form).then(values => {
return util.subscribeOnce(form).then((values) => {
if (typeof validateFunction === 'function') {
isValidating.set(true);

return Promise.resolve()
.then(() => validateFunction(values))
.then(error => {
.then((error) => {
if (util.isNullish(error) || util.getValues(error).length === 0) {
clearErrorsAndSubmit(values);
} else {
Expand All @@ -131,11 +147,11 @@ export const createForm = config => {
.validate(values, {abortEarly: false})
.then(() => clearErrorsAndSubmit(values))
// eslint-disable-next-line unicorn/catch-error-name
.catch(yupErrors => {
.catch((yupErrors) => {
if (yupErrors && yupErrors.inner) {
const updatedErrors = getInitial.errors();

yupErrors.inner.map(error =>
yupErrors.inner.map((error) =>
util.set(updatedErrors, error.path, error.message),
);

Expand Down

0 comments on commit 64df097

Please sign in to comment.