@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.
- package/LICENSE +426 -426
- package/dist/components/FieldRenderer.d.ts +9 -9
- package/dist/components/FieldRenderer.d.ts.map +1 -1
- package/dist/hooks/useForm.d.ts +9 -8
- package/dist/hooks/useForm.d.ts.map +1 -1
- package/dist/hooks/useFormErrorObserver.d.ts +2 -2
- package/dist/hooks/useFormErrorObserver.d.ts.map +1 -1
- package/dist/hooks/useFormField.d.ts +2 -2
- package/dist/hooks/useFormField.d.ts.map +1 -1
- package/dist/hooks/useFormValuesObserver.d.ts +3 -3
- package/dist/hooks/useFormValuesObserver.d.ts.map +1 -1
- package/dist/index.js +82 -76
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/FieldRenderer.tsx +166 -120
- package/src/hooks/useForm.tsx +50 -55
- package/src/hooks/useFormErrorObserver.ts +44 -42
- package/src/hooks/useFormField.tsx +63 -55
- package/src/hooks/useFormValuesObserver.ts +49 -45
- package/src/hooks/useRenderControl.tsx +26 -26
- package/src/index.ts +7 -7
- package/src/utils/composeFns.ts +7 -7
- package/src/utils/groupBy.ts +13 -13
- package/tsconfig.json +8 -8
- package/vite.config.ts +23 -23
|
@@ -1,45 +1,49 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
9
|
-
TPaths extends
|
|
10
|
-
>(
|
|
11
|
-
form: UseForm<
|
|
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 =
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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";
|
package/src/utils/composeFns.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/utils/groupBy.ts
CHANGED
|
@@ -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
|
+
});
|