@goodie-forms/react 1.1.5-alpha → 1.2.0-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,46 +1,50 @@
1
- import { FormController, type Form } from "@goodie-forms/core";
2
- import { useEffect, useState } from "react";
3
- import { composeFns } from "../utils/composeFns";
4
- import { useRenderControl } from "../hooks/useRenderControl";
5
-
6
- export function useForm<TShape extends object>(
7
- formConfigs: Form.FormConfigs<TShape>,
8
- hookConfigs?: {
9
- validateMode?: "onChange" | "onBlur" | "onSubmit";
10
- revalidateMode?: "onChange" | "onBlur" | "onSubmit";
11
- watchIssues?: boolean;
12
- watchValues?: boolean;
13
- },
14
- ) {
15
- const [controller] = useState(() => new FormController(formConfigs));
16
-
17
- const renderControl = useRenderControl();
18
-
19
- useEffect(() => {
20
- const noop = () => {};
21
-
22
- return composeFns(
23
- controller.events.on("submissionStatusChange", () => {
24
- renderControl.forceRerender();
25
- }),
26
- hookConfigs?.watchIssues
27
- ? controller.events.on("fieldIssuesUpdated", () =>
28
- renderControl.forceRerender(),
29
- )
30
- : noop,
31
- hookConfigs?.watchValues
32
- ? controller.events.on("valueChanged", () =>
33
- renderControl.forceRerender(),
34
- )
35
- : noop,
36
- );
37
- }, [controller]);
38
-
39
- return {
40
- formConfigs,
41
- hookConfigs,
42
- controller,
43
- };
44
- }
45
-
46
- export type UseForm<TShape extends object> = ReturnType<typeof useForm<TShape>>;
1
+ import { FieldPathBuilder, FormController } from "@goodie-forms/core";
2
+ import { useEffect, useState } from "react";
3
+ import { useRenderControl } from "../hooks/useRenderControl";
4
+ import { composeFns } from "../utils/composeFns";
5
+
6
+ export function useForm<TOutput extends object>(
7
+ formConfigs: FormController.Configs<TOutput>,
8
+ hookConfigs?: {
9
+ validateMode?: "onChange" | "onBlur" | "onSubmit";
10
+ revalidateMode?: "onChange" | "onBlur" | "onSubmit";
11
+ watchIssues?: boolean;
12
+ watchValues?: boolean;
13
+ },
14
+ ) {
15
+ const [controller] = useState(() => new FormController(formConfigs));
16
+ const [paths] = useState(() => new FieldPathBuilder<TOutput>());
17
+
18
+ const renderControl = useRenderControl();
19
+
20
+ useEffect(() => {
21
+ const noop = () => {};
22
+
23
+ return composeFns(
24
+ controller.events.on("submissionStatusChange", () => {
25
+ renderControl.forceRerender();
26
+ }),
27
+ hookConfigs?.watchIssues
28
+ ? controller.events.on("fieldIssuesUpdated", () =>
29
+ renderControl.forceRerender(),
30
+ )
31
+ : noop,
32
+ hookConfigs?.watchValues
33
+ ? controller.events.on("valueChanged", () =>
34
+ renderControl.forceRerender(),
35
+ )
36
+ : noop,
37
+ );
38
+ }, [controller]);
39
+
40
+ return {
41
+ formConfigs,
42
+ paths,
43
+ hookConfigs,
44
+ controller,
45
+ };
46
+ }
47
+
48
+ export type UseForm<TOutput extends object> = ReturnType<
49
+ typeof useForm<TOutput>
50
+ >;
@@ -1,40 +1,44 @@
1
- import { Field } from "@goodie-forms/core";
2
- import { useEffect } from "react";
3
- import { composeFns } from "../utils/composeFns";
4
- import { groupBy } from "../utils/groupBy";
5
- import type { UseForm } from "./useForm";
6
- import { useRenderControl } from "./useRenderControl";
7
-
8
- export function useFormErrorObserver<TShape extends object>(
9
- form: UseForm<TShape>,
10
- options?: {
11
- include?: Field.Paths<TShape>[];
12
- },
13
- ) {
14
- const renderControl = useRenderControl();
15
-
16
- const filteredIssues = form.controller._issues.filter((issue) => {
17
- if (options?.include == null) return true;
18
- const path = Field.parsePath(issue.path!) as Field.Paths<TShape>;
19
- return options.include.includes(path);
20
- });
21
-
22
- const observedIssues = groupBy(
23
- filteredIssues,
24
- (issue) => Field.parsePath(issue.path!) as Field.Paths<TShape>,
25
- );
26
-
27
- useEffect(() => {
28
- const { events } = form.controller;
29
-
30
- return composeFns(
31
- events.on("fieldIssuesUpdated", (path) => {
32
- if (options?.include?.includes?.(path) ?? true) {
33
- renderControl.forceRerender();
34
- }
35
- }),
36
- );
37
- }, []);
38
-
39
- return observedIssues;
40
- }
1
+ import { FieldPath } from "@goodie-forms/core";
2
+ import { groupBy } from "../utils/groupBy";
3
+ import { useEffect } from "react";
4
+ import { composeFns } from "../utils/composeFns";
5
+ import type { UseForm } from "./useForm";
6
+ import { useRenderControl } from "./useRenderControl";
7
+
8
+ export function useFormErrorObserver<
9
+ TOutput extends object,
10
+ TInclude extends FieldPath.Segments[] | undefined = undefined,
11
+ >(
12
+ form: UseForm<TOutput>,
13
+ options?: {
14
+ include?: TInclude;
15
+ },
16
+ ) {
17
+ const renderControl = useRenderControl();
18
+
19
+ const observedIssues = form.controller._issues.filter((issue) => {
20
+ if (options?.include == null) return true;
21
+ const normalizedIssuePath = FieldPath.normalize(issue.path);
22
+ return options.include.some((path) =>
23
+ FieldPath.equals(path, normalizedIssuePath),
24
+ );
25
+ });
26
+
27
+ useEffect(() => {
28
+ const { events } = form.controller;
29
+
30
+ return composeFns(
31
+ events.on("fieldIssuesUpdated", (path) => {
32
+ if (options?.include?.includes?.(path) ?? true) {
33
+ renderControl.forceRerender();
34
+ }
35
+ }),
36
+ );
37
+ }, []);
38
+
39
+ return groupBy(observedIssues, (issue) =>
40
+ issue.path == null
41
+ ? "$"
42
+ : FieldPath.toStringPath(FieldPath.normalize(issue.path)),
43
+ );
44
+ }
@@ -1,55 +1,63 @@
1
- import { Field } from "@goodie-forms/core";
2
- import { useEffect, useState } from "react";
3
- import { UseForm } from "../hooks/useForm";
4
- import { useRenderControl } from "../hooks/useRenderControl";
5
- import { composeFns } from "../utils/composeFns";
6
-
7
- export function useFormField<
8
- TShape extends object,
9
- TPath extends Field.Paths<TShape>
10
- >(
11
- form: UseForm<TShape>,
12
- path: TPath,
13
- bindingConfig?: Parameters<typeof form.controller.bindField<TPath>>[1]
14
- ) {
15
- const renderControl = useRenderControl();
16
-
17
- const [field, setField] = useState(() => {
18
- let field = form.controller.getField(path);
19
- if (field == null && bindingConfig != null) {
20
- field = form.controller.bindField(path, bindingConfig);
21
- }
22
- return field;
23
- });
24
-
25
- useEffect(() => {
26
- const { events } = form.controller;
27
-
28
- setField(form.controller.getField(path));
29
-
30
- return composeFns(
31
- events.on("fieldBound", (_path) => {
32
- if (_path === path) setField(form.controller.getField(path));
33
- }),
34
- events.on("fieldUnbound", (_path) => {
35
- if (_path === path) setField(undefined);
36
- }),
37
- events.on("valueChanged", (changedPath) => {
38
- if (changedPath === path || Field.isDescendant(changedPath, path)) {
39
- renderControl.forceRerender();
40
- }
41
- }),
42
- events.on("fieldTouchUpdated", (_path) => {
43
- if (_path === path) renderControl.forceRerender();
44
- }),
45
- events.on("fieldDirtyUpdated", (_path) => {
46
- if (_path === path) renderControl.forceRerender();
47
- }),
48
- events.on("fieldIssuesUpdated", (_path) => {
49
- if (_path === path) renderControl.forceRerender();
50
- })
51
- );
52
- }, []);
53
-
54
- return field;
55
- }
1
+ import { FieldPath } from "@goodie-forms/core";
2
+ import { useEffect, useState } from "react";
3
+ import { UseForm } from "../hooks/useForm";
4
+ import { useRenderControl } from "../hooks/useRenderControl";
5
+ import { composeFns } from "../utils/composeFns";
6
+
7
+ export function useFormField<
8
+ TOutput extends object,
9
+ TPath extends FieldPath.Segments,
10
+ >(
11
+ form: UseForm<TOutput>,
12
+ path: TPath,
13
+ bindingConfig?: Parameters<typeof form.controller.bindField<TPath>>[1],
14
+ ) {
15
+ const renderControl = useRenderControl();
16
+
17
+ const [field, setField] = useState(() => {
18
+ let field = form.controller.getField(path);
19
+ if (field == null && bindingConfig != null) {
20
+ field = form.controller.bindField(path, bindingConfig);
21
+ }
22
+ return field;
23
+ });
24
+
25
+ useEffect(() => {
26
+ const { events } = form.controller;
27
+
28
+ setField(form.controller.getField(path));
29
+
30
+ return composeFns(
31
+ events.on("fieldBound", (_path) => {
32
+ if (!FieldPath.equals(_path, path)) return;
33
+ setField(form.controller.getField(path));
34
+ }),
35
+ events.on("fieldUnbound", (_path) => {
36
+ if (!FieldPath.equals(_path, path)) return;
37
+ setField(undefined);
38
+ }),
39
+ events.on("valueChanged", (changedPath) => {
40
+ if (
41
+ FieldPath.equals(changedPath, path) ||
42
+ FieldPath.isDescendant(changedPath, path)
43
+ ) {
44
+ renderControl.forceRerender();
45
+ }
46
+ }),
47
+ events.on("fieldTouchUpdated", (_path) => {
48
+ if (!FieldPath.equals(_path, path)) return;
49
+ renderControl.forceRerender();
50
+ }),
51
+ events.on("fieldDirtyUpdated", (_path) => {
52
+ if (!FieldPath.equals(_path, path)) return;
53
+ renderControl.forceRerender();
54
+ }),
55
+ events.on("fieldIssuesUpdated", (_path) => {
56
+ if (!FieldPath.equals(_path, path)) return;
57
+ renderControl.forceRerender();
58
+ }),
59
+ );
60
+ }, []);
61
+
62
+ return field;
63
+ }
@@ -1,45 +1,49 @@
1
- import { Field } from "@goodie-forms/core";
2
- import { useEffect } from "react";
3
- import { composeFns } from "../utils/composeFns";
4
- import type { UseForm } from "./useForm";
5
- import { useRenderControl } from "./useRenderControl";
6
-
7
- export function useFormValuesObserver<
8
- TShape extends object,
9
- TPaths extends Field.Paths<TShape>[] | undefined = undefined
10
- >(
11
- form: UseForm<TShape>,
12
- options?: {
13
- include?: TPaths;
14
- }
15
- ) {
16
- const renderControl = useRenderControl();
17
-
18
- const observedValues =
19
- options?.include == null
20
- ? form.controller._data
21
- : options.include.reduce((data, path) => {
22
- const value = Field.getValue(form.controller._data as TShape, path)!;
23
- Field.setValue(data, path, value);
24
- return data;
25
- }, {} as TShape);
26
-
27
- useEffect(() => {
28
- const { events } = form.controller;
29
-
30
- return composeFns(
31
- events.on("valueChanged", (changedPath) => {
32
- const watchingChange =
33
- options?.include == null
34
- ? true
35
- : options.include.some(
36
- (path) =>
37
- path === changedPath || Field.isDescendant(path, changedPath)
38
- );
39
- if (watchingChange) renderControl.forceRerender();
40
- })
41
- );
42
- }, []);
43
-
44
- return observedValues;
45
- }
1
+ import { FieldPath } from "@goodie-forms/core";
2
+ import { useEffect } from "react";
3
+ import { composeFns } from "../utils/composeFns";
4
+ import type { UseForm } from "./useForm";
5
+ import { useRenderControl } from "./useRenderControl";
6
+
7
+ export function useFormValuesObserver<
8
+ TOutput extends object,
9
+ TPaths extends FieldPath.Segments[] | undefined = undefined,
10
+ >(
11
+ form: UseForm<TOutput>,
12
+ options?: {
13
+ include?: TPaths;
14
+ },
15
+ ) {
16
+ const renderControl = useRenderControl();
17
+
18
+ const observedValues =
19
+ options?.include == null
20
+ ? form.controller._data
21
+ : options.include.reduce((data, path) => {
22
+ const value = FieldPath.getValue(
23
+ form.controller._data as TOutput,
24
+ path,
25
+ )!;
26
+ FieldPath.setValue(data, path, value);
27
+ return data;
28
+ }, {} as TOutput);
29
+
30
+ useEffect(() => {
31
+ const { events } = form.controller;
32
+
33
+ return composeFns(
34
+ events.on("valueChanged", (changedPath) => {
35
+ const watchingChange =
36
+ options?.include == null
37
+ ? true
38
+ : options.include.some(
39
+ (path) =>
40
+ FieldPath.equals(path, changedPath) ||
41
+ FieldPath.isDescendant(path, changedPath),
42
+ );
43
+ if (watchingChange) renderControl.forceRerender();
44
+ }),
45
+ );
46
+ }, []);
47
+
48
+ return observedValues;
49
+ }
@@ -1,26 +1,26 @@
1
- import { startTransition, useRef, useState } from "react";
2
-
3
- export function useRenderControl() {
4
- const [, rerender] = useState(0);
5
- const renderCount = useRef(0);
6
- const renderScheduled = useRef(false);
7
- renderCount.current++;
8
-
9
- const scheduleRerender = () => {
10
- if (renderScheduled.current) return;
11
- renderScheduled.current = true;
12
-
13
- queueMicrotask(() => {
14
- startTransition(() => {
15
- rerender((i) => i + 1);
16
- });
17
-
18
- renderScheduled.current = false;
19
- });
20
- };
21
-
22
- return {
23
- renderCount: renderCount.current,
24
- forceRerender: scheduleRerender,
25
- };
26
- }
1
+ import { startTransition, useRef, useState } from "react";
2
+
3
+ export function useRenderControl() {
4
+ const [, rerender] = useState(0);
5
+ const renderCount = useRef(0);
6
+ const renderScheduled = useRef(false);
7
+ renderCount.current++;
8
+
9
+ const scheduleRerender = () => {
10
+ if (renderScheduled.current) return;
11
+ renderScheduled.current = true;
12
+
13
+ queueMicrotask(() => {
14
+ startTransition(() => {
15
+ rerender((i) => i + 1);
16
+ });
17
+
18
+ renderScheduled.current = false;
19
+ });
20
+ };
21
+
22
+ return {
23
+ renderCount: renderCount.current,
24
+ forceRerender: scheduleRerender,
25
+ };
26
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- export * from "./hooks/useForm";
2
- export * from "./hooks/useFormField";
3
- export * from "./hooks/useFormValuesObserver";
4
- export * from "./hooks/useFormErrorObserver";
5
- export * from "./hooks/useRenderControl";
6
-
7
- export * from "./components/FieldRenderer";
1
+ export * from "./hooks/useForm";
2
+ export * from "./hooks/useFormField";
3
+ export * from "./hooks/useFormValuesObserver";
4
+ export * from "./hooks/useFormErrorObserver";
5
+ export * from "./hooks/useRenderControl";
6
+
7
+ export * from "./components/FieldRenderer";
@@ -1,7 +1,7 @@
1
- export function composeFns<TFns extends (() => void)[]>(...fns: TFns) {
2
- return () => {
3
- for (const fn of fns) {
4
- fn();
5
- }
6
- };
7
- }
1
+ export function composeFns<TFns extends (() => void)[]>(...fns: TFns) {
2
+ return () => {
3
+ for (const fn of fns) {
4
+ fn();
5
+ }
6
+ };
7
+ }
@@ -1,13 +1,13 @@
1
- export function groupBy<T, K extends PropertyKey>(
2
- items: readonly T[],
3
- key: (item: T) => K,
4
- ): Record<K, T[]> {
5
- const result = {} as Record<K, T[]>;
6
-
7
- for (const item of items) {
8
- const k = key(item);
9
- (result[k] ??= []).push(item);
10
- }
11
-
12
- return result;
13
- }
1
+ export function groupBy<T, K extends PropertyKey>(
2
+ items: readonly T[],
3
+ key: (item: T) => K,
4
+ ): Record<K, T[]> {
5
+ const result = {} as Record<K, T[]>;
6
+
7
+ for (const item of items) {
8
+ const k = key(item);
9
+ (result[k] ??= []).push(item);
10
+ }
11
+
12
+ return result;
13
+ }
package/tsconfig.json CHANGED
@@ -1,8 +1,8 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "outDir": "dist",
5
- "jsx": "react-jsx"
6
- },
7
- "include": ["src"]
8
- }
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "jsx": "react-jsx"
6
+ },
7
+ "include": ["src"]
8
+ }
package/vite.config.ts CHANGED
@@ -1,23 +1,23 @@
1
- import { defineConfig } from "vite";
2
- import react from "@vitejs/plugin-react";
3
- import dts from "vite-plugin-dts";
4
-
5
- export default defineConfig({
6
- plugins: [react(), dts({ entryRoot: "src" })],
7
- build: {
8
- lib: {
9
- entry: "src/index.ts",
10
- formats: ["es"],
11
- fileName: "index",
12
- },
13
- rollupOptions: {
14
- external: [
15
- "react",
16
- "react-dom",
17
- "react/jsx-runtime",
18
- "@goodie-forms/core",
19
- ],
20
- },
21
- sourcemap: true,
22
- },
23
- });
1
+ import { defineConfig } from "vite";
2
+ import react from "@vitejs/plugin-react";
3
+ import dts from "vite-plugin-dts";
4
+
5
+ export default defineConfig({
6
+ plugins: [react(), dts({ entryRoot: "src" })],
7
+ build: {
8
+ lib: {
9
+ entry: "src/index.ts",
10
+ formats: ["es"],
11
+ fileName: "index",
12
+ },
13
+ rollupOptions: {
14
+ external: [
15
+ "react",
16
+ "react-dom",
17
+ "react/jsx-runtime",
18
+ "@goodie-forms/core",
19
+ ],
20
+ },
21
+ sourcemap: true,
22
+ },
23
+ });