@goodie-forms/react 1.1.6-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,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
+ });