|
1 | 1 | import type { ErrorObject } from "ajv"; |
2 | | -import { useForm, type FormAPI, type UseFormOptions } from "@sjsf/form"; |
| 2 | +import { useForm2, type FormAPI, type UseFormOptions2 } from "@sjsf/form"; |
3 | 3 | import { translation } from "@sjsf/form/translations/en"; |
4 | 4 | import { theme } from "@sjsf/form/basic-theme"; |
5 | 5 | import { createValidator } from "@sjsf/ajv8-validator"; |
6 | 6 |
|
7 | 7 | type Defaults = "widgets" | "components" | "validator" | "translation"; |
8 | 8 |
|
9 | | -export type CustomOptions<T, E> = Omit<UseFormOptions<T, E>, Defaults> & |
10 | | - Partial<Pick<UseFormOptions<T, E>, Defaults>>; |
| 9 | +export type CustomOptions<T, E> = Omit<UseFormOptions2<T, E>, Defaults> & |
| 10 | + Partial<Pick<UseFormOptions2<T, E>, Defaults>>; |
11 | 11 |
|
12 | | -export function useCustomForm<T, E = ErrorObject>(options: CustomOptions<T, E>): FormAPI<T, E> { |
| 12 | +export function useCustomForm<T, E = ErrorObject>( |
| 13 | + options: CustomOptions<T, E> |
| 14 | +): FormAPI<T, E> { |
13 | 15 | const validator = createValidator(); |
14 | | - return useForm( |
15 | | - Object.setPrototypeOf(options, { |
16 | | - ...theme, |
17 | | - validator, |
18 | | - translation, |
19 | | - }) |
| 16 | + const defaults: Pick<UseFormOptions2<T, ErrorObject>, Defaults> = { |
| 17 | + ...theme, |
| 18 | + validator, |
| 19 | + translation, |
| 20 | + }; |
| 21 | + return useForm2( |
| 22 | + new Proxy(options, { |
| 23 | + get(target, p, receiver) { |
| 24 | + if (!(p in target)) { |
| 25 | + return defaults[p as Defaults]; |
| 26 | + } |
| 27 | + return Reflect.get(target, p, receiver); |
| 28 | + }, |
| 29 | + }) as UseFormOptions2<T, E> |
20 | 30 | ); |
21 | 31 | } |
0 commit comments