@cyclone-ui/state 0.0.12 → 0.0.13
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/CHANGELOG.md +10 -0
- package/README.md +1 -1
- package/dist/base/atoms/atom-with-broadcast.cjs +45 -1
- package/dist/base/atoms/atom-with-broadcast.mjs +45 -1
- package/dist/base/atoms/atom-with-broadcast.mjs.map +1 -1
- package/dist/base/atoms/atom-with-compare.cjs +12 -1
- package/dist/base/atoms/atom-with-compare.mjs +12 -1
- package/dist/base/atoms/atom-with-compare.mjs.map +1 -1
- package/dist/base/atoms/atom-with-debounce.cjs +52 -1
- package/dist/base/atoms/atom-with-debounce.mjs +52 -1
- package/dist/base/atoms/atom-with-debounce.mjs.map +1 -1
- package/dist/base/atoms/atom-with-effect.cjs +123 -1
- package/dist/base/atoms/atom-with-effect.mjs +120 -1
- package/dist/base/atoms/atom-with-effect.mjs.map +1 -1
- package/dist/base/atoms/atom-with-listeners.cjs +36 -1
- package/dist/base/atoms/atom-with-listeners.mjs +36 -1
- package/dist/base/atoms/atom-with-listeners.mjs.map +1 -1
- package/dist/base/atoms/atom-with-pending.cjs +14 -1
- package/dist/base/atoms/atom-with-pending.mjs +14 -1
- package/dist/base/atoms/atom-with-pending.mjs.map +1 -1
- package/dist/base/atoms/atom-with-refresh-and-default.cjs +19 -1
- package/dist/base/atoms/atom-with-refresh-and-default.mjs +19 -1
- package/dist/base/atoms/atom-with-refresh-and-default.mjs.map +1 -1
- package/dist/base/atoms/atom-with-refresh.cjs +13 -1
- package/dist/base/atoms/atom-with-refresh.mjs +13 -1
- package/dist/base/atoms/atom-with-refresh.mjs.map +1 -1
- package/dist/base/atoms/atom-with-toggle.cjs +13 -1
- package/dist/base/atoms/atom-with-toggle.mjs +13 -1
- package/dist/base/atoms/atom-with-toggle.mjs.map +1 -1
- package/dist/base/atoms/atom-with-web-storage.cjs +48 -1
- package/dist/base/atoms/atom-with-web-storage.mjs +47 -1
- package/dist/base/atoms/atom-with-web-storage.mjs.map +1 -1
- package/dist/base/atoms/atom-with-wrapper.cjs +25 -1
- package/dist/base/atoms/atom-with-wrapper.mjs +25 -1
- package/dist/base/atoms/atom-with-wrapper.mjs.map +1 -1
- package/dist/base/atoms/base-atom.cjs +25 -1
- package/dist/base/atoms/base-atom.mjs +25 -1
- package/dist/base/atoms/base-atom.mjs.map +1 -1
- package/dist/base/atoms/index.cjs +24 -1
- package/dist/base/atoms/index.mjs +13 -1
- package/dist/base/hooks/index.cjs +9 -1
- package/dist/base/hooks/index.mjs +6 -1
- package/dist/base/hooks/use-atom-effect.cjs +17 -1
- package/dist/base/hooks/use-atom-effect.mjs +17 -1
- package/dist/base/hooks/use-atom-effect.mjs.map +1 -1
- package/dist/base/hooks/use-hydrate-molecule.cjs +17 -1
- package/dist/base/hooks/use-hydrate-molecule.mjs +17 -1
- package/dist/base/hooks/use-hydrate-molecule.mjs.map +1 -1
- package/dist/base/hooks/use-prepare-atoms.cjs +20 -1
- package/dist/base/hooks/use-prepare-atoms.mjs +20 -1
- package/dist/base/hooks/use-prepare-atoms.mjs.map +1 -1
- package/dist/base/hooks/use-sync-molecule.cjs +37 -1
- package/dist/base/hooks/use-sync-molecule.mjs +37 -1
- package/dist/base/hooks/use-sync-molecule.mjs.map +1 -1
- package/dist/base/index.cjs +47 -1
- package/dist/base/index.mjs +24 -1
- package/dist/base/utilities/create-molecule.cjs +105 -4
- package/dist/base/utilities/create-molecule.d.cts.map +1 -1
- package/dist/base/utilities/create-molecule.mjs +102 -4
- package/dist/base/utilities/create-molecule.mjs.map +1 -1
- package/dist/base/utilities/create-web-storage.cjs +55 -1
- package/dist/base/utilities/create-web-storage.mjs +55 -1
- package/dist/base/utilities/create-web-storage.mjs.map +1 -1
- package/dist/base/utilities/debounce.cjs +52 -1
- package/dist/base/utilities/debounce.mjs +52 -1
- package/dist/base/utilities/debounce.mjs.map +1 -1
- package/dist/base/utilities/index.cjs +13 -1
- package/dist/base/utilities/index.mjs +6 -1
- package/dist/base/utilities/is-atom.cjs +48 -1
- package/dist/base/utilities/is-atom.mjs +46 -1
- package/dist/base/utilities/is-atom.mjs.map +1 -1
- package/dist/base/utilities/set-atom-debug.cjs +24 -1
- package/dist/base/utilities/set-atom-debug.mjs +23 -1
- package/dist/base/utilities/set-atom-debug.mjs.map +1 -1
- package/dist/client/index.cjs +16 -1
- package/dist/client/index.mjs +8 -1
- package/dist/form/atoms/atom-with-field.cjs +23 -1
- package/dist/form/atoms/atom-with-field.mjs +23 -1
- package/dist/form/atoms/atom-with-field.mjs.map +1 -1
- package/dist/form/atoms/atom-with-messages.cjs +85 -1
- package/dist/form/atoms/atom-with-messages.mjs +78 -1
- package/dist/form/atoms/atom-with-messages.mjs.map +1 -1
- package/dist/form/hooks/index.cjs +7 -1
- package/dist/form/hooks/index.mjs +5 -1
- package/dist/form/hooks/use-field-actions.cjs +92 -1
- package/dist/form/hooks/use-field-actions.mjs +92 -1
- package/dist/form/hooks/use-field-actions.mjs.map +1 -1
- package/dist/form/hooks/use-field-ref.cjs +16 -1
- package/dist/form/hooks/use-field-ref.mjs +16 -1
- package/dist/form/hooks/use-field-ref.mjs.map +1 -1
- package/dist/form/hooks/use-form-actions.cjs +69 -1
- package/dist/form/hooks/use-form-actions.mjs +69 -1
- package/dist/form/hooks/use-form-actions.mjs.map +1 -1
- package/dist/form/index.cjs +30 -1
- package/dist/form/index.mjs +17 -1
- package/dist/form/molecules/field-molecule.cjs +154 -1
- package/dist/form/molecules/field-molecule.mjs +153 -1
- package/dist/form/molecules/field-molecule.mjs.map +1 -1
- package/dist/form/molecules/form-molecule.cjs +180 -1
- package/dist/form/molecules/form-molecule.mjs +179 -1
- package/dist/form/molecules/form-molecule.mjs.map +1 -1
- package/dist/form/molecules/index.cjs +7 -1
- package/dist/form/molecules/index.mjs +4 -1
- package/dist/form/providers/FieldProvider.cjs +103 -4
- package/dist/form/providers/FieldProvider.mjs +103 -4
- package/dist/form/providers/FieldProvider.mjs.map +1 -1
- package/dist/form/providers/FormProvider.cjs +66 -4
- package/dist/form/providers/FormProvider.d.cts.map +1 -1
- package/dist/form/providers/FormProvider.mjs +66 -4
- package/dist/form/providers/FormProvider.mjs.map +1 -1
- package/dist/form/providers/index.cjs +5 -1
- package/dist/form/providers/index.mjs +4 -1
- package/dist/form/types.cjs +13 -1
- package/dist/form/types.mjs +13 -1
- package/dist/form/types.mjs.map +1 -1
- package/dist/form/utilities/abort-error.cjs +15 -1
- package/dist/form/utilities/abort-error.mjs +15 -1
- package/dist/form/utilities/abort-error.mjs.map +1 -1
- package/dist/form/utilities/index.cjs +7 -1
- package/dist/form/utilities/index.mjs +5 -1
- package/dist/form/utilities/is-validation-results.cjs +9 -1
- package/dist/form/utilities/is-validation-results.mjs +9 -1
- package/dist/form/utilities/is-validation-results.mjs.map +1 -1
- package/dist/form/utilities/validators.cjs +8 -1
- package/dist/form/utilities/validators.mjs +8 -1
- package/dist/form/utilities/validators.mjs.map +1 -1
- package/dist/index.cjs +71 -1
- package/dist/index.mjs +34 -1
- package/dist/message/components/MessageViewport.cjs +21 -6
- package/dist/message/components/MessageViewport.mjs +21 -6
- package/dist/message/components/MessageViewport.mjs.map +1 -1
- package/dist/message/components/index.cjs +3 -1
- package/dist/message/components/index.mjs +3 -1
- package/dist/message/hooks/index.cjs +3 -1
- package/dist/message/hooks/index.mjs +3 -1
- package/dist/message/hooks/use-message-actions.cjs +63 -1
- package/dist/message/hooks/use-message-actions.mjs +63 -1
- package/dist/message/hooks/use-message-actions.mjs.map +1 -1
- package/dist/message/index.cjs +23 -1
- package/dist/message/index.mjs +8 -1
- package/dist/message/providers/MessageProvider.cjs +16 -1
- package/dist/message/providers/MessageProvider.mjs +16 -1
- package/dist/message/providers/MessageProvider.mjs.map +1 -1
- package/dist/theme/hooks/index.cjs +7 -1
- package/dist/theme/hooks/index.mjs +4 -1
- package/dist/theme/hooks/use-theme-actions.cjs +21 -1
- package/dist/theme/hooks/use-theme-actions.mjs +21 -1
- package/dist/theme/hooks/use-theme-actions.mjs.map +1 -1
- package/dist/theme/hooks/use-theme-store.cjs +61 -1
- package/dist/theme/hooks/use-theme-store.d.cts +2 -2
- package/dist/theme/hooks/use-theme-store.d.mts +2 -2
- package/dist/theme/hooks/use-theme-store.mjs +59 -1
- package/dist/theme/hooks/use-theme-store.mjs.map +1 -1
- package/dist/theme/index.cjs +17 -1
- package/dist/theme/index.mjs +9 -1
- package/dist/theme/molecules/index.cjs +3 -1
- package/dist/theme/molecules/index.mjs +3 -1
- package/dist/theme/molecules/theme-molecule.cjs +14 -1
- package/dist/theme/molecules/theme-molecule.mjs +14 -1
- package/dist/theme/molecules/theme-molecule.mjs.map +1 -1
- package/dist/theme/providers/ThemeProvider.cjs +30 -5
- package/dist/theme/providers/ThemeProvider.mjs +30 -5
- package/dist/theme/providers/ThemeProvider.mjs.map +1 -1
- package/dist/theme/utilities/theme-helpers.cjs +71 -1
- package/dist/theme/utilities/theme-helpers.mjs +70 -1
- package/dist/theme/utilities/theme-helpers.mjs.map +1 -1
- package/package.json +6 -6
|
@@ -1,2 +1,93 @@
|
|
|
1
|
-
import{FieldApi
|
|
1
|
+
import { FieldApi } from "../molecules/field-molecule.mjs";
|
|
2
|
+
import { ValidationCause } from "../types.mjs";
|
|
3
|
+
import { RESET, useAtomCallback } from "jotai/utils";
|
|
4
|
+
import { useCallback } from "react";
|
|
5
|
+
import { isPromise } from "@stryke/type-checks/is-promise";
|
|
6
|
+
import { upperCaseFirst } from "@stryke/string-format/upper-case-first";
|
|
7
|
+
|
|
8
|
+
//#region src/form/hooks/use-field-actions.ts
|
|
9
|
+
const useFieldActions = () => {
|
|
10
|
+
const field = FieldApi.useMolecule();
|
|
11
|
+
const validate = useAtomCallback(useCallback(async (get, set, nextValue, cause) => {
|
|
12
|
+
const options = get(field.options);
|
|
13
|
+
if (options.validate?.[`on${upperCaseFirst(cause)}`] && options.validate[`on${upperCaseFirst(cause)}`].length > 0 || options.validate?.[`on${upperCaseFirst(ValidationCause.INITIALIZE)}`] && options.validate[`on${upperCaseFirst(ValidationCause.INITIALIZE)}`].length > 0) {
|
|
14
|
+
const previousValue = get(field.previousValue);
|
|
15
|
+
const results = [];
|
|
16
|
+
if (options.validate?.[`on${upperCaseFirst(cause)}`] && options.validate[`on${upperCaseFirst(cause)}`].length > 0) results.push(...options.validate[`on${upperCaseFirst(cause)}`].map((validator) => validator(nextValue, previousValue, cause, get, set)));
|
|
17
|
+
if (cause !== ValidationCause.INITIALIZE && options.validate?.[`on${upperCaseFirst(ValidationCause.INITIALIZE)}`] && options.validate[`on${upperCaseFirst(ValidationCause.INITIALIZE)}`].length > 0) results.push(...options.validate[`on${upperCaseFirst(ValidationCause.INITIALIZE)}`].map((validator) => validator(nextValue, previousValue, cause, get, set)));
|
|
18
|
+
const messages = [];
|
|
19
|
+
const promises = [];
|
|
20
|
+
for (const result of results) if (result) {
|
|
21
|
+
if (isPromise(result)) promises.push(result);
|
|
22
|
+
else messages.push(...result);
|
|
23
|
+
}
|
|
24
|
+
if (promises.length > 0) {
|
|
25
|
+
set(field.validating, true);
|
|
26
|
+
for (const result of await Promise.all(promises)) if (result && result.length > 0) messages.push(...result);
|
|
27
|
+
set(field.validating, false);
|
|
28
|
+
}
|
|
29
|
+
set(field.validationResults, (prev) => ({
|
|
30
|
+
...prev,
|
|
31
|
+
[cause]: messages
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
}, []));
|
|
35
|
+
const reset = useAtomCallback(useCallback(async (get, set) => {
|
|
36
|
+
set(field.value, get(field.initialValue));
|
|
37
|
+
set(field.options, RESET);
|
|
38
|
+
}, []));
|
|
39
|
+
const initialize = useAtomCallback(useCallback(async (get, set, initialValue, skipIfDirty = true) => {
|
|
40
|
+
if (skipIfDirty && get(field.dirty)) return;
|
|
41
|
+
if (initialValue !== get(field.initialValue)) set(field.initialValue, initialValue);
|
|
42
|
+
}, []));
|
|
43
|
+
const change = useAtomCallback(useCallback(async (get, set, nextValue, touch = false) => {
|
|
44
|
+
if (!get(field.disabled)) {
|
|
45
|
+
if (!get(field.touched) && touch) set(field.touched, touch);
|
|
46
|
+
const options = get(field.options);
|
|
47
|
+
const value = get(field.value);
|
|
48
|
+
let nextParsed = nextValue;
|
|
49
|
+
if (options.parse) nextParsed = options.parse(nextValue);
|
|
50
|
+
if (!options.isEqual(nextParsed, value)) set(field.value, nextParsed);
|
|
51
|
+
}
|
|
52
|
+
}, []));
|
|
53
|
+
const focus = useAtomCallback(useCallback(async (get, set) => {
|
|
54
|
+
if (!get(field.disabled)) {
|
|
55
|
+
if (!get(field.touched)) set(field.touched, true);
|
|
56
|
+
set(field.focused, true);
|
|
57
|
+
}
|
|
58
|
+
}, []));
|
|
59
|
+
const blur = useAtomCallback(useCallback(async (get, set) => {
|
|
60
|
+
if (!get(field.disabled)) {
|
|
61
|
+
if (!get(field.touched)) set(field.touched, true);
|
|
62
|
+
set(field.focused, false);
|
|
63
|
+
set(field.blurred, true);
|
|
64
|
+
}
|
|
65
|
+
}, []));
|
|
66
|
+
const setFocused = useCallback(async (focused) => {
|
|
67
|
+
if (focused) return focus();
|
|
68
|
+
return blur();
|
|
69
|
+
}, [focus, blur]);
|
|
70
|
+
const toggleFocused = useAtomCallback(useCallback(async (get) => setFocused(!get(field.focused)), [setFocused]));
|
|
71
|
+
return {
|
|
72
|
+
mount: useAtomCallback(useCallback(async (get, set, ref, tabIndex) => {
|
|
73
|
+
set(field.ref, ref);
|
|
74
|
+
set(field.tabIndex, tabIndex ?? 0);
|
|
75
|
+
if (!get(field.initialValue)) {
|
|
76
|
+
const options = get(field.options);
|
|
77
|
+
set(field.initialValue, options.defaultValue ?? null);
|
|
78
|
+
}
|
|
79
|
+
}, [])),
|
|
80
|
+
initialize,
|
|
81
|
+
change,
|
|
82
|
+
focus,
|
|
83
|
+
blur,
|
|
84
|
+
setFocused,
|
|
85
|
+
toggleFocused,
|
|
86
|
+
validate,
|
|
87
|
+
reset
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
//#endregion
|
|
92
|
+
export { useFieldActions };
|
|
2
93
|
//# sourceMappingURL=use-field-actions.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-field-actions.mjs","names":[
|
|
1
|
+
{"version":3,"file":"use-field-actions.mjs","names":[],"sources":[],"mappings":""}
|
|
@@ -1 +1,16 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_field_molecule = require("../molecules/field-molecule.cjs");
|
|
2
|
+
let react = require("react");
|
|
3
|
+
let _maskito_react = require("@maskito/react");
|
|
4
|
+
let _tamagui_core = require("@tamagui/core");
|
|
5
|
+
|
|
6
|
+
//#region src/form/hooks/use-field-ref.ts
|
|
7
|
+
const useFieldRef = (forwardedRef) => {
|
|
8
|
+
const ref = (0, react.useRef)(null);
|
|
9
|
+
const options = require_field_molecule.FieldApi.use().options.get();
|
|
10
|
+
const refs = [ref, (0, _maskito_react.useMaskito)({ options: options.mask })];
|
|
11
|
+
if (forwardedRef) refs.push(forwardedRef);
|
|
12
|
+
return (0, _tamagui_core.useComposedRefs)(...refs);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
exports.useFieldRef = useFieldRef;
|
|
@@ -1,2 +1,17 @@
|
|
|
1
|
-
import{FieldApi
|
|
1
|
+
import { FieldApi } from "../molecules/field-molecule.mjs";
|
|
2
|
+
import { useRef } from "react";
|
|
3
|
+
import { useMaskito } from "@maskito/react";
|
|
4
|
+
import { useComposedRefs } from "@tamagui/core";
|
|
5
|
+
|
|
6
|
+
//#region src/form/hooks/use-field-ref.ts
|
|
7
|
+
const useFieldRef = (forwardedRef) => {
|
|
8
|
+
const ref = useRef(null);
|
|
9
|
+
const options = FieldApi.use().options.get();
|
|
10
|
+
const refs = [ref, useMaskito({ options: options.mask })];
|
|
11
|
+
if (forwardedRef) refs.push(forwardedRef);
|
|
12
|
+
return useComposedRefs(...refs);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { useFieldRef };
|
|
2
17
|
//# sourceMappingURL=use-field-ref.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-field-ref.mjs","names":[
|
|
1
|
+
{"version":3,"file":"use-field-ref.mjs","names":[],"sources":[],"mappings":""}
|
|
@@ -1 +1,69 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_form_molecule = require("../molecules/form-molecule.cjs");
|
|
2
|
+
const require_types = require("../types.cjs");
|
|
3
|
+
let jotai_utils = require("jotai/utils");
|
|
4
|
+
let react = require("react");
|
|
5
|
+
let _stryke_type_checks_is_promise = require("@stryke/type-checks/is-promise");
|
|
6
|
+
let _stryke_string_format_upper_case_first = require("@stryke/string-format/upper-case-first");
|
|
7
|
+
|
|
8
|
+
//#region src/form/hooks/use-form-actions.ts
|
|
9
|
+
const useFormActions = () => {
|
|
10
|
+
const form = require_form_molecule.FormApi.useMolecule();
|
|
11
|
+
return {
|
|
12
|
+
initializeField: (0, jotai_utils.useAtomCallback)((0, react.useCallback)(async (_get, _set, _name) => {}, [])),
|
|
13
|
+
uninitializeField: (0, jotai_utils.useAtomCallback)((0, react.useCallback)(async (_get, _set, _name) => {}, [])),
|
|
14
|
+
validate: (0, jotai_utils.useAtomCallback)((0, react.useCallback)(async (get, set, nextValues, cause) => {
|
|
15
|
+
const options = get(form.options);
|
|
16
|
+
if (options.validate?.[`on${(0, _stryke_string_format_upper_case_first.upperCaseFirst)(cause)}`] && options.validate[`on${(0, _stryke_string_format_upper_case_first.upperCaseFirst)(cause)}`].length > 0 || options.validate?.[`on${(0, _stryke_string_format_upper_case_first.upperCaseFirst)(require_types.ValidationCause.INITIALIZE)}`] && options.validate[`on${(0, _stryke_string_format_upper_case_first.upperCaseFirst)(require_types.ValidationCause.INITIALIZE)}`].length > 0) {
|
|
17
|
+
const previousValues = get(form.previousValues);
|
|
18
|
+
const results = [];
|
|
19
|
+
if (options.validate?.[`on${(0, _stryke_string_format_upper_case_first.upperCaseFirst)(cause)}`] && options.validate[`on${(0, _stryke_string_format_upper_case_first.upperCaseFirst)(cause)}`].length > 0) results.push(...options.validate[`on${(0, _stryke_string_format_upper_case_first.upperCaseFirst)(cause)}`].map((validator) => validator(nextValues, previousValues, cause, get, set)));
|
|
20
|
+
if (cause !== require_types.ValidationCause.INITIALIZE && options.validate?.[`on${(0, _stryke_string_format_upper_case_first.upperCaseFirst)(require_types.ValidationCause.INITIALIZE)}`] && options.validate[`on${(0, _stryke_string_format_upper_case_first.upperCaseFirst)(require_types.ValidationCause.INITIALIZE)}`].length > 0) results.push(...options.validate[`on${(0, _stryke_string_format_upper_case_first.upperCaseFirst)(require_types.ValidationCause.INITIALIZE)}`].map((validator) => validator(nextValues, previousValues, cause, get, set)));
|
|
21
|
+
const messages = [];
|
|
22
|
+
const promises = [];
|
|
23
|
+
for (const result of results) if (result) {
|
|
24
|
+
if ((0, _stryke_type_checks_is_promise.isPromise)(result)) promises.push(result);
|
|
25
|
+
else messages.push(...result);
|
|
26
|
+
}
|
|
27
|
+
if (promises.length > 0) {
|
|
28
|
+
set(form.formValidating, true);
|
|
29
|
+
for (const result of await Promise.all(promises)) if (result && result.length > 0) messages.push(...result);
|
|
30
|
+
set(form.formValidating, false);
|
|
31
|
+
}
|
|
32
|
+
set(form.validationResults, (prev) => ({
|
|
33
|
+
...prev,
|
|
34
|
+
[cause]: messages
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
}, [])),
|
|
38
|
+
change: (0, jotai_utils.useAtomCallback)((0, react.useCallback)(async (get, set, nextValues) => {
|
|
39
|
+
const options = get(form.options);
|
|
40
|
+
const values = get(form.values);
|
|
41
|
+
if (!options.isEqual(nextValues, values)) set(form.values, nextValues);
|
|
42
|
+
}, [])),
|
|
43
|
+
submit: (0, jotai_utils.useAtomCallback)((0, react.useCallback)(async (get, set) => {
|
|
44
|
+
if (get(form.canSubmit)) {
|
|
45
|
+
if (!get(form.submitting)) set(form.submitting, true);
|
|
46
|
+
set(form.submitAttempts, (prev) => prev + 1);
|
|
47
|
+
const options = get(form.options);
|
|
48
|
+
try {
|
|
49
|
+
if (get(form.valid)) {
|
|
50
|
+
await options.onSubmit?.({
|
|
51
|
+
get,
|
|
52
|
+
set,
|
|
53
|
+
atoms: form
|
|
54
|
+
});
|
|
55
|
+
set(form.submitted, true);
|
|
56
|
+
}
|
|
57
|
+
} finally {
|
|
58
|
+
set(form.submitting, false);
|
|
59
|
+
}
|
|
60
|
+
} else if (get(form.invalid)) {
|
|
61
|
+
if (get(form.invalid)) {}
|
|
62
|
+
}
|
|
63
|
+
}, [])),
|
|
64
|
+
reset: (0, react.useCallback)(() => {}, [])
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
exports.useFormActions = useFormActions;
|
|
@@ -1,2 +1,70 @@
|
|
|
1
|
-
import{FormApi
|
|
1
|
+
import { FormApi } from "../molecules/form-molecule.mjs";
|
|
2
|
+
import { ValidationCause } from "../types.mjs";
|
|
3
|
+
import { useAtomCallback } from "jotai/utils";
|
|
4
|
+
import { useCallback } from "react";
|
|
5
|
+
import { isPromise } from "@stryke/type-checks/is-promise";
|
|
6
|
+
import { upperCaseFirst } from "@stryke/string-format/upper-case-first";
|
|
7
|
+
|
|
8
|
+
//#region src/form/hooks/use-form-actions.ts
|
|
9
|
+
const useFormActions = () => {
|
|
10
|
+
const form = FormApi.useMolecule();
|
|
11
|
+
return {
|
|
12
|
+
initializeField: useAtomCallback(useCallback(async (_get, _set, _name) => {}, [])),
|
|
13
|
+
uninitializeField: useAtomCallback(useCallback(async (_get, _set, _name) => {}, [])),
|
|
14
|
+
validate: useAtomCallback(useCallback(async (get, set, nextValues, cause) => {
|
|
15
|
+
const options = get(form.options);
|
|
16
|
+
if (options.validate?.[`on${upperCaseFirst(cause)}`] && options.validate[`on${upperCaseFirst(cause)}`].length > 0 || options.validate?.[`on${upperCaseFirst(ValidationCause.INITIALIZE)}`] && options.validate[`on${upperCaseFirst(ValidationCause.INITIALIZE)}`].length > 0) {
|
|
17
|
+
const previousValues = get(form.previousValues);
|
|
18
|
+
const results = [];
|
|
19
|
+
if (options.validate?.[`on${upperCaseFirst(cause)}`] && options.validate[`on${upperCaseFirst(cause)}`].length > 0) results.push(...options.validate[`on${upperCaseFirst(cause)}`].map((validator) => validator(nextValues, previousValues, cause, get, set)));
|
|
20
|
+
if (cause !== ValidationCause.INITIALIZE && options.validate?.[`on${upperCaseFirst(ValidationCause.INITIALIZE)}`] && options.validate[`on${upperCaseFirst(ValidationCause.INITIALIZE)}`].length > 0) results.push(...options.validate[`on${upperCaseFirst(ValidationCause.INITIALIZE)}`].map((validator) => validator(nextValues, previousValues, cause, get, set)));
|
|
21
|
+
const messages = [];
|
|
22
|
+
const promises = [];
|
|
23
|
+
for (const result of results) if (result) {
|
|
24
|
+
if (isPromise(result)) promises.push(result);
|
|
25
|
+
else messages.push(...result);
|
|
26
|
+
}
|
|
27
|
+
if (promises.length > 0) {
|
|
28
|
+
set(form.formValidating, true);
|
|
29
|
+
for (const result of await Promise.all(promises)) if (result && result.length > 0) messages.push(...result);
|
|
30
|
+
set(form.formValidating, false);
|
|
31
|
+
}
|
|
32
|
+
set(form.validationResults, (prev) => ({
|
|
33
|
+
...prev,
|
|
34
|
+
[cause]: messages
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
}, [])),
|
|
38
|
+
change: useAtomCallback(useCallback(async (get, set, nextValues) => {
|
|
39
|
+
const options = get(form.options);
|
|
40
|
+
const values = get(form.values);
|
|
41
|
+
if (!options.isEqual(nextValues, values)) set(form.values, nextValues);
|
|
42
|
+
}, [])),
|
|
43
|
+
submit: useAtomCallback(useCallback(async (get, set) => {
|
|
44
|
+
if (get(form.canSubmit)) {
|
|
45
|
+
if (!get(form.submitting)) set(form.submitting, true);
|
|
46
|
+
set(form.submitAttempts, (prev) => prev + 1);
|
|
47
|
+
const options = get(form.options);
|
|
48
|
+
try {
|
|
49
|
+
if (get(form.valid)) {
|
|
50
|
+
await options.onSubmit?.({
|
|
51
|
+
get,
|
|
52
|
+
set,
|
|
53
|
+
atoms: form
|
|
54
|
+
});
|
|
55
|
+
set(form.submitted, true);
|
|
56
|
+
}
|
|
57
|
+
} finally {
|
|
58
|
+
set(form.submitting, false);
|
|
59
|
+
}
|
|
60
|
+
} else if (get(form.invalid)) {
|
|
61
|
+
if (get(form.invalid)) {}
|
|
62
|
+
}
|
|
63
|
+
}, [])),
|
|
64
|
+
reset: useCallback(() => {}, [])
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
export { useFormActions };
|
|
2
70
|
//# sourceMappingURL=use-form-actions.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-form-actions.mjs","names":[
|
|
1
|
+
{"version":3,"file":"use-form-actions.mjs","names":[],"sources":[],"mappings":""}
|
package/dist/form/index.cjs
CHANGED
|
@@ -1 +1,30 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_is_validation_results = require("./utilities/is-validation-results.cjs");
|
|
3
|
+
const require_validators = require("./utilities/validators.cjs");
|
|
4
|
+
const require_form_molecule = require("./molecules/form-molecule.cjs");
|
|
5
|
+
const require_field_molecule = require("./molecules/field-molecule.cjs");
|
|
6
|
+
const require_types = require("./types.cjs");
|
|
7
|
+
const require_use_field_actions = require("./hooks/use-field-actions.cjs");
|
|
8
|
+
const require_use_field_ref = require("./hooks/use-field-ref.cjs");
|
|
9
|
+
const require_use_form_actions = require("./hooks/use-form-actions.cjs");
|
|
10
|
+
require("./hooks/index.cjs");
|
|
11
|
+
require("./molecules/index.cjs");
|
|
12
|
+
const require_abort_error = require("./utilities/abort-error.cjs");
|
|
13
|
+
const require_FieldProvider = require("./providers/FieldProvider.cjs");
|
|
14
|
+
const require_FormProvider = require("./providers/FormProvider.cjs");
|
|
15
|
+
require("./providers/index.cjs");
|
|
16
|
+
require("./utilities/index.cjs");
|
|
17
|
+
|
|
18
|
+
exports.AbortError = require_abort_error.AbortError;
|
|
19
|
+
exports.DEFAULT_FIELD_OPTIONS = require_field_molecule.DEFAULT_FIELD_OPTIONS;
|
|
20
|
+
exports.DEFAULT_FORM_OPTIONS = require_form_molecule.DEFAULT_FORM_OPTIONS;
|
|
21
|
+
exports.FieldApi = require_field_molecule.FieldApi;
|
|
22
|
+
exports.FieldProvider = require_FieldProvider.FieldProvider;
|
|
23
|
+
exports.FormApi = require_form_molecule.FormApi;
|
|
24
|
+
exports.FormProvider = require_FormProvider.FormProvider;
|
|
25
|
+
exports.ValidationCause = require_types.ValidationCause;
|
|
26
|
+
exports.isValidationResults = require_is_validation_results.isValidationResults;
|
|
27
|
+
exports.requiredValidator = require_validators.requiredValidator;
|
|
28
|
+
exports.useFieldActions = require_use_field_actions.useFieldActions;
|
|
29
|
+
exports.useFieldRef = require_use_field_ref.useFieldRef;
|
|
30
|
+
exports.useFormActions = require_use_form_actions.useFormActions;
|
package/dist/form/index.mjs
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
import{isValidationResults
|
|
1
|
+
import { isValidationResults } from "./utilities/is-validation-results.mjs";
|
|
2
|
+
import { requiredValidator } from "./utilities/validators.mjs";
|
|
3
|
+
import { DEFAULT_FORM_OPTIONS, FormApi } from "./molecules/form-molecule.mjs";
|
|
4
|
+
import { DEFAULT_FIELD_OPTIONS, FieldApi } from "./molecules/field-molecule.mjs";
|
|
5
|
+
import { ValidationCause } from "./types.mjs";
|
|
6
|
+
import { useFieldActions } from "./hooks/use-field-actions.mjs";
|
|
7
|
+
import { useFieldRef } from "./hooks/use-field-ref.mjs";
|
|
8
|
+
import { useFormActions } from "./hooks/use-form-actions.mjs";
|
|
9
|
+
import "./hooks/index.mjs";
|
|
10
|
+
import "./molecules/index.mjs";
|
|
11
|
+
import { AbortError } from "./utilities/abort-error.mjs";
|
|
12
|
+
import { FieldProvider } from "./providers/FieldProvider.mjs";
|
|
13
|
+
import { FormProvider } from "./providers/FormProvider.mjs";
|
|
14
|
+
import "./providers/index.mjs";
|
|
15
|
+
import "./utilities/index.mjs";
|
|
16
|
+
|
|
17
|
+
export { AbortError, DEFAULT_FIELD_OPTIONS, DEFAULT_FORM_OPTIONS, FieldApi, FieldProvider, FormApi, FormProvider, ValidationCause, isValidationResults, requiredValidator, useFieldActions, useFieldRef, useFormActions };
|
|
@@ -1 +1,154 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_create_molecule = require("../../base/utilities/create-molecule.cjs");
|
|
2
|
+
require("../../base/index.cjs");
|
|
3
|
+
const require_atom_with_field = require("../atoms/atom-with-field.cjs");
|
|
4
|
+
const require_atom_with_messages = require("../atoms/atom-with-messages.cjs");
|
|
5
|
+
const require_validators = require("../utilities/validators.cjs");
|
|
6
|
+
const require_form_molecule = require("./form-molecule.cjs");
|
|
7
|
+
let _stryke_type_checks_is_function = require("@stryke/type-checks/is-function");
|
|
8
|
+
let jotai_utils = require("jotai/utils");
|
|
9
|
+
let _stryke_helpers_is_equal = require("@stryke/helpers/is-equal");
|
|
10
|
+
let jotai = require("jotai");
|
|
11
|
+
let _stryke_type_checks_is_string = require("@stryke/type-checks/is-string");
|
|
12
|
+
let _stryke_helpers_deep_merge = require("@stryke/helpers/deep-merge");
|
|
13
|
+
let _stryke_helpers_get_field = require("@stryke/helpers/get-field");
|
|
14
|
+
let _stryke_helpers_to_path = require("@stryke/helpers/to-path");
|
|
15
|
+
let _stryke_type_checks_is_set = require("@stryke/type-checks/is-set");
|
|
16
|
+
let jotai_optics = require("jotai-optics");
|
|
17
|
+
|
|
18
|
+
//#region src/form/molecules/field-molecule.ts
|
|
19
|
+
const DEFAULT_FIELD_OPTIONS = {
|
|
20
|
+
theme: "base",
|
|
21
|
+
size: "md",
|
|
22
|
+
debounceMs: 100,
|
|
23
|
+
isEqual: _stryke_helpers_is_equal.isEqual,
|
|
24
|
+
required: false,
|
|
25
|
+
disabled: false,
|
|
26
|
+
clearable: false,
|
|
27
|
+
initialValue: null
|
|
28
|
+
};
|
|
29
|
+
const FieldApi = require_create_molecule.createMoleculeApi((scope) => {
|
|
30
|
+
const formScope = require_create_molecule.use(require_form_molecule.FormApi.Scope);
|
|
31
|
+
const formScopeAtom = (0, jotai.atom)(() => formScope);
|
|
32
|
+
const form = require_create_molecule.use(require_form_molecule.FormApi.Molecule);
|
|
33
|
+
const optionsBaseAtom = (0, jotai.atom)(DEFAULT_FIELD_OPTIONS);
|
|
34
|
+
const optionsAtom = (0, jotai.atom)((get) => {
|
|
35
|
+
const formOptions = get(form.options);
|
|
36
|
+
const options = get(optionsBaseAtom);
|
|
37
|
+
const result = (0, _stryke_helpers_deep_merge.deepMerge)(formOptions.defaultFieldOptions, options);
|
|
38
|
+
return (0, _stryke_helpers_deep_merge.deepMerge)({ validate: { onBlur: result.required ? [require_validators.requiredValidator] : [] } }, result);
|
|
39
|
+
}, (get, set, update) => {
|
|
40
|
+
let nextValue = {};
|
|
41
|
+
if ((0, _stryke_type_checks_is_function.isFunction)(update)) nextValue = update(get(optionsBaseAtom));
|
|
42
|
+
else if (update === jotai_utils.RESET) nextValue = {};
|
|
43
|
+
else nextValue = update;
|
|
44
|
+
set(optionsBaseAtom, (0, _stryke_helpers_deep_merge.deepMerge)(DEFAULT_FIELD_OPTIONS, nextValue));
|
|
45
|
+
});
|
|
46
|
+
const formPrefix = `${formScope}-`;
|
|
47
|
+
const path = (0, _stryke_helpers_to_path.toPath)(scope.startsWith(formPrefix) ? scope.slice(formPrefix.length) : scope.includes("-") ? scope.slice(scope.indexOf("-") + 1) : scope);
|
|
48
|
+
const pathAtom = (0, jotai.atom)(() => path);
|
|
49
|
+
const nameAtom = (0, jotai.atom)((get) => get(pathAtom).slice(-1)[0]);
|
|
50
|
+
const focusedAtom = (0, jotai_optics.focusAtom)(form.focusedFields, (optic) => optic.path(...path));
|
|
51
|
+
const requiredStateAtom = (0, jotai_optics.focusAtom)(form.requiredFields, (optic) => optic.path(...path));
|
|
52
|
+
const disabledStateAtom = (0, jotai_optics.focusAtom)(form.disabledFields, (optic) => optic.path(...path));
|
|
53
|
+
const requiredAtom = (0, jotai.atom)((get) => get(requiredStateAtom) ?? get(optionsAtom).required, (_get, set, update) => {
|
|
54
|
+
set(requiredStateAtom, update);
|
|
55
|
+
});
|
|
56
|
+
const disabledAtom = (0, jotai.atom)((get) => get(disabledStateAtom) ?? get(optionsAtom).disabled, (_get, set, update) => {
|
|
57
|
+
set(disabledStateAtom, update);
|
|
58
|
+
});
|
|
59
|
+
const clearableAtom = (0, jotai.atom)((get) => get(optionsAtom).clearable);
|
|
60
|
+
const touchedAtom = (0, jotai_optics.focusAtom)(form.touchedFields, (optic) => optic.path(...path));
|
|
61
|
+
const blurredAtom = (0, jotai_optics.focusAtom)(form.blurredFields, (optic) => optic.path(...path));
|
|
62
|
+
const validatingAtom = (0, jotai_optics.focusAtom)(form.validatingFields, (optic) => optic.path(...path));
|
|
63
|
+
const validationResultsAtom = (0, jotai_optics.focusAtom)(form.validationResultsFields, (optic) => optic.path(...path));
|
|
64
|
+
const tabIndexAtom = (0, jotai_optics.focusAtom)(form.tabIndexes, (optic) => optic.path(...path));
|
|
65
|
+
const refAtom = (0, jotai_optics.focusAtom)(form.refs, (optic) => optic.path(...path));
|
|
66
|
+
const initialValueAtom = (0, jotai_optics.focusAtom)(form.initialValues, (optic) => optic.path(...path));
|
|
67
|
+
const valueAtom = (0, jotai_optics.focusAtom)(form.values, (optic) => optic.path(...path));
|
|
68
|
+
const dirtyAtom = (0, jotai.atom)((get) => {
|
|
69
|
+
return get(optionsAtom).isEqual(get(valueAtom), get(initialValueAtom));
|
|
70
|
+
});
|
|
71
|
+
const errorsAtom = require_atom_with_messages.atomWithFieldsMessageTypes(validationResultsAtom, "danger");
|
|
72
|
+
const warningsAtom = require_atom_with_messages.atomWithFieldsMessageTypes(validationResultsAtom, "warning");
|
|
73
|
+
const infoAtom = require_atom_with_messages.atomWithFieldsMessageTypes(validationResultsAtom, "info");
|
|
74
|
+
const discoveryAtom = require_atom_with_messages.atomWithFieldsMessageTypes(validationResultsAtom, "discovery");
|
|
75
|
+
const successesAtom = require_atom_with_messages.atomWithFieldsMessageTypes(validationResultsAtom, "success");
|
|
76
|
+
const positivesAtom = require_atom_with_messages.atomWithFieldsMessageTypes(validationResultsAtom, "positive");
|
|
77
|
+
const negativesAtom = require_atom_with_messages.atomWithFieldsMessageTypes(validationResultsAtom, "negative");
|
|
78
|
+
const errorMessagesAtom = require_atom_with_messages.atomWithFieldsMessageList(validationResultsAtom, "danger");
|
|
79
|
+
const warningMessagesAtom = require_atom_with_messages.atomWithFieldsMessageList(validationResultsAtom, "warning");
|
|
80
|
+
const infoMessagesAtom = require_atom_with_messages.atomWithFieldsMessageList(validationResultsAtom, "info");
|
|
81
|
+
const discoveryMessagesAtom = require_atom_with_messages.atomWithFieldsMessageList(validationResultsAtom, "discovery");
|
|
82
|
+
const successMessagesAtom = require_atom_with_messages.atomWithFieldsMessageList(validationResultsAtom, "success");
|
|
83
|
+
const positiveMessagesAtom = require_atom_with_messages.atomWithFieldsMessageList(validationResultsAtom, "positive");
|
|
84
|
+
const negativeMessagesAtom = require_atom_with_messages.atomWithFieldsMessageList(validationResultsAtom, "negative");
|
|
85
|
+
const invalidAtom = (0, jotai.atom)((get) => {
|
|
86
|
+
return get(errorMessagesAtom).length > 0;
|
|
87
|
+
});
|
|
88
|
+
const validAtom = (0, jotai.atom)((get) => !get(invalidAtom));
|
|
89
|
+
const formattedValueAtom = (0, jotai.atom)((get) => {
|
|
90
|
+
const options = get(optionsAtom);
|
|
91
|
+
const value = get(valueAtom);
|
|
92
|
+
let formattedValue;
|
|
93
|
+
if (options?.format) formattedValue = options.format(value);
|
|
94
|
+
else formattedValue = value;
|
|
95
|
+
if (!(0, _stryke_type_checks_is_set.isSet)(formattedValue)) return "";
|
|
96
|
+
else if ((0, _stryke_type_checks_is_string.isString)(formattedValue)) return formattedValue;
|
|
97
|
+
return String(formattedValue);
|
|
98
|
+
});
|
|
99
|
+
const itemsAtom = require_atom_with_field.atomWithFieldItems(optionsAtom, valueAtom, disabledAtom);
|
|
100
|
+
const itemsAtomsAtom = (0, jotai_utils.splitAtom)(itemsAtom);
|
|
101
|
+
const sizeAtom = (0, jotai_utils.atomWithDefault)((get) => {
|
|
102
|
+
return get(optionsAtom).size ?? "md";
|
|
103
|
+
});
|
|
104
|
+
return {
|
|
105
|
+
options: optionsAtom,
|
|
106
|
+
form: formScopeAtom,
|
|
107
|
+
name: nameAtom,
|
|
108
|
+
path: pathAtom,
|
|
109
|
+
initialValue: initialValueAtom,
|
|
110
|
+
previousValue: (0, jotai.atom)((get) => {
|
|
111
|
+
const previousValues = get(form.previousValues);
|
|
112
|
+
return (0, _stryke_helpers_get_field.getField)(previousValues, get(pathAtom).join(".")) ?? null;
|
|
113
|
+
}),
|
|
114
|
+
value: valueAtom,
|
|
115
|
+
formattedValue: formattedValueAtom,
|
|
116
|
+
focused: focusedAtom,
|
|
117
|
+
required: requiredAtom,
|
|
118
|
+
disabled: disabledAtom,
|
|
119
|
+
clearable: clearableAtom,
|
|
120
|
+
touched: touchedAtom,
|
|
121
|
+
blurred: blurredAtom,
|
|
122
|
+
validating: validatingAtom,
|
|
123
|
+
validationResults: validationResultsAtom,
|
|
124
|
+
tabIndex: tabIndexAtom,
|
|
125
|
+
ref: refAtom,
|
|
126
|
+
pristine: (0, jotai.atom)((get) => !get(dirtyAtom)),
|
|
127
|
+
dirty: dirtyAtom,
|
|
128
|
+
items: itemsAtom,
|
|
129
|
+
itemsAtoms: itemsAtomsAtom,
|
|
130
|
+
size: sizeAtom,
|
|
131
|
+
errors: errorsAtom,
|
|
132
|
+
warnings: warningsAtom,
|
|
133
|
+
info: infoAtom,
|
|
134
|
+
discovery: discoveryAtom,
|
|
135
|
+
success: successesAtom,
|
|
136
|
+
positive: positivesAtom,
|
|
137
|
+
negative: negativesAtom,
|
|
138
|
+
dangerMessages: errorMessagesAtom,
|
|
139
|
+
warningMessages: warningMessagesAtom,
|
|
140
|
+
infoMessages: infoMessagesAtom,
|
|
141
|
+
discoveryMessages: discoveryMessagesAtom,
|
|
142
|
+
successMessages: successMessagesAtom,
|
|
143
|
+
positiveMessages: positiveMessagesAtom,
|
|
144
|
+
negativeMessages: negativeMessagesAtom,
|
|
145
|
+
theme: require_atom_with_messages.atomWithTheme(optionsAtom, errorMessagesAtom, warningMessagesAtom, infoMessagesAtom, discoveryMessagesAtom, successMessagesAtom, positiveMessagesAtom, negativeMessagesAtom),
|
|
146
|
+
messages: require_atom_with_messages.atomWithMessages(errorMessagesAtom, warningMessagesAtom, infoMessagesAtom, discoveryMessagesAtom, successMessagesAtom, positiveMessagesAtom, negativeMessagesAtom),
|
|
147
|
+
invalid: invalidAtom,
|
|
148
|
+
valid: validAtom
|
|
149
|
+
};
|
|
150
|
+
}, { type: "field" });
|
|
151
|
+
|
|
152
|
+
//#endregion
|
|
153
|
+
exports.DEFAULT_FIELD_OPTIONS = DEFAULT_FIELD_OPTIONS;
|
|
154
|
+
exports.FieldApi = FieldApi;
|