create.vue
1.53 KB
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
<script lang="ts" setup>
  import type { CreateModalDefineExposeType } from '../../../types';
  import { BasicForm, useForm } from '/@/components/Form';
  import { ChangeOriginatorFieldsEnum, formSchemas } from './create.config';
  import { NodeData } from '../../../types/node';
  import { RelationsQuery } from '../../../src/components/RelationsQuery';
  import { ref, unref } from 'vue';
  defineProps<{
    config: NodeData;
  }>();
  const relationsQueryElRef = ref<Nullable<InstanceType<typeof RelationsQuery>>>();
  const [register, { validate, getFieldsValue, setFieldsValue, resetFields }] = useForm({
    schemas: formSchemas,
    showActionButtonGroup: false,
  });
  const getValue: CreateModalDefineExposeType['getFieldsValue'] = async () => {
    await validate();
    const value = getFieldsValue() || {};
    const relationsQuery = unref(relationsQueryElRef)?.getFieldsValue();
    return { ...value, [ChangeOriginatorFieldsEnum.RELATIONS_QUERY]: relationsQuery };
  };
  const setValue: CreateModalDefineExposeType['setFieldsValue'] = (value) => {
    resetFields();
    setFieldsValue(value);
    unref(relationsQueryElRef)?.setFieldsValue(value?.[ChangeOriginatorFieldsEnum.RELATIONS_QUERY]);
  };
  defineExpose({
    setFieldsValue: setValue,
    getFieldsValue: getValue,
  } as CreateModalDefineExposeType);
</script>
<template>
  <BasicForm @register="register">
    <template #relationsQuery="{ field, model }">
      <RelationsQuery ref="relationsQueryElRef" v-model:value="model[field]" />
    </template>
  </BasicForm>
</template>