svelte-forms-lib
Advanced tools
Comparing version
@@ -699,3 +699,17 @@ 'use strict'; | ||
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; | ||
} | ||
} | ||
const createForm = (config) => { | ||
let initialValues = config.initialValues || {}; | ||
@@ -723,8 +737,10 @@ | ||
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); | ||
@@ -739,3 +755,3 @@ | ||
const isModified = derived(modified, $modified => { | ||
const isModified = derived(modified, ($modified) => { | ||
return util.getValues($modified).includes(true); | ||
@@ -747,3 +763,3 @@ }); | ||
.subscribeOnce(form) | ||
.then(values => validateFieldValue(field, values[field])); | ||
.then((values) => validateFieldValue(field, values[field])); | ||
} | ||
@@ -760,3 +776,3 @@ | ||
.then(() => util.update(errors, field, '')) | ||
.catch(error => util.update(errors, field, error.message)) | ||
.catch((error) => util.update(errors, field, error.message)) | ||
.finally(() => { | ||
@@ -771,3 +787,3 @@ isValidating.set(false); | ||
.then(() => validateFunction({[field]: value})) | ||
.then(errs => | ||
.then((errs) => | ||
util.update(errors, field, !util.isNullish(errs) ? errs[field] : ''), | ||
@@ -791,3 +807,3 @@ ) | ||
const field = element.name || element.id; | ||
const value = isCheckbox(element) ? element.checked : element.value; | ||
const value = resolveValue(element); | ||
@@ -804,3 +820,3 @@ return updateValidateField(field, value); | ||
return util.subscribeOnce(form).then(values => { | ||
return util.subscribeOnce(form).then((values) => { | ||
if (typeof validateFunction === 'function') { | ||
@@ -811,3 +827,3 @@ isValidating.set(true); | ||
.then(() => validateFunction(values)) | ||
.then(error => { | ||
.then((error) => { | ||
if (util.isNullish(error) || util.getValues(error).length === 0) { | ||
@@ -831,7 +847,7 @@ 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), | ||
@@ -838,0 +854,0 @@ ); |
@@ -11,3 +11,17 @@ import {derived, writable, get} from 'svelte/store'; | ||
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 || {}; | ||
@@ -35,8 +49,10 @@ | ||
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); | ||
@@ -51,3 +67,3 @@ | ||
const isModified = derived(modified, $modified => { | ||
const isModified = derived(modified, ($modified) => { | ||
return util.getValues($modified).includes(true); | ||
@@ -59,3 +75,3 @@ }); | ||
.subscribeOnce(form) | ||
.then(values => validateFieldValue(field, values[field])); | ||
.then((values) => validateFieldValue(field, values[field])); | ||
} | ||
@@ -72,3 +88,3 @@ | ||
.then(() => util.update(errors, field, '')) | ||
.catch(error => util.update(errors, field, error.message)) | ||
.catch((error) => util.update(errors, field, error.message)) | ||
.finally(() => { | ||
@@ -83,3 +99,3 @@ isValidating.set(false); | ||
.then(() => validateFunction({[field]: value})) | ||
.then(errs => | ||
.then((errs) => | ||
util.update(errors, field, !util.isNullish(errs) ? errs[field] : ''), | ||
@@ -103,3 +119,3 @@ ) | ||
const field = element.name || element.id; | ||
const value = isCheckbox(element) ? element.checked : element.value; | ||
const value = resolveValue(element); | ||
@@ -116,3 +132,3 @@ return updateValidateField(field, value); | ||
return util.subscribeOnce(form).then(values => { | ||
return util.subscribeOnce(form).then((values) => { | ||
if (typeof validateFunction === 'function') { | ||
@@ -123,3 +139,3 @@ isValidating.set(true); | ||
.then(() => validateFunction(values)) | ||
.then(error => { | ||
.then((error) => { | ||
if (util.isNullish(error) || util.getValues(error).length === 0) { | ||
@@ -143,7 +159,7 @@ 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), | ||
@@ -150,0 +166,0 @@ ); |
@@ -6,3 +6,3 @@ /// <reference lib="svelte2tsx" /> | ||
export type FormProps<Inf = Record<string, any>> = { | ||
export type FormProps<Inf = Record<string, unknown>> = { | ||
context?: FormState; | ||
@@ -58,3 +58,3 @@ initialValues?: Inf; | ||
}>; | ||
handleChange: () => void; | ||
handleChange: () => any; | ||
handleSubmit: () => any; | ||
@@ -72,3 +72,3 @@ }; | ||
FormProps, | ||
{}, | ||
Record<string, unknown>, | ||
{ | ||
@@ -79,9 +79,17 @@ default: FormState; | ||
declare class Field extends SvelteComponentTyped<FieldProps, {}, {}> {} | ||
declare class Field extends SvelteComponentTyped< | ||
FieldProps, | ||
Record<string, unknown>, | ||
Record<string, unknown> | ||
> {} | ||
declare class Textarea extends SvelteComponentTyped<TextareaProps, {}, {}> {} | ||
declare class Textarea extends SvelteComponentTyped< | ||
TextareaProps, | ||
Record<string, unknown>, | ||
Record<string, unknown> | ||
> {} | ||
declare class Select extends SvelteComponentTyped< | ||
SelectProps, | ||
{}, | ||
Record<string, unknown>, | ||
{default: any} | ||
@@ -92,3 +100,3 @@ > {} | ||
ErrorProps, | ||
{}, | ||
Record<string, unknown>, | ||
{default: any} | ||
@@ -95,0 +103,0 @@ > {} |
@@ -1,3 +0,2 @@ | ||
import {key} from './components/key'; | ||
export {key} from './components/key'; | ||
export {createForm} from './create-form'; | ||
@@ -10,2 +9,1 @@ | ||
export {default as ErrorMessage} from './components/ErrorMessage.svelte'; | ||
export {key}; |
{ | ||
"name": "svelte-forms-lib", | ||
"version": "1.10.8", | ||
"version": "1.10.9", | ||
"description": "Svelte forms lib - A lightweight library for managing forms in Svelte v3", | ||
@@ -54,2 +54,4 @@ "typings": "./lib/index.d.ts", | ||
"@types/yup": "0.29.13", | ||
"@typescript-eslint/eslint-plugin": "^5.3.0", | ||
"@typescript-eslint/parser": "^5.3.0", | ||
"chance": "1.1.7", | ||
@@ -62,3 +64,3 @@ "commitizen": "^4.2.4", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-unicorn": "^34.0.1", | ||
"eslint-plugin-unicorn": "^38.0.0", | ||
"husky": "^7.0.0", | ||
@@ -65,0 +67,0 @@ "jest": "^26.6.3", |
Sorry, the diff of this file is not supported yet
101157
1.18%5657
4.72%3083
1.62%42
5%