@ilokesto/form 1.0.2 → 1.0.3
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/adapters/FormInput.d.ts +1 -1
- package/dist/adapters/FormInput.js +10 -0
- package/dist/adapters/FormStateSummary.d.ts +1 -1
- package/dist/adapters/FormStateSummary.js +27 -0
- package/dist/adapters/dom/FieldValue.d.ts +1 -1
- package/dist/adapters/dom/FieldValue.js +34 -0
- package/dist/adapters/dom/RegisterBinding.d.ts +2 -2
- package/dist/adapters/dom/RegisterBinding.js +110 -0
- package/dist/adapters/dom/SubmitHandler.d.ts +2 -2
- package/dist/adapters/dom/SubmitHandler.js +6 -0
- package/dist/adapters/dom/index.d.ts +5 -5
- package/dist/adapters/dom/index.js +3 -0
- package/dist/adapters/dom/types.d.ts +1 -1
- package/dist/adapters/dom/types.js +1 -0
- package/dist/core/array/ArrayItemReorder.js +24 -0
- package/dist/core/array/ArrayKeyGenerator.js +7 -0
- package/dist/core/array/FormArrayController.d.ts +3 -3
- package/dist/core/array/FormArrayController.js +54 -0
- package/dist/core/array/FormArrayFactory.d.ts +2 -2
- package/dist/core/array/FormArrayFactory.js +13 -0
- package/dist/core/array/FormArrayMutationPlanner.d.ts +1 -1
- package/dist/core/array/FormArrayMutationPlanner.js +68 -0
- package/dist/core/array/FormArrayPath.d.ts +1 -1
- package/dist/core/array/FormArrayPath.js +11 -0
- package/dist/core/array/FormArrayRebaser.d.ts +4 -4
- package/dist/core/array/FormArrayRebaser.js +54 -0
- package/dist/core/array/index.d.ts +1 -1
- package/dist/core/array/index.js +1 -0
- package/dist/core/form/CreateForm.d.ts +1 -1
- package/dist/core/form/CreateForm.js +64 -0
- package/dist/core/form/FormFieldCommands.d.ts +3 -3
- package/dist/core/form/FormFieldCommands.js +40 -0
- package/dist/core/form/FormSubmitter.d.ts +3 -3
- package/dist/core/form/FormSubmitter.js +26 -0
- package/dist/core/form/index.d.ts +1 -1
- package/dist/core/form/index.js +1 -0
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +1 -0
- package/dist/core/path/FormPath.d.ts +1 -1
- package/dist/core/path/FormPath.js +36 -0
- package/dist/core/path/index.d.ts +1 -1
- package/dist/core/path/index.js +1 -0
- package/dist/core/state/FieldStateFactory.d.ts +1 -1
- package/dist/core/state/FieldStateFactory.js +19 -0
- package/dist/core/state/FormStateInitializer.d.ts +1 -1
- package/dist/core/state/FormStateInitializer.js +45 -0
- package/dist/core/state/FormStateReader.d.ts +1 -1
- package/dist/core/state/FormStateReader.js +30 -0
- package/dist/core/state/FormStateStore.d.ts +1 -1
- package/dist/core/state/FormStateStore.js +66 -0
- package/dist/core/state/FormStateWriter.d.ts +1 -1
- package/dist/core/state/FormStateWriter.js +107 -0
- package/dist/core/state/index.d.ts +1 -1
- package/dist/core/state/index.js +1 -0
- package/dist/core/types.js +1 -0
- package/dist/core/validation/StandardSchemaValidator.d.ts +1 -1
- package/dist/core/validation/StandardSchemaValidator.js +63 -0
- package/dist/core/validation/ValidationEngine.d.ts +2 -2
- package/dist/core/validation/ValidationEngine.js +116 -0
- package/dist/core/validation/index.d.ts +1 -1
- package/dist/core/validation/index.js +1 -0
- package/dist/core/value/ValueHelper.d.ts +1 -1
- package/dist/core/value/ValueHelper.js +66 -0
- package/dist/core/value/index.d.ts +1 -1
- package/dist/core/value/index.js +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -4
- package/dist/react/RegisterBinding.d.ts +3 -3
- package/dist/react/RegisterBinding.js +21 -0
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.js +1 -91
- package/dist/react/types.d.ts +3 -3
- package/dist/react/types.js +1 -0
- package/dist/react/useField.d.ts +2 -2
- package/dist/react/useField.js +23 -0
- package/dist/react/useFieldSchemaRegistration.d.ts +2 -2
- package/dist/react/useFieldSchemaRegistration.js +25 -0
- package/dist/react/useFieldState.d.ts +2 -2
- package/dist/react/useFieldState.js +6 -0
- package/dist/react/useForm.d.ts +2 -2
- package/dist/react/useForm.js +30 -0
- package/dist/react/useFormSnapshot.d.ts +1 -1
- package/dist/react/useFormSnapshot.js +4 -0
- package/dist/react/useFormState.d.ts +2 -2
- package/dist/react/useFormState.js +7 -0
- package/dist/react/useRegister.d.ts +2 -2
- package/dist/react/useRegister.js +26 -0
- package/dist/solid/RegisterBinding.d.ts +3 -3
- package/dist/solid/RegisterBinding.js +28 -0
- package/dist/solid/index.d.ts +2 -2
- package/dist/solid/index.js +1 -117
- package/dist/solid/types.d.ts +3 -3
- package/dist/solid/types.js +1 -0
- package/dist/solid/useField.d.ts +2 -2
- package/dist/solid/useField.js +28 -0
- package/dist/solid/useFieldSchemaRegistration.d.ts +2 -2
- package/dist/solid/useFieldSchemaRegistration.js +15 -0
- package/dist/solid/useForm.d.ts +2 -2
- package/dist/solid/useForm.js +13 -0
- package/dist/solid/useFormSnapshot.d.ts +1 -1
- package/dist/solid/useFormSnapshot.js +9 -0
- package/dist/solid/useFormState.d.ts +2 -2
- package/dist/solid/useFormState.js +38 -0
- package/dist/solid/useRegister.d.ts +2 -2
- package/dist/solid/useRegister.js +21 -0
- package/dist/svelte/RegisterAction.d.ts +2 -2
- package/dist/svelte/RegisterAction.js +121 -0
- package/dist/svelte/index.d.ts +2 -2
- package/dist/svelte/index.js +1 -111
- package/dist/svelte/types.d.ts +4 -4
- package/dist/svelte/types.js +1 -0
- package/dist/svelte/useForm.d.ts +2 -2
- package/dist/svelte/useForm.js +17 -0
- package/dist/svelte/useFormState.d.ts +2 -2
- package/dist/svelte/useFormState.js +10 -0
- package/dist/vue/RegisterBinding.d.ts +3 -3
- package/dist/vue/RegisterBinding.js +28 -0
- package/dist/vue/index.d.ts +2 -2
- package/dist/vue/index.js +1 -117
- package/dist/vue/types.d.ts +3 -3
- package/dist/vue/types.js +1 -0
- package/dist/vue/useField.d.ts +2 -2
- package/dist/vue/useField.js +28 -0
- package/dist/vue/useFieldSchemaRegistration.d.ts +2 -2
- package/dist/vue/useFieldSchemaRegistration.js +17 -0
- package/dist/vue/useForm.d.ts +2 -2
- package/dist/vue/useForm.js +13 -0
- package/dist/vue/useFormSnapshot.d.ts +1 -1
- package/dist/vue/useFormSnapshot.js +11 -0
- package/dist/vue/useFormState.d.ts +2 -2
- package/dist/vue/useFormState.js +38 -0
- package/dist/vue/useRegister.d.ts +2 -2
- package/dist/vue/useRegister.js +21 -0
- package/package.json +2 -2
- package/dist/CreateForm-hQ4cxAK-.js +0 -1283
- package/dist/FormStateSummary-gmEQdNXj.js +0 -140
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getCurrentScope, onScopeDispose } from 'vue';
|
|
2
|
+
export function useFieldSchemaRegistrations(form, options) {
|
|
3
|
+
const cleanups = options.flatMap(option => {
|
|
4
|
+
if (!option.schema) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
return [form.registerFieldSchema(option.name, { schema: option.schema, schemaOptions: option.schemaOptions })];
|
|
8
|
+
});
|
|
9
|
+
if (cleanups.length === 0) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (getCurrentScope()) {
|
|
13
|
+
onScopeDispose(() => {
|
|
14
|
+
cleanups.forEach(cleanup => cleanup());
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
package/dist/vue/useForm.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CreateFormOptions, Form } from '../core/index
|
|
2
|
-
import type { VueForm } from './types
|
|
1
|
+
import type { CreateFormOptions, Form } from '../core/index';
|
|
2
|
+
import type { VueForm } from './types';
|
|
3
3
|
export declare function useForm<TValues>(form: Form<TValues>): VueForm<TValues>;
|
|
4
4
|
export declare function useForm<TValues>(options: CreateFormOptions<TValues>): VueForm<TValues>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createSubmitHandler } from '../adapters/dom';
|
|
2
|
+
import { createFormFromOptions, isFormInstance } from '../adapters/FormInput';
|
|
3
|
+
import { useFieldWithForm } from './useField';
|
|
4
|
+
import { useFormStateWithForm } from './useFormState';
|
|
5
|
+
import { useRegisterWithForm } from './useRegister';
|
|
6
|
+
export function useForm(input) {
|
|
7
|
+
const form = isFormInstance(input) ? input : createFormFromOptions(input);
|
|
8
|
+
const useRegister = ((first, ...rest) => useRegisterWithForm(form, first, ...rest));
|
|
9
|
+
const useField = ((options) => useFieldWithForm(form, options));
|
|
10
|
+
const useFormState = () => useFormStateWithForm(form);
|
|
11
|
+
const handleSubmit = ((onValid, onInvalid) => createSubmitHandler(form, onValid, onInvalid));
|
|
12
|
+
return { form, useRegister, useField, useFormState, handleSubmit };
|
|
13
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getCurrentScope, onScopeDispose, shallowRef } from 'vue';
|
|
2
|
+
export function useFormSnapshot(form) {
|
|
3
|
+
const snapshot = shallowRef(form.getState());
|
|
4
|
+
const unsubscribe = form.subscribe(() => {
|
|
5
|
+
snapshot.value = form.getState();
|
|
6
|
+
});
|
|
7
|
+
if (getCurrentScope()) {
|
|
8
|
+
onScopeDispose(unsubscribe);
|
|
9
|
+
}
|
|
10
|
+
return snapshot;
|
|
11
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { Form } from '../core/index
|
|
2
|
-
import type { VueFormStateReturn } from './types
|
|
1
|
+
import type { Form } from '../core/index';
|
|
2
|
+
import type { VueFormStateReturn } from './types';
|
|
3
3
|
export declare function useFormStateWithForm<TValues>(form: Form<TValues>): VueFormStateReturn<TValues>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createFormStateSummary } from '../adapters/FormStateSummary';
|
|
2
|
+
import { useFormSnapshot } from './useFormSnapshot';
|
|
3
|
+
export function useFormStateWithForm(form) {
|
|
4
|
+
const snapshot = useFormSnapshot(form);
|
|
5
|
+
const getSummary = () => createFormStateSummary(snapshot.value);
|
|
6
|
+
return {
|
|
7
|
+
get state() {
|
|
8
|
+
return snapshot.value;
|
|
9
|
+
},
|
|
10
|
+
get errors() {
|
|
11
|
+
return getSummary().errors;
|
|
12
|
+
},
|
|
13
|
+
get dirtyFields() {
|
|
14
|
+
return getSummary().dirtyFields;
|
|
15
|
+
},
|
|
16
|
+
get touchedFields() {
|
|
17
|
+
return getSummary().touchedFields;
|
|
18
|
+
},
|
|
19
|
+
get isDirty() {
|
|
20
|
+
return getSummary().isDirty;
|
|
21
|
+
},
|
|
22
|
+
get isValid() {
|
|
23
|
+
return getSummary().isValid;
|
|
24
|
+
},
|
|
25
|
+
get isSubmitting() {
|
|
26
|
+
return getSummary().isSubmitting;
|
|
27
|
+
},
|
|
28
|
+
get isSubmitted() {
|
|
29
|
+
return getSummary().isSubmitted;
|
|
30
|
+
},
|
|
31
|
+
get isSubmitSuccessful() {
|
|
32
|
+
return getSummary().isSubmitSuccessful;
|
|
33
|
+
},
|
|
34
|
+
get submitCount() {
|
|
35
|
+
return getSummary().submitCount;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Form } from '../core/index
|
|
2
|
-
import type { RegisterOptions, VueRegisterElement, VueRegisterPropsForElement, VueRegisterPropsList } from './types
|
|
1
|
+
import type { Form } from '../core/index';
|
|
2
|
+
import type { RegisterOptions, VueRegisterElement, VueRegisterPropsForElement, VueRegisterPropsList } from './types';
|
|
3
3
|
export declare function useRegisterWithForm<TValues, TElement extends VueRegisterElement = HTMLInputElement>(form: Form<TValues>, options: RegisterOptions): VueRegisterPropsForElement<TElement>;
|
|
4
4
|
export declare function useRegisterWithForm<TValues, TElement extends VueRegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(form: Form<TValues>, options: TOptions): VueRegisterPropsList<TElement, TOptions>;
|
|
5
5
|
export declare function useRegisterWithForm<TValues, TElement extends VueRegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(form: Form<TValues>, ...options: TOptions): VueRegisterPropsList<TElement, TOptions>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getFieldState } from '../adapters/dom';
|
|
2
|
+
import { createRegisterProps, fieldPathToDomName } from './RegisterBinding';
|
|
3
|
+
import { useFieldSchemaRegistrations } from './useFieldSchemaRegistration';
|
|
4
|
+
import { useFormSnapshot } from './useFormSnapshot';
|
|
5
|
+
export function useRegisterWithForm(form, first, ...rest) {
|
|
6
|
+
const shouldReturnList = Array.isArray(first) || rest.length > 0;
|
|
7
|
+
const options = normalizeRegisterOptions(first, rest);
|
|
8
|
+
useFieldSchemaRegistrations(form, options);
|
|
9
|
+
const snapshot = useFormSnapshot(form);
|
|
10
|
+
const bindings = options.map(option => createRegisterProps(form, option, () => getFieldState(form, snapshot.value, option.name), fieldPathToDomName(option.name)));
|
|
11
|
+
return shouldReturnList ? bindings : bindings[0];
|
|
12
|
+
}
|
|
13
|
+
function normalizeRegisterOptions(first, rest) {
|
|
14
|
+
if (Array.isArray(first)) {
|
|
15
|
+
return first;
|
|
16
|
+
}
|
|
17
|
+
if (rest.length > 0) {
|
|
18
|
+
return [first, ...rest];
|
|
19
|
+
}
|
|
20
|
+
return [first];
|
|
21
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ilokesto/form",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -86,7 +86,7 @@
|
|
|
86
86
|
"access": "public"
|
|
87
87
|
},
|
|
88
88
|
"scripts": {
|
|
89
|
-
"build": "rm -rf dist && tsc
|
|
89
|
+
"build": "rm -rf dist && tsc",
|
|
90
90
|
"typecheck": "tsc --noEmit",
|
|
91
91
|
"test": "vitest run"
|
|
92
92
|
}
|