@steveesamson/microform 1.0.11 → 1.0.12

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/README.md CHANGED
@@ -41,6 +41,11 @@ const { form, values, errors, submit, sanity } = uform({
41
41
  validateEvent:'blur',
42
42
  // Configure validators here
43
43
  validators:{}
44
+ // Configurable wait time in millis for form field
45
+ // to initialize; default is 200 millis - needed for more involving fields.
46
+ fieldWaitTimeInMilliSecond: 200
47
+ // Default false, set to true to know if changes are happening.
48
+ debug: false
44
49
  }
45
50
  });
46
51
 
@@ -1,3 +1,3 @@
1
- import type { FormOptions, FormAction, FormValues, FormErrors, FormSanity } from './types.js';
1
+ import type { FormOptions, FormAction, FormValues, FormErrors } from './types.js';
2
2
  import type { Params } from './internal.svelte.js';
3
- export declare const formAction: (values: FormValues, errors: FormErrors, unfits: FormErrors, sanity: FormSanity, options: FormOptions, validationMap: Params) => FormAction;
3
+ export declare const formAction: (values: FormValues, errors: FormErrors, unfits: FormErrors, onSanity: (sanity: boolean) => void, options: FormOptions, validationMap: Params) => FormAction;
@@ -1,5 +1,5 @@
1
1
  import { useValidator } from './form-validators.js';
2
- import { getEditableContent } from './utils.js';
2
+ import { debounce, getEditableContent } from './utils.js';
3
3
  const isField = (node) => {
4
4
  return (node instanceof HTMLSelectElement ||
5
5
  node instanceof HTMLInputElement ||
@@ -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, sanity, options, validationMap) => {
22
+ export const formAction = (values, errors, unfits, onSanity, options, validationMap) => {
23
23
  const { validators: customValidators } = options;
24
24
  const { validate, validators } = useValidator(errors, values);
25
25
  // override
@@ -29,7 +29,29 @@ export const formAction = (values, errors, unfits, sanity, options, validationMa
29
29
  }
30
30
  }
31
31
  const hasError = (next) => !!next;
32
+ let canWatch = false; // $state(false);
33
+ const evaluateSanity = () => {
34
+ const { validate: validateUnfit } = useValidator(unfits, values, validators);
35
+ checkFormFitness(values, validationMap, validateUnfit);
36
+ const withErrors = Object.values(errors).some(hasError);
37
+ const withUnfits = Object.values(unfits).some(hasError);
38
+ onSanity(!withErrors && !withUnfits && canWatch);
39
+ };
40
+ $effect(() => {
41
+ if (options.debug) {
42
+ console.log('values changed...');
43
+ }
44
+ Object.values(values);
45
+ evaluateSanity();
46
+ });
47
+ const start = debounce(() => {
48
+ canWatch = true;
49
+ if (options.debug) {
50
+ console.log('microform initialized...');
51
+ }
52
+ }, options.fieldWaitTimeInMilliSecond);
32
53
  return (node, eventProps) => {
54
+ start();
33
55
  const nodeName = isField(node) ? node.name : '';
34
56
  const { name: dsname = nodeName } = node.dataset || {};
35
57
  const { name = dsname, validations = [], validateEvent = (options.validateEvent = 'blur'), html = false } = eventProps || {};
@@ -73,13 +95,6 @@ export const formAction = (values, errors, unfits, sanity, options, validationMa
73
95
  }
74
96
  validate({ name, value: values[name], validations, node });
75
97
  };
76
- $effect(() => {
77
- const { validate: validateUnfit } = useValidator(unfits, values, validators);
78
- checkFormFitness(values, validationMap, validateUnfit);
79
- const withErrors = Object.values(errors).some(hasError);
80
- const withUnfits = Object.values(unfits).some(hasError);
81
- sanity.ok = !withErrors && !withUnfits;
82
- });
83
98
  $effect(() => {
84
99
  node.addEventListener(validateEvent, updateNode);
85
100
  return () => {
@@ -1,22 +1,26 @@
1
1
  import { formAction } from './form-action.svelte.js';
2
- import { formState } from './internal.svelte.js';
3
- import { resetObject } from './utils.js';
2
+ import { formStore } from './internal.svelte.js';
4
3
  const microform = (props) => {
5
4
  // form default values
6
- const data = props?.data || {};
7
- // form state
8
- const state = formState(data);
9
- const validationMap = {};
10
- const { options = {
5
+ // const data = props?.data || {};
6
+ const defaultOptions = {
11
7
  validateEvent: 'blur',
12
- validators: {}
13
- } } = props || {};
14
- const form = formAction(state.values, state.errors, state.unfits, state.sanity, options, validationMap);
8
+ validators: {},
9
+ fieldWaitTimeInMilliSecond: 200,
10
+ debug: false
11
+ };
12
+ const { options: userOptions = {}, data = {} } = props || {};
13
+ const options = { ...defaultOptions, ...userOptions };
14
+ // form state
15
+ const { values, errors, unfits, reset, sanity, validationMap } = formStore(data);
16
+ const form = formAction(values, errors, unfits, (_sanity) => {
17
+ sanity.ok = _sanity;
18
+ }, options, validationMap);
15
19
  const handleSubmit = (e, handler) => {
16
20
  e.preventDefault();
17
- if (!state.sanity.ok)
21
+ if (!sanity.ok)
18
22
  return;
19
- handler({ ...state.values });
23
+ handler({ ...values });
20
24
  };
21
25
  const onsubmit = (handler) => {
22
26
  return (e) => {
@@ -32,26 +36,10 @@ const microform = (props) => {
32
36
  return () => formNode.removeEventListener('submit', localHandler);
33
37
  });
34
38
  };
35
- const reset = () => {
36
- resetObject(state.values, data);
37
- resetObject(state.errors);
38
- resetObject(state.unfits);
39
- state.sanity.ok = false;
40
- for (const [name, { nodeRef, html }] of Object.entries(validationMap).filter(([, { nodeRef }]) => !!nodeRef)) {
41
- if (nodeRef) {
42
- if (nodeRef.isContentEditable) {
43
- nodeRef[html ? 'innerHTML' : 'textContent'] = data[name] || '';
44
- }
45
- else {
46
- nodeRef['value'] = data[name] || '';
47
- }
48
- }
49
- }
50
- };
51
39
  return {
52
- values: state.values,
53
- errors: state.errors,
54
- sanity: state.sanity,
40
+ values,
41
+ errors,
42
+ sanity,
55
43
  form,
56
44
  submit,
57
45
  onsubmit,
@@ -9,4 +9,8 @@ export type FormState = {
9
9
  sanity: FormSanity;
10
10
  unfits: FormErrors;
11
11
  };
12
- export declare const formState: (data: Params) => FormState;
12
+ export type FormStore = FormState & {
13
+ validationMap: Params;
14
+ reset: () => void;
15
+ };
16
+ export declare const formStore: (data: Params) => FormStore;
@@ -1,15 +1,43 @@
1
- export const formState = (data) => {
2
- // form values
3
- const values = $state({ ...data });
4
- // external form errors
5
- const errors = $state({});
6
- const sanity = $state({ ok: false });
7
- // internal checks
8
- const unfits = $state({});
1
+ import { resetObject } from "./utils.js";
2
+ export const formStore = (data) => {
3
+ const validationMap = {};
4
+ // form state
5
+ const state = $state({
6
+ values: { ...data },
7
+ errors: {},
8
+ unfits: {},
9
+ sanity: { ok: false }
10
+ });
9
11
  return {
10
- values,
11
- errors,
12
- sanity,
13
- unfits,
12
+ validationMap,
13
+ get values() {
14
+ return state.values;
15
+ },
16
+ get errors() {
17
+ return state.errors;
18
+ },
19
+ get unfits() {
20
+ return state.unfits;
21
+ },
22
+ get sanity() {
23
+ return state.sanity;
24
+ },
25
+ reset() {
26
+ resetObject(state.values, data);
27
+ resetObject(state.errors);
28
+ resetObject(state.unfits);
29
+ state.sanity.ok = false;
30
+ const nodeEntries = Object.entries(validationMap).filter(([, { nodeRef }]) => !!nodeRef);
31
+ for (const [name, { nodeRef, html }] of nodeEntries) {
32
+ if (nodeRef) {
33
+ if (nodeRef.isContentEditable) {
34
+ nodeRef[html ? 'innerHTML' : 'textContent'] = data[name] || '';
35
+ }
36
+ else {
37
+ nodeRef['value'] = data[name] || '';
38
+ }
39
+ }
40
+ }
41
+ }
14
42
  };
15
43
  };
package/dist/types.d.ts CHANGED
@@ -44,6 +44,8 @@ export type FormSubmit = (_data: Params) => (void | Promise<void>);
44
44
  export type FormOptions = {
45
45
  validateEvent?: ValidateEvent;
46
46
  validators?: Partial<ValidatorMap<ValidatorType>>;
47
+ fieldWaitTimeInMilliSecond?: number;
48
+ debug?: boolean;
47
49
  };
48
50
  export type MicroFormProps = {
49
51
  data?: Params;
package/dist/utils.d.ts CHANGED
@@ -10,4 +10,5 @@ export declare const getEditableContent: (e: TEvent, isHtml: boolean) => {
10
10
  export declare const makeName: (str: string) => string;
11
11
  export declare const isValidFileSize: (node: HTMLInputElement | undefined, maxFileSizeInMB: number) => string;
12
12
  export declare const resetObject: (target: FormValues, data?: Params | undefined) => void;
13
+ export declare const debounce: (func: (...args?: any) => any, delay?: number) => (...par?: any) => void;
13
14
  export {};
package/dist/utils.js CHANGED
@@ -61,3 +61,11 @@ export const resetObject = (target, data = undefined) => {
61
61
  }
62
62
  }
63
63
  };
64
+ export const debounce = (func, delay = 1000) => {
65
+ let timeoutId;
66
+ return function (...par) {
67
+ const context = this;
68
+ clearTimeout(timeoutId);
69
+ timeoutId = setTimeout(() => func.apply(context, par), delay);
70
+ };
71
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steveesamson/microform",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "postbuild": "touch ./docs/.nojekyll",