@bquery/bquery 1.5.0 → 1.7.0
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 +193 -23
- package/dist/a11y/announce.d.ts +43 -0
- package/dist/a11y/announce.d.ts.map +1 -0
- package/dist/a11y/audit.d.ts +42 -0
- package/dist/a11y/audit.d.ts.map +1 -0
- package/dist/a11y/index.d.ts +53 -0
- package/dist/a11y/index.d.ts.map +1 -0
- package/dist/a11y/media-preferences.d.ts +77 -0
- package/dist/a11y/media-preferences.d.ts.map +1 -0
- package/dist/a11y/roving-tab-index.d.ts +38 -0
- package/dist/a11y/roving-tab-index.d.ts.map +1 -0
- package/dist/a11y/skip-link.d.ts +37 -0
- package/dist/a11y/skip-link.d.ts.map +1 -0
- package/dist/a11y/trap-focus.d.ts +49 -0
- package/dist/a11y/trap-focus.d.ts.map +1 -0
- package/dist/a11y/types.d.ts +152 -0
- package/dist/a11y/types.d.ts.map +1 -0
- package/dist/a11y-C5QOVvRn.js +421 -0
- package/dist/a11y-C5QOVvRn.js.map +1 -0
- package/dist/a11y.es.mjs +14 -0
- package/dist/component/component.d.ts +13 -5
- package/dist/component/component.d.ts.map +1 -1
- package/dist/component/html.d.ts +40 -3
- package/dist/component/html.d.ts.map +1 -1
- package/dist/component/index.d.ts +3 -2
- package/dist/component/index.d.ts.map +1 -1
- package/dist/component/library.d.ts.map +1 -1
- package/dist/component/scope.d.ts +138 -0
- package/dist/component/scope.d.ts.map +1 -0
- package/dist/component/types.d.ts +184 -17
- package/dist/component/types.d.ts.map +1 -1
- package/dist/component-CuuTijA6.js +684 -0
- package/dist/component-CuuTijA6.js.map +1 -0
- package/dist/component.es.mjs +10 -6
- package/dist/{config-DRmZZno3.js → config-BW35FKuA.js} +4 -4
- package/dist/config-BW35FKuA.js.map +1 -0
- package/dist/constraints-3lV9yyBw.js +100 -0
- package/dist/constraints-3lV9yyBw.js.map +1 -0
- package/dist/core/collection.d.ts +48 -0
- package/dist/core/collection.d.ts.map +1 -1
- package/dist/core/element.d.ts +92 -0
- package/dist/core/element.d.ts.map +1 -1
- package/dist/core/env.d.ts +18 -0
- package/dist/core/env.d.ts.map +1 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/shared.d.ts +8 -0
- package/dist/core/shared.d.ts.map +1 -1
- package/dist/core/utils/index.d.ts +52 -41
- package/dist/core/utils/index.d.ts.map +1 -1
- package/dist/core-Cjl7GUu8.js +717 -0
- package/dist/core-Cjl7GUu8.js.map +1 -0
- package/dist/{core-DPdbItcq.js → core-DnlyjbF2.js} +1 -1
- package/dist/{core-DPdbItcq.js.map → core-DnlyjbF2.js.map} +1 -1
- package/dist/core.es.mjs +45 -44
- package/dist/custom-directives-7wAShnnd.js +9 -0
- package/dist/custom-directives-7wAShnnd.js.map +1 -0
- package/dist/devtools/devtools.d.ts +212 -0
- package/dist/devtools/devtools.d.ts.map +1 -0
- package/dist/devtools/index.d.ts +20 -0
- package/dist/devtools/index.d.ts.map +1 -0
- package/dist/devtools/types.d.ts +69 -0
- package/dist/devtools/types.d.ts.map +1 -0
- package/dist/devtools-D2fQLhDN.js +122 -0
- package/dist/devtools-D2fQLhDN.js.map +1 -0
- package/dist/devtools.es.mjs +19 -0
- package/dist/dnd/draggable.d.ts +51 -0
- package/dist/dnd/draggable.d.ts.map +1 -0
- package/dist/dnd/droppable.d.ts +38 -0
- package/dist/dnd/droppable.d.ts.map +1 -0
- package/dist/dnd/index.d.ts +47 -0
- package/dist/dnd/index.d.ts.map +1 -0
- package/dist/dnd/sortable.d.ts +43 -0
- package/dist/dnd/sortable.d.ts.map +1 -0
- package/dist/dnd/types.d.ts +250 -0
- package/dist/dnd/types.d.ts.map +1 -0
- package/dist/dnd-B8EgyzaI.js +244 -0
- package/dist/dnd-B8EgyzaI.js.map +1 -0
- package/dist/dnd.es.mjs +6 -0
- package/dist/env-NeVmr4Gf.js +19 -0
- package/dist/env-NeVmr4Gf.js.map +1 -0
- package/dist/forms/create-form.d.ts +49 -0
- package/dist/forms/create-form.d.ts.map +1 -0
- package/dist/forms/index.d.ts +39 -0
- package/dist/forms/index.d.ts.map +1 -0
- package/dist/forms/types.d.ts +139 -0
- package/dist/forms/types.d.ts.map +1 -0
- package/dist/forms/validators.d.ts +179 -0
- package/dist/forms/validators.d.ts.map +1 -0
- package/dist/forms-C3yovgH9.js +141 -0
- package/dist/forms-C3yovgH9.js.map +1 -0
- package/dist/forms.es.mjs +14 -0
- package/dist/full.d.ts +37 -9
- package/dist/full.d.ts.map +1 -1
- package/dist/full.es.mjs +186 -91
- package/dist/full.iife.js +47 -31
- package/dist/full.iife.js.map +1 -1
- package/dist/full.umd.js +47 -31
- package/dist/full.umd.js.map +1 -1
- package/dist/i18n/formatting.d.ts +40 -0
- package/dist/i18n/formatting.d.ts.map +1 -0
- package/dist/i18n/i18n.d.ts +48 -0
- package/dist/i18n/i18n.d.ts.map +1 -0
- package/dist/i18n/index.d.ts +57 -0
- package/dist/i18n/index.d.ts.map +1 -0
- package/dist/i18n/translate.d.ts +83 -0
- package/dist/i18n/translate.d.ts.map +1 -0
- package/dist/i18n/types.d.ts +156 -0
- package/dist/i18n/types.d.ts.map +1 -0
- package/dist/i18n-BnnhTFOS.js +89 -0
- package/dist/i18n-BnnhTFOS.js.map +1 -0
- package/dist/i18n.es.mjs +6 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.mjs +233 -138
- package/dist/media/battery.d.ts +35 -0
- package/dist/media/battery.d.ts.map +1 -0
- package/dist/media/breakpoints.d.ts +51 -0
- package/dist/media/breakpoints.d.ts.map +1 -0
- package/dist/media/clipboard.d.ts +30 -0
- package/dist/media/clipboard.d.ts.map +1 -0
- package/dist/media/device-sensors.d.ts +54 -0
- package/dist/media/device-sensors.d.ts.map +1 -0
- package/dist/media/geolocation.d.ts +38 -0
- package/dist/media/geolocation.d.ts.map +1 -0
- package/dist/media/index.d.ts +42 -0
- package/dist/media/index.d.ts.map +1 -0
- package/dist/media/media-query.d.ts +36 -0
- package/dist/media/media-query.d.ts.map +1 -0
- package/dist/media/network.d.ts +35 -0
- package/dist/media/network.d.ts.map +1 -0
- package/dist/media/types.d.ts +173 -0
- package/dist/media/types.d.ts.map +1 -0
- package/dist/media/viewport.d.ts +32 -0
- package/dist/media/viewport.d.ts.map +1 -0
- package/dist/media-Di2Ta22s.js +340 -0
- package/dist/media-Di2Ta22s.js.map +1 -0
- package/dist/media.es.mjs +12 -0
- package/dist/motion/index.d.ts +7 -3
- package/dist/motion/index.d.ts.map +1 -1
- package/dist/motion/morph.d.ts +27 -0
- package/dist/motion/morph.d.ts.map +1 -0
- package/dist/motion/parallax.d.ts +30 -0
- package/dist/motion/parallax.d.ts.map +1 -0
- package/dist/motion/reduced-motion.d.ts +36 -3
- package/dist/motion/reduced-motion.d.ts.map +1 -1
- package/dist/motion/types.d.ts +58 -0
- package/dist/motion/types.d.ts.map +1 -1
- package/dist/motion/typewriter.d.ts +31 -0
- package/dist/motion/typewriter.d.ts.map +1 -0
- package/dist/motion-qPj_TYGv.js +530 -0
- package/dist/motion-qPj_TYGv.js.map +1 -0
- package/dist/motion.es.mjs +27 -23
- package/dist/mount-SM07RUa6.js +403 -0
- package/dist/mount-SM07RUa6.js.map +1 -0
- package/dist/{object-qGpWr6-J.js → object-BCk-1c8T.js} +5 -4
- package/dist/{object-qGpWr6-J.js.map → object-BCk-1c8T.js.map} +1 -1
- package/dist/{platform-B7JhGBc7.js → platform-CPbCprb6.js} +3 -3
- package/dist/platform-CPbCprb6.js.map +1 -0
- package/dist/platform.es.mjs +2 -2
- package/dist/plugin/index.d.ts +22 -0
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/registry.d.ts +108 -0
- package/dist/plugin/registry.d.ts.map +1 -0
- package/dist/plugin/types.d.ts +110 -0
- package/dist/plugin/types.d.ts.map +1 -0
- package/dist/plugin-cPoOHFLY.js +64 -0
- package/dist/plugin-cPoOHFLY.js.map +1 -0
- package/dist/plugin.es.mjs +9 -0
- package/dist/reactive/computed.d.ts +7 -0
- package/dist/reactive/computed.d.ts.map +1 -1
- package/dist/reactive-Cfv0RK6x.js +233 -0
- package/dist/reactive-Cfv0RK6x.js.map +1 -0
- package/dist/reactive.es.mjs +18 -17
- package/dist/registry-CWf368tT.js +26 -0
- package/dist/registry-CWf368tT.js.map +1 -0
- package/dist/router/bq-link.d.ts +112 -0
- package/dist/router/bq-link.d.ts.map +1 -0
- package/dist/router/constraints.d.ts +9 -0
- package/dist/router/constraints.d.ts.map +1 -0
- package/dist/router/index.d.ts +14 -6
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/match.d.ts +0 -1
- package/dist/router/match.d.ts.map +1 -1
- package/dist/router/path-pattern.d.ts +14 -0
- package/dist/router/path-pattern.d.ts.map +1 -0
- package/dist/router/query.d.ts.map +1 -1
- package/dist/router/router.d.ts +3 -1
- package/dist/router/router.d.ts.map +1 -1
- package/dist/router/types.d.ts +48 -4
- package/dist/router/types.d.ts.map +1 -1
- package/dist/router/use-route.d.ts +50 -0
- package/dist/router/use-route.d.ts.map +1 -0
- package/dist/router/utils.d.ts +3 -0
- package/dist/router/utils.d.ts.map +1 -1
- package/dist/router-BrthaP_z.js +473 -0
- package/dist/router-BrthaP_z.js.map +1 -0
- package/dist/router.es.mjs +13 -10
- package/dist/{sanitize-jyJ2ryE2.js → sanitize-B1V4JswB.js} +95 -83
- package/dist/sanitize-B1V4JswB.js.map +1 -0
- package/dist/security/index.d.ts +2 -0
- package/dist/security/index.d.ts.map +1 -1
- package/dist/security/sanitize.d.ts +4 -1
- package/dist/security/sanitize.d.ts.map +1 -1
- package/dist/security/trusted-html.d.ts +53 -0
- package/dist/security/trusted-html.d.ts.map +1 -0
- package/dist/security.es.mjs +10 -9
- package/dist/ssr/hydrate.d.ts +65 -0
- package/dist/ssr/hydrate.d.ts.map +1 -0
- package/dist/ssr/index.d.ts +59 -0
- package/dist/ssr/index.d.ts.map +1 -0
- package/dist/ssr/render.d.ts +62 -0
- package/dist/ssr/render.d.ts.map +1 -0
- package/dist/ssr/serialize.d.ts +118 -0
- package/dist/ssr/serialize.d.ts.map +1 -0
- package/dist/ssr/types.d.ts +70 -0
- package/dist/ssr/types.d.ts.map +1 -0
- package/dist/ssr-B2qd_WBB.js +248 -0
- package/dist/ssr-B2qd_WBB.js.map +1 -0
- package/dist/ssr.es.mjs +9 -0
- package/dist/store/create-store.d.ts.map +1 -1
- package/dist/store/define-store.d.ts +1 -1
- package/dist/store/define-store.d.ts.map +1 -1
- package/dist/store/index.d.ts +1 -1
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/mapping.d.ts +1 -1
- package/dist/store/mapping.d.ts.map +1 -1
- package/dist/store/persisted.d.ts +38 -4
- package/dist/store/persisted.d.ts.map +1 -1
- package/dist/store/types.d.ts +140 -3
- package/dist/store/types.d.ts.map +1 -1
- package/dist/store/utils.d.ts +2 -2
- package/dist/store/utils.d.ts.map +1 -1
- package/dist/store/watch.d.ts +1 -1
- package/dist/store/watch.d.ts.map +1 -1
- package/dist/store-DWpyH6p5.js +338 -0
- package/dist/store-DWpyH6p5.js.map +1 -0
- package/dist/store.es.mjs +11 -10
- package/dist/storybook/index.d.ts +37 -0
- package/dist/storybook/index.d.ts.map +1 -0
- package/dist/storybook.es.mjs +151 -0
- package/dist/storybook.es.mjs.map +1 -0
- package/dist/testing/index.d.ts +23 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/testing.d.ts +156 -0
- package/dist/testing/testing.d.ts.map +1 -0
- package/dist/testing/types.d.ts +134 -0
- package/dist/testing/types.d.ts.map +1 -0
- package/dist/testing-CsqjNUyy.js +224 -0
- package/dist/testing-CsqjNUyy.js.map +1 -0
- package/dist/testing.es.mjs +9 -0
- package/dist/type-guards-Do9DWgNp.js +44 -0
- package/dist/type-guards-Do9DWgNp.js.map +1 -0
- package/dist/untrack-DJVQQ2WM.js +33 -0
- package/dist/untrack-DJVQQ2WM.js.map +1 -0
- package/dist/view/custom-directives.d.ts +20 -0
- package/dist/view/custom-directives.d.ts.map +1 -0
- package/dist/view/evaluate.d.ts.map +1 -1
- package/dist/view/process.d.ts.map +1 -1
- package/dist/view.es.mjs +11 -10
- package/package.json +52 -11
- package/src/a11y/announce.ts +131 -0
- package/src/a11y/audit.ts +314 -0
- package/src/a11y/index.ts +68 -0
- package/src/a11y/media-preferences.ts +255 -0
- package/src/a11y/roving-tab-index.ts +164 -0
- package/src/a11y/skip-link.ts +255 -0
- package/src/a11y/trap-focus.ts +184 -0
- package/src/a11y/types.ts +183 -0
- package/src/component/component.ts +345 -65
- package/src/component/html.ts +153 -53
- package/src/component/index.ts +12 -2
- package/src/component/library.ts +66 -28
- package/src/component/scope.ts +212 -0
- package/src/component/types.ts +238 -19
- package/src/core/collection.ts +707 -628
- package/src/core/element.ts +981 -774
- package/src/core/env.ts +60 -0
- package/src/core/index.ts +49 -48
- package/src/core/shared.ts +62 -13
- package/src/core/utils/index.ts +148 -83
- package/src/devtools/devtools.ts +410 -0
- package/src/devtools/index.ts +48 -0
- package/src/devtools/types.ts +104 -0
- package/src/dnd/draggable.ts +296 -0
- package/src/dnd/droppable.ts +228 -0
- package/src/dnd/index.ts +62 -0
- package/src/dnd/sortable.ts +307 -0
- package/src/dnd/types.ts +293 -0
- package/src/forms/create-form.ts +278 -0
- package/src/forms/index.ts +65 -0
- package/src/forms/types.ts +154 -0
- package/src/forms/validators.ts +265 -0
- package/src/full.ts +260 -3
- package/src/i18n/formatting.ts +67 -0
- package/src/i18n/i18n.ts +200 -0
- package/src/i18n/index.ts +67 -0
- package/src/i18n/translate.ts +182 -0
- package/src/i18n/types.ts +171 -0
- package/src/index.ts +108 -36
- package/src/media/battery.ts +116 -0
- package/src/media/breakpoints.ts +131 -0
- package/src/media/clipboard.ts +80 -0
- package/src/media/device-sensors.ts +158 -0
- package/src/media/geolocation.ts +119 -0
- package/src/media/index.ts +76 -0
- package/src/media/media-query.ts +92 -0
- package/src/media/network.ts +115 -0
- package/src/media/types.ts +177 -0
- package/src/media/viewport.ts +84 -0
- package/src/motion/index.ts +57 -48
- package/src/motion/morph.ts +151 -0
- package/src/motion/parallax.ts +120 -0
- package/src/motion/reduced-motion.ts +66 -17
- package/src/motion/transition.ts +97 -97
- package/src/motion/types.ts +63 -0
- package/src/motion/typewriter.ts +164 -0
- package/src/platform/announcer.ts +208 -208
- package/src/platform/config.ts +163 -163
- package/src/platform/cookies.ts +165 -165
- package/src/platform/index.ts +39 -39
- package/src/platform/meta.ts +168 -168
- package/src/plugin/index.ts +37 -0
- package/src/plugin/registry.ts +269 -0
- package/src/plugin/types.ts +137 -0
- package/src/reactive/async-data.ts +486 -486
- package/src/reactive/computed.ts +130 -92
- package/src/reactive/index.ts +37 -37
- package/src/reactive/signal.ts +29 -29
- package/src/router/bq-link.ts +279 -0
- package/src/router/constraints.ts +201 -0
- package/src/router/index.ts +49 -41
- package/src/router/match.ts +312 -106
- package/src/router/path-pattern.ts +52 -0
- package/src/router/query.ts +38 -35
- package/src/router/router.ts +402 -211
- package/src/router/types.ts +139 -93
- package/src/router/use-route.ts +68 -0
- package/src/router/utils.ts +157 -116
- package/src/security/constants.ts +211 -211
- package/src/security/index.ts +12 -10
- package/src/security/sanitize.ts +6 -2
- package/src/security/trusted-html.ts +71 -0
- package/src/ssr/hydrate.ts +82 -0
- package/src/ssr/index.ts +70 -0
- package/src/ssr/render.ts +508 -0
- package/src/ssr/serialize.ts +296 -0
- package/src/ssr/types.ts +81 -0
- package/src/store/create-store.ts +467 -329
- package/src/store/define-store.ts +2 -1
- package/src/store/index.ts +27 -22
- package/src/store/mapping.ts +2 -1
- package/src/store/persisted.ts +249 -61
- package/src/store/types.ts +247 -94
- package/src/store/utils.ts +135 -141
- package/src/store/watch.ts +2 -1
- package/src/storybook/index.ts +480 -0
- package/src/testing/index.ts +42 -0
- package/src/testing/testing.ts +593 -0
- package/src/testing/types.ts +170 -0
- package/src/view/custom-directives.ts +30 -0
- package/src/view/evaluate.ts +292 -290
- package/src/view/process.ts +108 -92
- package/dist/component-CY5MVoYN.js +0 -531
- package/dist/component-CY5MVoYN.js.map +0 -1
- package/dist/config-DRmZZno3.js.map +0 -1
- package/dist/core-CK2Mfpf4.js +0 -648
- package/dist/core-CK2Mfpf4.js.map +0 -1
- package/dist/motion-C5DRdPnO.js +0 -415
- package/dist/motion-C5DRdPnO.js.map +0 -1
- package/dist/platform-B7JhGBc7.js.map +0 -1
- package/dist/reactive-BDya-ia8.js +0 -253
- package/dist/reactive-BDya-ia8.js.map +0 -1
- package/dist/router-CijiICxt.js +0 -188
- package/dist/router-CijiICxt.js.map +0 -1
- package/dist/sanitize-jyJ2ryE2.js.map +0 -1
- package/dist/store-CPK9E62U.js +0 -262
- package/dist/store-CPK9E62U.js.map +0 -1
- package/dist/view-Cdi0g-qo.js +0 -396
- package/dist/view-Cdi0g-qo.js.map +0 -1
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive form creation and management.
|
|
3
|
+
*
|
|
4
|
+
* @module bquery/forms
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { computed, signal } from '../reactive/index';
|
|
8
|
+
import { isPromise } from '../core/utils/type-guards';
|
|
9
|
+
import type {
|
|
10
|
+
CrossFieldValidator,
|
|
11
|
+
FieldConfig,
|
|
12
|
+
Form,
|
|
13
|
+
FormConfig,
|
|
14
|
+
FormErrors,
|
|
15
|
+
FormField,
|
|
16
|
+
FormFields,
|
|
17
|
+
ValidationResult,
|
|
18
|
+
Validator,
|
|
19
|
+
} from './types';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Determines whether a validator returned a valid result.
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
const isValid = (result: ValidationResult): boolean => result === true || result === undefined;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Runs a single validator, normalising sync and async results.
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
const runValidator = async <T>(validator: Validator<T>, value: T): Promise<string | undefined> => {
|
|
32
|
+
const result = validator(value);
|
|
33
|
+
const resolved = isPromise(result) ? await result : result;
|
|
34
|
+
return isValid(resolved) ? undefined : (resolved as string);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Creates a reactive form field from its configuration.
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
const createField = <T>(config: FieldConfig<T>): FormField<T> => {
|
|
42
|
+
const initial = config.initialValue;
|
|
43
|
+
const value = signal<T>(initial);
|
|
44
|
+
const error = signal('');
|
|
45
|
+
const isTouched = signal(false);
|
|
46
|
+
|
|
47
|
+
const isDirty = computed(() => !Object.is(value.value, initial));
|
|
48
|
+
const isPristine = computed(() => !isDirty.value);
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
value,
|
|
52
|
+
error,
|
|
53
|
+
isDirty,
|
|
54
|
+
isTouched,
|
|
55
|
+
isPristine,
|
|
56
|
+
touch: () => {
|
|
57
|
+
isTouched.value = true;
|
|
58
|
+
},
|
|
59
|
+
reset: () => {
|
|
60
|
+
value.value = initial;
|
|
61
|
+
error.value = '';
|
|
62
|
+
isTouched.value = false;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Validates a single field against its validators.
|
|
69
|
+
* Sets the field's error signal.
|
|
70
|
+
*
|
|
71
|
+
* @returns The first error message, or an empty string if valid.
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
const validateSingleField = async <T>(
|
|
75
|
+
field: FormField<T>,
|
|
76
|
+
validators: Validator<T>[] | undefined
|
|
77
|
+
): Promise<string> => {
|
|
78
|
+
if (!validators || validators.length === 0) {
|
|
79
|
+
field.error.value = '';
|
|
80
|
+
return '';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (const validator of validators) {
|
|
84
|
+
const errorMsg = await runValidator(validator, field.value.value);
|
|
85
|
+
if (errorMsg) {
|
|
86
|
+
field.error.value = errorMsg;
|
|
87
|
+
return errorMsg;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
field.error.value = '';
|
|
92
|
+
return '';
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Creates a fully reactive form with field-level validation,
|
|
97
|
+
* dirty/touched tracking, cross-field validation, and submission handling.
|
|
98
|
+
*
|
|
99
|
+
* Each field's `value`, `error`, `isDirty`, `isTouched`, and `isPristine`
|
|
100
|
+
* are reactive signals/computed values that can be used in effects, computed
|
|
101
|
+
* values, or directly read/written.
|
|
102
|
+
*
|
|
103
|
+
* @template T - Shape of the form values (e.g. `{ name: string; age: number }`)
|
|
104
|
+
* @param config - Form configuration with field definitions, validators, and submit handler
|
|
105
|
+
* @returns A reactive {@link Form} instance
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* import { createForm, required, email, min } from '@bquery/bquery/forms';
|
|
110
|
+
*
|
|
111
|
+
* const form = createForm({
|
|
112
|
+
* fields: {
|
|
113
|
+
* name: { initialValue: '', validators: [required()] },
|
|
114
|
+
* email: { initialValue: '', validators: [required(), email()] },
|
|
115
|
+
* age: { initialValue: 0, validators: [min(18, 'Must be 18+')] },
|
|
116
|
+
* },
|
|
117
|
+
* onSubmit: async (values) => {
|
|
118
|
+
* await fetch('/api/register', {
|
|
119
|
+
* method: 'POST',
|
|
120
|
+
* body: JSON.stringify(values),
|
|
121
|
+
* });
|
|
122
|
+
* },
|
|
123
|
+
* });
|
|
124
|
+
*
|
|
125
|
+
* // Read reactive state
|
|
126
|
+
* console.log(form.isValid.value); // true (initially, before validation runs)
|
|
127
|
+
* console.log(form.fields.name.value.value); // ''
|
|
128
|
+
*
|
|
129
|
+
* // Update a field
|
|
130
|
+
* form.fields.name.value.value = 'Ada';
|
|
131
|
+
*
|
|
132
|
+
* // Validate and submit
|
|
133
|
+
* await form.handleSubmit();
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
export const createForm = <T extends Record<string, unknown>>(config: FormConfig<T>): Form<T> => {
|
|
137
|
+
// Build reactive field objects
|
|
138
|
+
const fieldEntries = Object.entries(config.fields) as [
|
|
139
|
+
keyof T & string,
|
|
140
|
+
FieldConfig<T[keyof T]>,
|
|
141
|
+
][];
|
|
142
|
+
|
|
143
|
+
const fields = {} as FormFields<T>;
|
|
144
|
+
const errors = {} as FormErrors<T>;
|
|
145
|
+
|
|
146
|
+
for (const [name, fieldConfig] of fieldEntries) {
|
|
147
|
+
const field = createField(fieldConfig as FieldConfig<T[typeof name]>);
|
|
148
|
+
(fields as Record<string, FormField>)[name] = field;
|
|
149
|
+
(errors as Record<string, typeof field.error>)[name] = field.error;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const isSubmitting = signal(false);
|
|
153
|
+
|
|
154
|
+
// Computed: form is valid when all error signals are empty
|
|
155
|
+
const isFormValid = computed(() => {
|
|
156
|
+
for (const name of Object.keys(fields)) {
|
|
157
|
+
if ((fields as Record<string, FormField>)[name].error.value !== '') {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return true;
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Computed: form is dirty when any field is dirty
|
|
165
|
+
const isFormDirty = computed(() => {
|
|
166
|
+
for (const name of Object.keys(fields)) {
|
|
167
|
+
if ((fields as Record<string, FormField>)[name].isDirty.value) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Validate a single field by name.
|
|
176
|
+
*/
|
|
177
|
+
const validateField = async (name: keyof T & string): Promise<void> => {
|
|
178
|
+
const field = (fields as Record<string, FormField>)[name];
|
|
179
|
+
const fieldConfig = (config.fields as Record<string, FieldConfig>)[name];
|
|
180
|
+
if (!field || !fieldConfig) return;
|
|
181
|
+
await validateSingleField(field, fieldConfig.validators);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Validate all fields (per-field + cross-field).
|
|
186
|
+
* Returns `true` if the entire form is valid.
|
|
187
|
+
*/
|
|
188
|
+
const validate = async (): Promise<boolean> => {
|
|
189
|
+
let hasError = false;
|
|
190
|
+
|
|
191
|
+
// Per-field validation
|
|
192
|
+
for (const [name, fieldConfig] of fieldEntries) {
|
|
193
|
+
const field = (fields as Record<string, FormField>)[name];
|
|
194
|
+
const error = await validateSingleField(field, (fieldConfig as FieldConfig).validators);
|
|
195
|
+
if (error) hasError = true;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Cross-field validation
|
|
199
|
+
if (config.crossValidators && config.crossValidators.length > 0) {
|
|
200
|
+
const values = getValues();
|
|
201
|
+
for (const crossValidator of config.crossValidators as CrossFieldValidator<T>[]) {
|
|
202
|
+
const crossErrors = await crossValidator(values);
|
|
203
|
+
if (crossErrors) {
|
|
204
|
+
for (const [fieldName, errorMsg] of Object.entries(crossErrors) as [
|
|
205
|
+
string,
|
|
206
|
+
string | undefined,
|
|
207
|
+
][]) {
|
|
208
|
+
if (errorMsg) {
|
|
209
|
+
const field = (fields as Record<string, FormField>)[fieldName];
|
|
210
|
+
if (field) {
|
|
211
|
+
// Only set cross-field error if no per-field error exists
|
|
212
|
+
if (field.error.value === '') {
|
|
213
|
+
field.error.value = errorMsg;
|
|
214
|
+
}
|
|
215
|
+
hasError = true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return !hasError;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Validate all fields and, if valid, invoke the onSubmit handler.
|
|
228
|
+
* Prevents concurrent submissions by setting isSubmitting before validation.
|
|
229
|
+
*/
|
|
230
|
+
const handleSubmit = async (): Promise<void> => {
|
|
231
|
+
if (isSubmitting.value) return;
|
|
232
|
+
isSubmitting.value = true;
|
|
233
|
+
|
|
234
|
+
try {
|
|
235
|
+
const valid = await validate();
|
|
236
|
+
if (!valid) return;
|
|
237
|
+
|
|
238
|
+
if (config.onSubmit) {
|
|
239
|
+
await config.onSubmit(getValues());
|
|
240
|
+
}
|
|
241
|
+
} finally {
|
|
242
|
+
isSubmitting.value = false;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Reset every field to its initial value and clear all errors.
|
|
248
|
+
*/
|
|
249
|
+
const reset = (): void => {
|
|
250
|
+
for (const name of Object.keys(fields)) {
|
|
251
|
+
(fields as Record<string, FormField>)[name].reset();
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Return a plain object snapshot of all current field values.
|
|
257
|
+
*/
|
|
258
|
+
const getValues = (): T => {
|
|
259
|
+
const values = {} as Record<string, unknown>;
|
|
260
|
+
for (const name of Object.keys(fields)) {
|
|
261
|
+
values[name] = (fields as Record<string, FormField>)[name].value.value;
|
|
262
|
+
}
|
|
263
|
+
return values as T;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
return {
|
|
267
|
+
fields,
|
|
268
|
+
errors,
|
|
269
|
+
isValid: isFormValid,
|
|
270
|
+
isDirty: isFormDirty,
|
|
271
|
+
isSubmitting,
|
|
272
|
+
handleSubmit,
|
|
273
|
+
validateField,
|
|
274
|
+
validate,
|
|
275
|
+
reset,
|
|
276
|
+
getValues,
|
|
277
|
+
};
|
|
278
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Form handling module for bQuery.js.
|
|
3
|
+
*
|
|
4
|
+
* Provides a reactive, TypeScript-first form API with field-level
|
|
5
|
+
* and cross-field validation, dirty/touched tracking, and submission
|
|
6
|
+
* handling — all backed by bQuery's signal-based reactivity system.
|
|
7
|
+
*
|
|
8
|
+
* @module bquery/forms
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { createForm, required, email, min } from '@bquery/bquery/forms';
|
|
13
|
+
*
|
|
14
|
+
* const form = createForm({
|
|
15
|
+
* fields: {
|
|
16
|
+
* name: { initialValue: '', validators: [required()] },
|
|
17
|
+
* email: { initialValue: '', validators: [required(), email()] },
|
|
18
|
+
* age: { initialValue: 0, validators: [min(18, 'Must be 18+')] },
|
|
19
|
+
* },
|
|
20
|
+
* onSubmit: async (values) => {
|
|
21
|
+
* await fetch('/api/register', {
|
|
22
|
+
* method: 'POST',
|
|
23
|
+
* body: JSON.stringify(values),
|
|
24
|
+
* });
|
|
25
|
+
* },
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // Reactive access
|
|
29
|
+
* console.log(form.isValid.value); // boolean
|
|
30
|
+
* console.log(form.fields.name.error.value); // '' or error message
|
|
31
|
+
*
|
|
32
|
+
* // Submit
|
|
33
|
+
* await form.handleSubmit();
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
export { createForm } from './create-form';
|
|
38
|
+
|
|
39
|
+
export {
|
|
40
|
+
custom,
|
|
41
|
+
customAsync,
|
|
42
|
+
email,
|
|
43
|
+
max,
|
|
44
|
+
maxLength,
|
|
45
|
+
min,
|
|
46
|
+
minLength,
|
|
47
|
+
pattern,
|
|
48
|
+
required,
|
|
49
|
+
url,
|
|
50
|
+
} from './validators';
|
|
51
|
+
|
|
52
|
+
export type {
|
|
53
|
+
AsyncValidator,
|
|
54
|
+
CrossFieldValidator,
|
|
55
|
+
FieldConfig,
|
|
56
|
+
Form,
|
|
57
|
+
FormConfig,
|
|
58
|
+
FormErrors,
|
|
59
|
+
FormField,
|
|
60
|
+
FormFields,
|
|
61
|
+
SubmitHandler,
|
|
62
|
+
SyncValidator,
|
|
63
|
+
ValidationResult,
|
|
64
|
+
Validator,
|
|
65
|
+
} from './types';
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Form module types and interfaces.
|
|
3
|
+
*
|
|
4
|
+
* @module bquery/forms
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Computed, Signal } from '../reactive/index';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Result of a single validation rule.
|
|
11
|
+
* A string indicates an error message; `true` or `undefined` means valid.
|
|
12
|
+
*/
|
|
13
|
+
export type ValidationResult = string | true | undefined;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Synchronous validator function.
|
|
17
|
+
*
|
|
18
|
+
* @param value - The current field value
|
|
19
|
+
* @returns A validation result — `true` / `undefined` for valid, or an error string
|
|
20
|
+
*/
|
|
21
|
+
export type SyncValidator<T = unknown> = (value: T) => ValidationResult;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Asynchronous validator function.
|
|
25
|
+
*
|
|
26
|
+
* @param value - The current field value
|
|
27
|
+
* @returns A promise resolving to a validation result
|
|
28
|
+
*/
|
|
29
|
+
export type AsyncValidator<T = unknown> = (value: T) => Promise<ValidationResult>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Either a sync or async validator.
|
|
33
|
+
*/
|
|
34
|
+
export type Validator<T = unknown> = SyncValidator<T> | AsyncValidator<T>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Configuration for a single form field.
|
|
38
|
+
*
|
|
39
|
+
* @template T - The type of the field value
|
|
40
|
+
*/
|
|
41
|
+
export type FieldConfig<T = unknown> = {
|
|
42
|
+
/** Initial value for this field */
|
|
43
|
+
initialValue: T;
|
|
44
|
+
/** Validation rules applied in order; stops at first failure */
|
|
45
|
+
validators?: Validator<T>[];
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Reactive state for a single form field.
|
|
50
|
+
*
|
|
51
|
+
* @template T - The type of the field value
|
|
52
|
+
*/
|
|
53
|
+
export type FormField<T = unknown> = {
|
|
54
|
+
/** Reactive signal holding the current value */
|
|
55
|
+
value: Signal<T>;
|
|
56
|
+
/** Reactive signal for the first validation error (empty string when valid) */
|
|
57
|
+
error: Signal<string>;
|
|
58
|
+
/** Whether the field value differs from its initial value */
|
|
59
|
+
isDirty: Computed<boolean>;
|
|
60
|
+
/** Whether the field has been interacted with (blur / explicit touch) */
|
|
61
|
+
isTouched: Signal<boolean>;
|
|
62
|
+
/** Whether the field has never been modified */
|
|
63
|
+
isPristine: Computed<boolean>;
|
|
64
|
+
/** Mark the field as touched */
|
|
65
|
+
touch: () => void;
|
|
66
|
+
/** Reset the field to its initial value and clear errors */
|
|
67
|
+
reset: () => void;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Map of field names to their reactive field state.
|
|
72
|
+
*/
|
|
73
|
+
export type FormFields<T extends Record<string, unknown>> = {
|
|
74
|
+
[K in keyof T]: FormField<T[K]>;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Map of field names to their error strings (reactive signals).
|
|
79
|
+
*/
|
|
80
|
+
export type FormErrors<T extends Record<string, unknown>> = {
|
|
81
|
+
[K in keyof T]: Signal<string>;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Cross-field validation function.
|
|
86
|
+
* Receives all current field values and returns a map of field name → error message,
|
|
87
|
+
* or an empty object / undefined if all fields are valid.
|
|
88
|
+
*/
|
|
89
|
+
export type CrossFieldValidator<T extends Record<string, unknown>> = (
|
|
90
|
+
values: T
|
|
91
|
+
) =>
|
|
92
|
+
| Partial<Record<keyof T, string>>
|
|
93
|
+
| undefined
|
|
94
|
+
| Promise<Partial<Record<keyof T, string>> | undefined>;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Submit handler function.
|
|
98
|
+
*
|
|
99
|
+
* @template T - Shape of the form values
|
|
100
|
+
*/
|
|
101
|
+
export type SubmitHandler<T extends Record<string, unknown>> = (values: T) => void | Promise<void>;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Configuration for `createForm()`.
|
|
105
|
+
*
|
|
106
|
+
* @template T - Shape of the form values
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* const config: FormConfig<{ name: string; age: number }> = {
|
|
111
|
+
* fields: {
|
|
112
|
+
* name: { initialValue: '', validators: [required('Name is required')] },
|
|
113
|
+
* age: { initialValue: 0, validators: [min(1, 'Must be positive')] },
|
|
114
|
+
* },
|
|
115
|
+
* onSubmit: (values) => console.log(values),
|
|
116
|
+
* };
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export type FormConfig<T extends Record<string, unknown>> = {
|
|
120
|
+
/** Per-field configuration with initial values and validators */
|
|
121
|
+
fields: { [K in keyof T]: FieldConfig<T[K]> };
|
|
122
|
+
/** Optional cross-field validators run after per-field validation */
|
|
123
|
+
crossValidators?: CrossFieldValidator<T>[];
|
|
124
|
+
/** Callback invoked on successful form submission */
|
|
125
|
+
onSubmit?: SubmitHandler<T>;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Return value of `createForm()`.
|
|
130
|
+
*
|
|
131
|
+
* @template T - Shape of the form values
|
|
132
|
+
*/
|
|
133
|
+
export type Form<T extends Record<string, unknown>> = {
|
|
134
|
+
/** Reactive field objects keyed by field name */
|
|
135
|
+
fields: FormFields<T>;
|
|
136
|
+
/** Shorthand error signals keyed by field name */
|
|
137
|
+
errors: FormErrors<T>;
|
|
138
|
+
/** Computed signal: `true` when all fields pass validation */
|
|
139
|
+
isValid: Computed<boolean>;
|
|
140
|
+
/** Computed signal: `true` when any field value differs from initial */
|
|
141
|
+
isDirty: Computed<boolean>;
|
|
142
|
+
/** Reactive signal: `true` while the submit handler is executing */
|
|
143
|
+
isSubmitting: Signal<boolean>;
|
|
144
|
+
/** Validate all fields and, if valid, call the `onSubmit` handler */
|
|
145
|
+
handleSubmit: () => Promise<void>;
|
|
146
|
+
/** Validate a single field by name */
|
|
147
|
+
validateField: (name: keyof T & string) => Promise<void>;
|
|
148
|
+
/** Validate all fields without submitting */
|
|
149
|
+
validate: () => Promise<boolean>;
|
|
150
|
+
/** Reset the entire form to initial values */
|
|
151
|
+
reset: () => void;
|
|
152
|
+
/** Get a snapshot of all current field values */
|
|
153
|
+
getValues: () => T;
|
|
154
|
+
};
|