1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| import type {
| BaseFormComponentType,
| ExtendedFormApi,
| VbenFormProps,
| } from './types';
|
| import { defineComponent, h, isReactive, onBeforeUnmount, watch } from 'vue';
|
| import { useStore } from '@vben-core/shared/store';
|
| import { FormApi } from './form-api';
| import VbenUseForm from './vben-use-form.vue';
|
| export function useVbenForm<
| T extends BaseFormComponentType = BaseFormComponentType,
| >(options: VbenFormProps<T>) {
| const IS_REACTIVE = isReactive(options);
| const api = new FormApi(options);
| const extendedApi: ExtendedFormApi = api as never;
| extendedApi.useStore = (selector) => {
| return useStore(api.store, selector);
| };
|
| const Form = defineComponent(
| (props: VbenFormProps, { attrs, slots }) => {
| onBeforeUnmount(() => {
| api.unmount();
| });
| api.setState({ ...props, ...attrs });
| return () =>
| h(VbenUseForm, { ...props, ...attrs, formApi: extendedApi }, slots);
| },
| {
| inheritAttrs: false,
| name: 'VbenUseForm',
| },
| );
| // Add reactivity support
| if (IS_REACTIVE) {
| watch(
| () => options.schema,
| () => {
| api.setState({ schema: options.schema });
| },
| { immediate: true },
| );
| }
|
| return [Form, extendedApi] as const;
| }
|
|