create.vue
2.27 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<script lang="ts" setup>
  import type { CreateModalDefineExposeType } from '../../../types';
  import { BasicForm, useForm } from '/@/components/Form';
  import { formSchemas, RestApiCallFieldsEnum } from './create.config';
  import { NodeData } from '../../../types/node';
  import { AttributeConfiguration } from '../../../src/components/AttributeConfiguration';
  import { ref, unref } from 'vue';
  import { CredentialsCard } from '../Mqtt/CredentialsCard';
  defineProps<{
    config: NodeData;
  }>();
  const attributeConfigurationElRef = ref<Nullable<InstanceType<typeof AttributeConfiguration>>>();
  const credentialsCardElRef = ref<Nullable<InstanceType<typeof CredentialsCard>>>();
  const [register, { validate, getFieldsValue, setFieldsValue, resetFields }] = useForm({
    schemas: formSchemas,
    showActionButtonGroup: false,
  });
  const getValue: CreateModalDefineExposeType['getFieldsValue'] = async () => {
    await validate();
    await unref(attributeConfigurationElRef)?.validate();
    await unref(credentialsCardElRef)?.validate();
    const value = getFieldsValue() || {};
    const headers = unref(attributeConfigurationElRef)?.getFieldsValue();
    const credentials = unref(credentialsCardElRef)?.getFieldsValue();
    return {
      ...value,
      [RestApiCallFieldsEnum.HEADERS]: headers,
      [RestApiCallFieldsEnum.CREDENTIALS]: credentials,
    };
  };
  const setValue: CreateModalDefineExposeType['setFieldsValue'] = (value) => {
    resetFields();
    setFieldsValue(value);
    unref(attributeConfigurationElRef)?.setFieldsValue(value?.[RestApiCallFieldsEnum.HEADERS]);
    unref(credentialsCardElRef)?.setFieldsValue(value?.[RestApiCallFieldsEnum.CREDENTIALS]);
  };
  defineExpose({
    setFieldsValue: setValue,
    getFieldsValue: getValue,
  } as CreateModalDefineExposeType);
</script>
<template>
  <BasicForm @register="register">
    <template #headers="{ field, model }">
      <AttributeConfiguration
        ref="attributeConfigurationElRef"
        v-model:value="model[field]"
        :allowEmpty="true"
        keyLabel="Header"
        valueLabel="Value"
      />
    </template>
    <template #credentials="{ field, model }">
      <CredentialsCard ref="credentialsCardElRef" v-model:value="model[field]" />
    </template>
  </BasicForm>
</template>