@dxos/react-ui-form 0.7.3-main.2dd075e → 0.7.3-staging.cc8dd3e
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/dist/lib/browser/index.mjs +15 -8
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +15 -8
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +15 -8
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/hooks/useForm.d.ts +2 -1
- package/dist/types/src/hooks/useForm.d.ts.map +1 -1
- package/package.json +23 -23
- package/src/hooks/useForm.ts +12 -4
package/src/hooks/useForm.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { type SimpleType, type S } from '@dxos/effect';
|
|
|
9
9
|
import { invariant } from '@dxos/invariant';
|
|
10
10
|
import { log } from '@dxos/log';
|
|
11
11
|
import { validateSchema, type ValidationError } from '@dxos/schema';
|
|
12
|
+
import { type MaybePromise } from '@dxos/util';
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Return type from `useForm` hook.
|
|
@@ -74,7 +75,7 @@ export interface FormOptions<T extends BaseObject> {
|
|
|
74
75
|
/**
|
|
75
76
|
* Called when the form is submitted and passes validation.
|
|
76
77
|
*/
|
|
77
|
-
onSave?: (values: T, meta: { changed: FormHandler<T>['changed'] }) => void
|
|
78
|
+
onSave?: (values: T, meta: { changed: FormHandler<T>['changed'] }) => MaybePromise<void>;
|
|
78
79
|
}
|
|
79
80
|
|
|
80
81
|
/**
|
|
@@ -97,6 +98,7 @@ export const useForm = <T extends BaseObject>({
|
|
|
97
98
|
const [touched, setTouched] = useState<Record<PropertyKey<T>, boolean>>(createKeySet(initialValues, false));
|
|
98
99
|
const [changed, setChanged] = useState<Record<PropertyKey<T>, boolean>>(createKeySet(initialValues, false));
|
|
99
100
|
const [errors, setErrors] = useState<Record<PropertyKey<T>, string>>({} as Record<PropertyKey<T>, string>);
|
|
101
|
+
const [saving, setSaving] = useState(false);
|
|
100
102
|
|
|
101
103
|
//
|
|
102
104
|
// Validation.
|
|
@@ -132,15 +134,21 @@ export const useForm = <T extends BaseObject>({
|
|
|
132
134
|
*/
|
|
133
135
|
const canSave = useMemo(
|
|
134
136
|
() =>
|
|
137
|
+
!saving &&
|
|
135
138
|
Object.keys(values).every(
|
|
136
139
|
(property) => touched[property as PropertyKey<T>] === false || !errors[property as PropertyKey<T>],
|
|
137
140
|
),
|
|
138
|
-
[values, touched, errors],
|
|
141
|
+
[values, touched, errors, saving],
|
|
139
142
|
);
|
|
140
143
|
|
|
141
|
-
const handleSave = useCallback(() => {
|
|
144
|
+
const handleSave = useCallback(async () => {
|
|
142
145
|
if (validate(values)) {
|
|
143
|
-
|
|
146
|
+
setSaving(true);
|
|
147
|
+
try {
|
|
148
|
+
await onSave?.(values, { changed });
|
|
149
|
+
} finally {
|
|
150
|
+
setSaving(false);
|
|
151
|
+
}
|
|
144
152
|
}
|
|
145
153
|
}, [values, validate, onSave]);
|
|
146
154
|
|