@explita/formly 0.1.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.
Files changed (61) hide show
  1. package/README.md +261 -0
  2. package/README.old.md +141 -0
  3. package/dist/components/field-error.d.ts +2 -0
  4. package/dist/components/field-error.js +14 -0
  5. package/dist/components/field.d.ts +4 -0
  6. package/dist/components/field.js +120 -0
  7. package/dist/components/form-spy.d.ts +16 -0
  8. package/dist/components/form-spy.js +66 -0
  9. package/dist/components/index.d.ts +4 -0
  10. package/dist/components/index.js +20 -0
  11. package/dist/components/label.d.ts +2 -0
  12. package/dist/components/label.js +46 -0
  13. package/dist/hooks/use-field.d.ts +21 -0
  14. package/dist/hooks/use-field.js +66 -0
  15. package/dist/hooks/use-form-by-id.d.ts +6 -0
  16. package/dist/hooks/use-form-by-id.js +25 -0
  17. package/dist/hooks/use-form-context.d.ts +5 -0
  18. package/dist/hooks/use-form-context.js +17 -0
  19. package/dist/hooks/use-form.d.ts +43 -0
  20. package/dist/hooks/use-form.js +961 -0
  21. package/dist/index.d.ts +9 -0
  22. package/dist/index.js +25 -0
  23. package/dist/lib/array-helpers.d.ts +6 -0
  24. package/dist/lib/array-helpers.js +281 -0
  25. package/dist/lib/css.d.ts +1 -0
  26. package/dist/lib/css.js +45 -0
  27. package/dist/lib/debounce.d.ts +13 -0
  28. package/dist/lib/debounce.js +28 -0
  29. package/dist/lib/deep-path.d.ts +4 -0
  30. package/dist/lib/deep-path.js +60 -0
  31. package/dist/lib/drafts-helpter.d.ts +31 -0
  32. package/dist/lib/drafts-helpter.js +67 -0
  33. package/dist/lib/form-registry.d.ts +9 -0
  34. package/dist/lib/form-registry.js +24 -0
  35. package/dist/lib/group-helpers.d.ts +9 -0
  36. package/dist/lib/group-helpers.js +29 -0
  37. package/dist/lib/pub-sub.d.ts +13 -0
  38. package/dist/lib/pub-sub.js +38 -0
  39. package/dist/lib/utils.d.ts +17 -0
  40. package/dist/lib/utils.js +190 -0
  41. package/dist/lib/validation.d.ts +22 -0
  42. package/dist/lib/validation.js +46 -0
  43. package/dist/lib/zod-helpers.d.ts +5 -0
  44. package/dist/lib/zod-helpers.js +63 -0
  45. package/dist/providers/form.d.ts +51 -0
  46. package/dist/providers/form.js +63 -0
  47. package/dist/types/array.d.ts +197 -0
  48. package/dist/types/array.js +2 -0
  49. package/dist/types/field.d.ts +61 -0
  50. package/dist/types/field.js +2 -0
  51. package/dist/types/group.d.ts +16 -0
  52. package/dist/types/group.js +2 -0
  53. package/dist/types/path.d.ts +8 -0
  54. package/dist/types/path.js +2 -0
  55. package/dist/types/pub-sub.d.ts +2 -0
  56. package/dist/types/pub-sub.js +2 -0
  57. package/dist/types/utils.d.ts +310 -0
  58. package/dist/types/utils.js +2 -0
  59. package/dist/utils/index.d.ts +4 -0
  60. package/dist/utils/index.js +14 -0
  61. package/package.json +53 -0
@@ -0,0 +1,46 @@
1
+ "use client";
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.Label = Label;
38
+ const React = __importStar(require("react"));
39
+ const utils_1 = require("../lib/utils");
40
+ const field_1 = require("./field");
41
+ function Label({ className, ...props }) {
42
+ const { id, required, hasError, label } = (0, field_1.useFieldContext)();
43
+ if (!label)
44
+ return;
45
+ return (React.createElement("label", { "data-slot": "label", htmlFor: id, "data-required": required, "aria-required": required, "data-error": hasError, "aria-invalid": hasError, className: (0, utils_1.cn)("form-label", className), ...props }, label));
46
+ }
@@ -0,0 +1,21 @@
1
+ import { Path } from "../types/path";
2
+ import { FieldRegistrationOptions, InputChangeEvent } from "../types/utils";
3
+ export declare function useField<T extends Record<string, any>>(name: string, options?: FieldRegistrationOptions<T, Path<T>>): {
4
+ value: string;
5
+ error: string | undefined;
6
+ hasError: boolean;
7
+ isTouched: boolean;
8
+ setValue: (value: any) => void;
9
+ reset: () => void;
10
+ focus: () => void;
11
+ refId: string;
12
+ bind: () => {
13
+ name: string;
14
+ value: string;
15
+ onChange: (e: InputChangeEvent) => void;
16
+ onBlur: () => void;
17
+ "data-input-ref": string;
18
+ "data-input-error": boolean;
19
+ "aria-invalid": boolean;
20
+ };
21
+ };
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useField = useField;
4
+ const use_form_context_1 = require("./use-form-context");
5
+ const react_1 = require("react");
6
+ function useField(name, options) {
7
+ const form = (0, use_form_context_1.useFormContext)();
8
+ //@ts-ignore
9
+ form.register(name, options, true);
10
+ const [value, setValue] = (0, react_1.useState)("");
11
+ const [error, setError] = (0, react_1.useState)("");
12
+ //@ts-ignore
13
+ const [isDirty, setIsDirty] = (0, react_1.useState)(form.isDirty(name));
14
+ //@ts-ignore
15
+ const [isTouched, setIsTouched] = (0, react_1.useState)(form.isTouched(name));
16
+ const refId = name + "-" + (0, react_1.useId)();
17
+ (0, react_1.useEffect)(() => {
18
+ if (!form || !name)
19
+ return;
20
+ //@ts-ignore
21
+ form.subscribe(name, setValue, { internalRef: refId });
22
+ //@ts-ignore
23
+ form.subscribeFieldError(name, setError);
24
+ return () => {
25
+ //@ts-ignore
26
+ form.unsubscribeField(name, setValue);
27
+ };
28
+ }, [name]);
29
+ // Field helpers
30
+ const bind = {
31
+ name,
32
+ value: value || "",
33
+ onChange: (e) => {
34
+ form.field(name).set(e.currentTarget.value);
35
+ setValue(e.currentTarget.value);
36
+ setIsDirty(true);
37
+ },
38
+ onBlur: () => {
39
+ //@ts-ignore
40
+ form.markTouched(name);
41
+ setIsTouched(true);
42
+ //@ts-ignore
43
+ if (form.validateOn === "blur")
44
+ form.field(name).validate();
45
+ },
46
+ "data-input-ref": refId,
47
+ "data-input-error": !!error,
48
+ "aria-invalid": !!error,
49
+ };
50
+ return {
51
+ value,
52
+ error,
53
+ hasError: !!error,
54
+ isTouched,
55
+ setValue: (value) => form.field(name).set(value),
56
+ reset: () => {
57
+ //@ts-ignore
58
+ form.resetField(name);
59
+ setValue("");
60
+ },
61
+ //@ts-ignore
62
+ focus: () => form.focus(name),
63
+ refId,
64
+ bind: () => bind,
65
+ };
66
+ }
@@ -0,0 +1,6 @@
1
+ import { FormInstance as BaseInstance } from "../types/utils";
2
+ type FormInstance<T> = Omit<BaseInstance<T>, "isDirty" | "Field"> & {
3
+ isDirty: boolean;
4
+ };
5
+ export declare function useFormById<T extends Record<string, any>>(id: string, field?: string): FormInstance<T>;
6
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useFormById = useFormById;
4
+ const react_1 = require("react");
5
+ const form_registry_1 = require("../lib/form-registry");
6
+ function useFormById(id, field) {
7
+ const [, setTick] = (0, react_1.useState)(0);
8
+ const form = (0, react_1.useMemo)(() => form_registry_1.registry.get(id), [id]);
9
+ (0, react_1.useEffect)(() => {
10
+ // trigger initial render so values are pulled into React
11
+ setTick((t) => t + 1);
12
+ const channelName = field !== null && field !== void 0 ? field : "*";
13
+ const unsub = form
14
+ //@ts-ignore
15
+ .channel(`value:${channelName}`)
16
+ .subscribe(() => setTick((t) => t + 1));
17
+ return unsub;
18
+ }, [form, field]);
19
+ return {
20
+ ...form,
21
+ get isDirty() {
22
+ return form.isDirty();
23
+ },
24
+ };
25
+ }
@@ -0,0 +1,5 @@
1
+ import { FormContextValue } from "../providers/form";
2
+ import type { z } from "zod";
3
+ export declare function useFormContext<TDefaultValues extends Record<string, any> = Record<string, any>, TSchema extends z.ZodObject<any> | undefined = undefined>(props?: {
4
+ schema?: TSchema;
5
+ }): FormContextValue<TSchema, TDefaultValues>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useFormContext = useFormContext;
4
+ const react_1 = require("react");
5
+ const form_1 = require("../providers/form");
6
+ function useFormContext(props) {
7
+ const form = (0, react_1.useContext)(form_1.FormContext);
8
+ if (!form) {
9
+ throw new Error("useFormContext must be used within a FormProvider");
10
+ }
11
+ // Handle schema updates if provided
12
+ (0, react_1.useEffect)(() => {
13
+ //@ts-ignore
14
+ form.setSchema(props === null || props === void 0 ? void 0 : props.schema);
15
+ }, [props === null || props === void 0 ? void 0 : props.schema]);
16
+ return form;
17
+ }
@@ -0,0 +1,43 @@
1
+ import type { z } from "zod";
2
+ import { FormInstance, FormValues } from "../types/utils";
3
+ /**
4
+ * useForm is a React hook that allows you to manage form state and validation.
5
+ *
6
+ * @param schema - a Zod schema object that defines the form structure and validation rules.
7
+ * @param defaultValues - an object containing the default values for the form fields.
8
+ * @param errors - an object containing the initial errors for the form fields.
9
+ * @param mode - the form mode, either "controlled" or "uncontrolled".
10
+ * @param errorParser - a function that takes an error and returns a string to be displayed to the user.
11
+ * @param persistKey - a string that defines the key under which the form state is persisted.
12
+ * @param check - a function that takes the form values and an object containing the focus method for each field.
13
+ * @param onSubmit - a function that takes the form values and an object containing the focus method for each field.
14
+ * @param persistKey - a string that defines the key under which the form state is persisted.
15
+ * @param computed - an object containing the computed fields.
16
+ * @param autoFocusOnError - a boolean that defines whether to focus on the first error field.
17
+ * @param savedFormFirst - a boolean that defines whether to prioritize the saved form state.
18
+ * @param validateOn - a string that defines when to validate the form.
19
+ *
20
+ * @returns an object containing the useForm API methods.
21
+ *
22
+ * @example
23
+ * const form = useForm({
24
+ * schema,
25
+ * defaultValues,
26
+ * errors,
27
+ * mode,
28
+ * errorParser,
29
+ * persistKey,
30
+ * check,
31
+ * onSubmit: async(values)=>{}
32
+ * });
33
+ *
34
+ * <Form use={form}>
35
+ * {formFields.map((field) => (
36
+ * <div key={field.name}>
37
+ * <label>{field.label}</label>
38
+ * <input type="text" {...register(field.name)} />
39
+ * </div>
40
+ * ))}
41
+ * </Form>
42
+ */
43
+ export declare function useForm<TSchema extends z.ZodObject<any> | undefined = undefined, TDefault = TSchema extends z.ZodObject<any> ? z.infer<TSchema> : Record<string, any>>(options?: FormValues<TSchema, TDefault>): FormInstance<TSchema extends z.ZodObject<any> ? z.infer<TSchema> : TDefault>;