@steveesamson/microform 1.0.10 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/form-action.svelte.d.ts +2 -2
- package/dist/form-action.svelte.js +6 -9
- package/dist/index.svelte.js +12 -19
- package/dist/types.d.ts +2 -2
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +18 -7
- package/package.json +1 -1
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { FormOptions, FormAction, FormValues, FormErrors } from './types.js';
|
|
1
|
+
import type { FormOptions, FormAction, FormValues, FormErrors, FormSanity } from './types.js';
|
|
2
2
|
import type { Params } from './internal.svelte.js';
|
|
3
|
-
export declare const formAction: (values: FormValues, errors: FormErrors, unfits: FormErrors,
|
|
3
|
+
export declare const formAction: (values: FormValues, errors: FormErrors, unfits: FormErrors, sanity: FormSanity, options: FormOptions, validationMap: Params) => FormAction;
|
|
@@ -19,7 +19,7 @@ const checkFormFitness = (values, validationMap, validate) => {
|
|
|
19
19
|
validate({ name, value: values[name], validations });
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
|
-
export const formAction = (values, errors, unfits,
|
|
22
|
+
export const formAction = (values, errors, unfits, sanity, options, validationMap) => {
|
|
23
23
|
const { validators: customValidators } = options;
|
|
24
24
|
const { validate, validators } = useValidator(errors, values);
|
|
25
25
|
// override
|
|
@@ -45,11 +45,7 @@ export const formAction = (values, errors, unfits, reportDirty, options, validat
|
|
|
45
45
|
node.innerHTML = defValue;
|
|
46
46
|
}
|
|
47
47
|
values[name] = defValue;
|
|
48
|
-
let eventBound = $state(false);
|
|
49
48
|
const updateNode = (e) => {
|
|
50
|
-
if (!eventBound) {
|
|
51
|
-
eventBound = true;
|
|
52
|
-
}
|
|
53
49
|
if (isField(node) && !isExcluded(node)) {
|
|
54
50
|
const value = e.target.value || '';
|
|
55
51
|
values[name] = value;
|
|
@@ -75,14 +71,15 @@ export const formAction = (values, errors, unfits, reportDirty, options, validat
|
|
|
75
71
|
const { value: fvalue } = node;
|
|
76
72
|
values[name] = fvalue;
|
|
77
73
|
}
|
|
74
|
+
validate({ name, value: values[name], validations, node });
|
|
75
|
+
};
|
|
76
|
+
$effect(() => {
|
|
78
77
|
const { validate: validateUnfit } = useValidator(unfits, values, validators);
|
|
79
78
|
checkFormFitness(values, validationMap, validateUnfit);
|
|
80
|
-
validate({ name, value: values[name], validations, node });
|
|
81
79
|
const withErrors = Object.values(errors).some(hasError);
|
|
82
80
|
const withUnfits = Object.values(unfits).some(hasError);
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
};
|
|
81
|
+
sanity.ok = !withErrors && !withUnfits;
|
|
82
|
+
});
|
|
86
83
|
$effect(() => {
|
|
87
84
|
node.addEventListener(validateEvent, updateNode);
|
|
88
85
|
return () => {
|
package/dist/index.svelte.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { formAction } from './form-action.svelte.js';
|
|
2
2
|
import { formState } from './internal.svelte.js';
|
|
3
|
+
import { resetObject } from './utils.js';
|
|
3
4
|
const microform = (props) => {
|
|
4
5
|
// form default values
|
|
5
6
|
const data = props?.data || {};
|
|
@@ -10,10 +11,7 @@ const microform = (props) => {
|
|
|
10
11
|
validateEvent: 'blur',
|
|
11
12
|
validators: {}
|
|
12
13
|
} } = props || {};
|
|
13
|
-
const
|
|
14
|
-
state.sanity.ok = isOk;
|
|
15
|
-
};
|
|
16
|
-
const form = formAction(state.values, state.errors, state.unfits, updateSanity, options, validationMap);
|
|
14
|
+
const form = formAction(state.values, state.errors, state.unfits, state.sanity, options, validationMap);
|
|
17
15
|
const handleSubmit = (e, handler) => {
|
|
18
16
|
e.preventDefault();
|
|
19
17
|
if (!state.sanity.ok)
|
|
@@ -21,28 +19,23 @@ const microform = (props) => {
|
|
|
21
19
|
handler({ ...state.values });
|
|
22
20
|
};
|
|
23
21
|
const onsubmit = (handler) => {
|
|
24
|
-
|
|
22
|
+
return (e) => {
|
|
25
23
|
handleSubmit(e, handler);
|
|
26
24
|
};
|
|
27
|
-
return onSubmit;
|
|
28
25
|
};
|
|
29
26
|
const submit = (formNode, handler) => {
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
$effect(() => {
|
|
28
|
+
const localHandler = (e) => {
|
|
29
|
+
handleSubmit(e, handler);
|
|
30
|
+
};
|
|
31
|
+
formNode.addEventListener('submit', localHandler);
|
|
32
|
+
return () => formNode.removeEventListener('submit', localHandler);
|
|
32
33
|
});
|
|
33
34
|
};
|
|
34
35
|
const reset = () => {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
state.values[key] = data[key];
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
delete state.values[key];
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
state.errors = {};
|
|
45
|
-
state.unfits = {};
|
|
36
|
+
resetObject(state.values, data);
|
|
37
|
+
resetObject(state.errors);
|
|
38
|
+
resetObject(state.unfits);
|
|
46
39
|
state.sanity.ok = false;
|
|
47
40
|
for (const [name, { nodeRef, html }] of Object.entries(validationMap).filter(([, { nodeRef }]) => !!nodeRef)) {
|
|
48
41
|
if (nodeRef) {
|
package/dist/types.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ export type FormAction = (node: HTMLElement, eventProps?: ActionOptions) => void
|
|
|
40
40
|
export type FormSubmitEvent = SubmitEvent & {
|
|
41
41
|
currentTarget: EventTarget & HTMLFormElement;
|
|
42
42
|
};
|
|
43
|
-
export type FormSubmit = (_data: Params) => void;
|
|
43
|
+
export type FormSubmit = (_data: Params) => (void | Promise<void>);
|
|
44
44
|
export type FormOptions = {
|
|
45
45
|
validateEvent?: ValidateEvent;
|
|
46
46
|
validators?: Partial<ValidatorMap<ValidatorType>>;
|
|
@@ -58,7 +58,7 @@ export type MicroFormReturn = {
|
|
|
58
58
|
sanity: FormSanity;
|
|
59
59
|
form: (node: HTMLElement, eventProps?: ActionOptions) => void;
|
|
60
60
|
submit: (formNode: HTMLFormElement, handler: FormSubmit) => void;
|
|
61
|
-
onsubmit: (handler: FormSubmit) => (e: Event) =>
|
|
61
|
+
onsubmit: (handler: FormSubmit) => (e: Event) => void;
|
|
62
62
|
reset: () => void;
|
|
63
63
|
};
|
|
64
64
|
export type Microform = (props?: MicroFormProps) => MicroFormReturn;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Params } from "./internal.svelte.js";
|
|
2
|
+
import type { FormValues } from "./types.js";
|
|
1
3
|
type TEvent = {
|
|
2
4
|
target: HTMLElement;
|
|
3
5
|
};
|
|
@@ -7,4 +9,5 @@ export declare const getEditableContent: (e: TEvent, isHtml: boolean) => {
|
|
|
7
9
|
};
|
|
8
10
|
export declare const makeName: (str: string) => string;
|
|
9
11
|
export declare const isValidFileSize: (node: HTMLInputElement | undefined, maxFileSizeInMB: number) => string;
|
|
12
|
+
export declare const resetObject: (target: FormValues, data?: Params | undefined) => void;
|
|
10
13
|
export {};
|
package/dist/utils.js
CHANGED
|
@@ -43,10 +43,21 @@ export const isValidFileSize = (node, maxFileSizeInMB) => {
|
|
|
43
43
|
}
|
|
44
44
|
return '';
|
|
45
45
|
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
export const resetObject = (target, data = undefined) => {
|
|
47
|
+
if (data) {
|
|
48
|
+
const defaultKeys = Object.keys({ ...data });
|
|
49
|
+
for (const [key,] of Object.entries(target)) {
|
|
50
|
+
if (defaultKeys.includes(key)) {
|
|
51
|
+
target[key] = data[key];
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
delete target[key];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
for (const [key,] of Object.entries(target)) {
|
|
60
|
+
target[key] = '';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|