@ilokesto/form 1.0.0 → 1.0.2

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.
@@ -1,24 +1,25 @@
1
- function p(e, t, r) {
1
+ import { C as a } from "./CreateForm-hQ4cxAK-.js";
2
+ function j(e, t, r) {
2
3
  const n = e.getFieldState(r);
3
- if (y(t, r))
4
+ if (b(t, r))
4
5
  return n;
5
- const u = h(e.getValues(), l(r));
6
+ const i = y(e.getValues(), l(r));
6
7
  return {
7
8
  ...n,
8
- value: u
9
+ value: i
9
10
  };
10
11
  }
11
12
  function l(e) {
12
13
  return typeof e == "string" ? [e] : e;
13
14
  }
14
- function y(e, t) {
15
- return a(t) in e.fields;
15
+ function b(e, t) {
16
+ return m(t) in e.fields;
16
17
  }
17
- function a(e) {
18
+ function m(e) {
18
19
  const t = l(e);
19
20
  return t.length === 0 ? "$" : JSON.stringify(t);
20
21
  }
21
- function h(e, t) {
22
+ function y(e, t) {
22
23
  let r = e;
23
24
  for (const n of t) {
24
25
  if (r == null || typeof r != "object" && typeof r != "function")
@@ -28,22 +29,22 @@ function h(e, t) {
28
29
  return r;
29
30
  }
30
31
  const d = /* @__PURE__ */ Symbol("skip change");
31
- function j(e, t, r, n) {
32
- const u = t.type ?? "text";
32
+ function x(e, t, r, n) {
33
+ const i = t.type ?? "text";
33
34
  return {
34
35
  name: n,
35
- type: u,
36
+ type: i,
36
37
  get value() {
37
- return m(u, r().value, i(t));
38
+ return v(i, r().value, u(t));
38
39
  },
39
40
  get checked() {
40
- return v(u, r().value, i(t));
41
+ return O(i, r().value, u(t));
41
42
  },
42
43
  input(c) {
43
- b(c, t) || f(e, t, r().value, c);
44
+ h(c, t) || s(e, t, r().value, c);
44
45
  },
45
46
  change(c) {
46
- f(e, t, r().value, c);
47
+ s(e, t, r().value, c);
47
48
  },
48
49
  blur() {
49
50
  e.blur(t.name);
@@ -53,48 +54,58 @@ function j(e, t, r, n) {
53
54
  }
54
55
  };
55
56
  }
56
- function S(e) {
57
+ function E(e) {
57
58
  return typeof e == "string" ? e : JSON.stringify(e);
58
59
  }
59
- function f(e, t, r, n) {
60
- const u = g(n, t, r);
61
- u !== d && e.setValue(t.name, u, { source: "user" });
60
+ function s(e, t, r, n) {
61
+ const i = S(n, t, r);
62
+ i !== d && e.setValue(t.name, i, { source: "user" });
62
63
  }
63
- function b(e, t) {
64
+ function h(e, t) {
64
65
  const r = t.type ?? e.type;
65
66
  return r === "checkbox" || r === "radio" || !!e.multiple;
66
67
  }
67
- function i(e) {
68
+ function u(e) {
68
69
  return e.value ?? e.checkedValue;
69
70
  }
70
- function m(e, t, r) {
71
+ function v(e, t, r) {
71
72
  return e === "checkbox" ? o(r) : e === "radio" ? o(r) ?? "" : o(t) ?? "";
72
73
  }
73
- function v(e, t, r) {
74
+ function O(e, t, r) {
74
75
  if (e === "checkbox")
75
76
  return r !== void 0 && Array.isArray(t) ? t.some((n) => Object.is(n, r)) : r !== void 0 ? Object.is(t, r) : !!t;
76
77
  if (e === "radio")
77
78
  return Object.is(t, r);
78
79
  }
79
- function g(e, t, r) {
80
+ function S(e, t, r) {
80
81
  const n = t.type ?? e.type;
81
- return n === "checkbox" ? O(e, t, r) : n === "radio" ? e.checked ? i(t) ?? e.value : d : e.multiple && e.selectedOptions ? [...e.selectedOptions].map((u) => u.value) : e.value;
82
+ return n === "checkbox" ? g(e, t, r) : n === "radio" ? e.checked ? u(t) ?? e.value : d : e.multiple && e.selectedOptions ? [...e.selectedOptions].map((i) => i.value) : e.value;
82
83
  }
83
- function O(e, t, r) {
84
+ function g(e, t, r) {
84
85
  if (t.checkedValue === void 0 && t.value === void 0)
85
86
  return !!e.checked;
86
87
  if (Array.isArray(r)) {
87
- const n = i(t);
88
- return e.checked ? r.some((u) => Object.is(u, n)) ? r : [...r, n] : r.filter((u) => !Object.is(u, n));
88
+ const n = u(t);
89
+ return e.checked ? r.some((i) => Object.is(i, n)) ? r : [...r, n] : r.filter((i) => !Object.is(i, n));
89
90
  }
90
- return e.checked ? i(t) : t.uncheckedValue ?? !1;
91
+ return e.checked ? u(t) : t.uncheckedValue ?? !1;
91
92
  }
92
93
  function o(e) {
93
94
  if (e != null)
94
95
  return typeof e == "string" || typeof e == "number" ? e : Array.isArray(e) ? e.map((t) => String(t)) : String(e);
95
96
  }
96
- function x(e) {
97
- const t = k(e), r = s(e, "dirty"), n = s(e, "touched");
97
+ function F(e, t, r) {
98
+ return async (n) => (n?.preventDefault?.(), e.submit(t, r));
99
+ }
100
+ function A(e) {
101
+ return "subscribe" in e && "submit" in e;
102
+ }
103
+ function C(e) {
104
+ const { values: t, resetOptions: r, ...n } = e;
105
+ return new a(n);
106
+ }
107
+ function B(e) {
108
+ const t = p(e), r = f(e, "dirty"), n = f(e, "touched");
98
109
  return {
99
110
  state: e,
100
111
  errors: t,
@@ -102,23 +113,28 @@ function x(e) {
102
113
  touchedFields: n,
103
114
  isDirty: Object.keys(r).length > 0,
104
115
  isValid: Object.keys(t).length === 0,
105
- isSubmitting: !1,
116
+ isSubmitting: e.isSubmitting,
117
+ isSubmitted: e.isSubmitted,
118
+ isSubmitSuccessful: e.isSubmitSuccessful,
106
119
  submitCount: e.submitCount
107
120
  };
108
121
  }
109
- function k(e) {
122
+ function p(e) {
110
123
  return Object.fromEntries(
111
124
  Object.entries(e.fields).filter(([, t]) => t.errors.length > 0).map(([t, r]) => [t, [...r.errors]])
112
125
  );
113
126
  }
114
- function s(e, t) {
127
+ function f(e, t) {
115
128
  return Object.fromEntries(
116
129
  Object.entries(e.fields).filter(([, r]) => r[t]).map(([r]) => [r, !0])
117
130
  );
118
131
  }
119
132
  export {
120
- x as a,
121
- j as c,
122
- S as f,
123
- p as g
133
+ C as a,
134
+ B as b,
135
+ x as c,
136
+ F as d,
137
+ E as f,
138
+ j as g,
139
+ A as i
124
140
  };
@@ -0,0 +1,8 @@
1
+ import { type CreateFormOptions, type Form, type ResetOptions } from '../core/index.js';
2
+ export type ReactiveFormOptions<TValues> = CreateFormOptions<TValues> & {
3
+ values?: TValues;
4
+ resetOptions?: ResetOptions;
5
+ };
6
+ export type FormInput<TValues> = Form<TValues> | ReactiveFormOptions<TValues>;
7
+ export declare function isFormInstance<TValues>(input: FormInput<TValues>): input is Form<TValues>;
8
+ export declare function createFormFromOptions<TValues>(options: ReactiveFormOptions<TValues>): Form<TValues>;
@@ -7,6 +7,8 @@ export type FormStateSummary<TValues> = {
7
7
  isDirty: boolean;
8
8
  isValid: boolean;
9
9
  isSubmitting: boolean;
10
+ isSubmitted: boolean;
11
+ isSubmitSuccessful: boolean;
10
12
  submitCount: number;
11
13
  };
12
14
  export declare function createFormStateSummary<TValues>(state: Readonly<FormState<TValues>>): FormStateSummary<TValues>;
@@ -0,0 +1,9 @@
1
+ import type { FieldState, Form } from '../../core/index.js';
2
+ import type { PathKey } from '../../core/types.js';
3
+ export type SubmitEventLike = {
4
+ preventDefault?: () => void;
5
+ };
6
+ export type SubmitValidHandler<TValues, TResult> = (values: TValues) => TResult | Promise<TResult>;
7
+ export type SubmitInvalidHandler = (fields: Readonly<Record<PathKey, FieldState>>) => void;
8
+ export type SubmitHandler<TEvent extends SubmitEventLike, TResult> = (event?: TEvent) => Promise<TResult | undefined>;
9
+ export declare function createSubmitHandler<TValues, TEvent extends SubmitEventLike, TResult>(form: Form<TValues>, onValid: SubmitValidHandler<TValues, TResult>, onInvalid?: SubmitInvalidHandler): SubmitHandler<TEvent, TResult>;
@@ -1,3 +1,5 @@
1
1
  export { getFieldState } from './FieldValue.js';
2
2
  export { createDomBinding, fieldPathToDomName, type DomBindingCore } from './RegisterBinding.js';
3
+ export { createSubmitHandler } from './SubmitHandler.js';
4
+ export type { SubmitEventLike, SubmitHandler, SubmitInvalidHandler, SubmitValidHandler } from './SubmitHandler.js';
3
5
  export type { DomValue, FieldEventTarget, FieldHookOptions, RegisterOptions } from './types.js';
@@ -1,4 +1,4 @@
1
- import type { CreateFormOptions, FieldPathInput, FieldSchemaOptions, FieldState, Form, FormArray, FormError, FormState, PathKey, SetValueOptions } from '../types.js';
1
+ import type { CreateFormOptions, FieldPathInput, FieldSchemaOptions, FieldState, Form, FormArray, FormError, FormState, PathKey, ResetOptions, SetValueOptions } from '../types.js';
2
2
  export declare class CreateForm<TValues> implements Form<TValues> {
3
3
  private readonly store;
4
4
  private readonly fields;
@@ -18,6 +18,6 @@ export declare class CreateForm<TValues> implements Form<TValues> {
18
18
  clearErrors(...paths: FieldPathInput[]): void;
19
19
  trigger(...paths: FieldPathInput[]): Promise<boolean>;
20
20
  array(path: FieldPathInput): FormArray;
21
- reset(values?: TValues): void;
21
+ reset(values?: TValues, options?: ResetOptions): void;
22
22
  submit<TResult>(onValid: (values: TValues) => TResult | Promise<TResult>, onInvalid?: (fields: Readonly<Record<PathKey, FieldState>>) => void): Promise<TResult | undefined>;
23
23
  }
@@ -1,2 +1,2 @@
1
1
  export { CreateForm } from './form/index.js';
2
- export type { CreateFormOptions, FieldSchemaOptions, FieldPathInput, FieldState, Form, FormArray, FormError, FormState, SetValueOptions, StandardSchemaV1, ValidationTrigger } from './types.js';
2
+ export type { CreateFormOptions, FieldSchemaOptions, FieldPathValue, FieldPathInput, FieldState, Form, FormArray, FormError, FormState, ResetOptions, SetValueOptions, StandardSchemaV1, ValidationTrigger } from './types.js';
@@ -1,6 +1,6 @@
1
1
  import type { FormState } from '../types.js';
2
2
  export declare class FormStateInitializer {
3
- static initialize<TValues>(initialValues: TValues): FormState<TValues>;
3
+ static initialize<TValues>(defaultValues: TValues): FormState<TValues>;
4
4
  private static isTraversableObject;
5
5
  private static createInitialArrayKeys;
6
6
  private static visitInitialValue;
@@ -1,10 +1,10 @@
1
- import type { FieldPath, FieldPathInput, FieldState, FormError, FormState, PathKey, SetValueOptions } from '../types.js';
1
+ import type { FieldPath, FieldPathInput, FieldState, FormError, FormState, PathKey, ResetOptions, SetValueOptions } from '../types.js';
2
2
  export declare class FormStateStore<TValues> {
3
3
  private readonly store;
4
4
  private readonly reader;
5
5
  private readonly writer;
6
6
  static getDefaultFieldState(): FieldState;
7
- constructor(initialValues: TValues);
7
+ constructor(defaultValues: TValues);
8
8
  getState(): Readonly<FormState<TValues>>;
9
9
  subscribe(listener: () => void): () => void;
10
10
  getKnownFieldPaths(): Record<PathKey, FieldPath>;
@@ -17,7 +17,8 @@ export declare class FormStateStore<TValues> {
17
17
  touchField(fieldKey: PathKey): void;
18
18
  setErrorsByKey(fieldKey: PathKey, errors: readonly FormError[]): void;
19
19
  clearErrors(fieldKeys?: readonly PathKey[]): void;
20
- reset(values?: TValues): void;
21
- incrementSubmitCount(): void;
20
+ reset(values?: TValues, options?: ResetOptions): void;
21
+ beginSubmit(): void;
22
+ completeSubmit(successful: boolean): void;
22
23
  replaceState(updater: (previousState: FormState<TValues>) => FormState<TValues>): void;
23
24
  }
@@ -1,5 +1,5 @@
1
1
  import type { Store } from '@ilokesto/store';
2
- import type { FieldPath, FormError, FormState, PathKey, SetValueOptions } from '../types.js';
2
+ import type { FieldPath, FormError, FormState, PathKey, ResetOptions, SetValueOptions } from '../types.js';
3
3
  type WritableFormStore<TValues> = Pick<Store<FormState<TValues>>, 'getState' | 'setState'>;
4
4
  export declare class FormStateWriter<TValues> {
5
5
  private readonly store;
@@ -8,8 +8,9 @@ export declare class FormStateWriter<TValues> {
8
8
  touchField(fieldKey: PathKey): void;
9
9
  setErrorsByKey(fieldKey: PathKey, errors: readonly FormError[]): void;
10
10
  clearErrors(fieldKeys?: readonly PathKey[]): void;
11
- reset(values?: TValues): void;
12
- incrementSubmitCount(): void;
11
+ reset(values?: TValues, options?: ResetOptions): void;
12
+ beginSubmit(): void;
13
+ completeSubmit(successful: boolean): void;
13
14
  replaceState(updater: (previousState: FormState<TValues>) => FormState<TValues>): void;
14
15
  private static isSameValue;
15
16
  }
@@ -47,19 +47,34 @@ export type FieldState<TValue = unknown> = {
47
47
  dirty: boolean;
48
48
  modified: boolean;
49
49
  };
50
+ export type FieldPathValue<TValues, TName extends FieldPathInput> = TName extends string ? StringFieldValue<TValues, TName> : TName extends FieldPath ? TuplePathValue<TValues, TName> : unknown;
51
+ type StringFieldValue<TValues, TName extends string> = TName extends keyof NonNullable<TValues> ? NonNullable<TValues>[TName] | MissingContainer<TValues> : unknown;
52
+ type TuplePathValue<TValue, TPath extends FieldPath> = TPath extends readonly [] ? TValue : TPath extends readonly [infer THead, ...infer TRest] ? THead extends FieldPathSegment ? TRest extends FieldPath ? SegmentValue<TValue, THead, TRest> : unknown : unknown : unknown;
53
+ type SegmentValue<TValue, THead extends FieldPathSegment, TRest extends FieldPath> = NonNullable<TValue> extends readonly unknown[] ? THead extends number ? TuplePathValue<ArrayElement<NonNullable<TValue>, THead>, TRest> | MissingContainer<TValue> : unknown : THead extends keyof NonNullable<TValue> ? TuplePathValue<NonNullable<TValue>[THead], TRest> | MissingContainer<TValue> : unknown;
54
+ type ArrayElement<TArray, TIndex extends number> = TArray extends readonly unknown[] ? TIndex extends keyof TArray ? TArray[TIndex] : TArray[number] : unknown;
55
+ type MissingContainer<TValue> = Extract<TValue, null | undefined> extends never ? never : undefined;
50
56
  export type ArrayKeys = Record<PathKey, string[]>;
51
57
  export type FormState<TValues> = {
52
- initialValues: TValues;
58
+ defaultValues: TValues;
53
59
  fields: Record<PathKey, FieldState>;
54
60
  submitCount: number;
61
+ isSubmitting: boolean;
62
+ isSubmitted: boolean;
63
+ isSubmitSuccessful: boolean;
55
64
  arrayKeys: ArrayKeys;
56
65
  };
57
66
  export type CreateFormOptions<TValues> = {
58
- initialValues: TValues;
67
+ defaultValues: TValues;
59
68
  schema?: StandardSchemaV1<unknown, TValues>;
60
69
  schemaOptions?: StandardSchemaV1.Options;
61
70
  validateOn?: readonly ValidationTrigger[];
62
71
  };
72
+ export type ResetOptions = {
73
+ keepDirtyValues?: boolean;
74
+ keepErrors?: boolean;
75
+ keepTouched?: boolean;
76
+ keepSubmitState?: boolean;
77
+ };
63
78
  export type FieldSchemaOptions = {
64
79
  schema: StandardSchemaV1<unknown, unknown>;
65
80
  schemaOptions?: StandardSchemaV1.Options;
@@ -78,7 +93,7 @@ export type Form<TValues> = {
78
93
  clearErrors(...paths: FieldPathInput[]): void;
79
94
  trigger(...paths: FieldPathInput[]): Promise<boolean>;
80
95
  array(path: FieldPathInput): FormArray;
81
- reset(values?: TValues): void;
96
+ reset(values?: TValues, options?: ResetOptions): void;
82
97
  submit<TResult>(onValid: (values: TValues) => TResult | Promise<TResult>, onInvalid?: (fields: Readonly<Record<PathKey, FieldState>>) => void): Promise<TResult | undefined>;
83
98
  };
84
99
  export type FormArray = {
@@ -94,3 +109,4 @@ export type SetValueOptions = {
94
109
  source?: 'user' | 'program';
95
110
  validate?: boolean;
96
111
  };
112
+ export {};
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { CreateForm, } from './core/index.js';
2
- export type { CreateFormOptions, FieldPathInput, Form, FormError, StandardSchemaV1, } from './core/index.js';
2
+ export type { CreateFormOptions, FieldPathInput, Form, FormError, ResetOptions, StandardSchemaV1, } from './core/index.js';