create.vue 1.83 KB
<script lang="ts" setup>
  import type { CreateModalDefineExposeType } from '../../../types';
  import { BasicForm, useForm } from '/@/components/Form';
  import { formSchemas } from './create.config';
  import { NodeData } from '../../../types/node';
  import { AttributeConfiguration } from '/@/views/rule/designer/src/components/AttributeConfiguration';
  import { ref, unref } from 'vue';
  import { RabbitmqFieldsEnum } from '../../../enum/formField/external';

  defineProps<{
    config: NodeData;
  }>();

  const attributeConfigurationElRef = ref<Nullable<InstanceType<typeof AttributeConfiguration>>>();

  const [register, { validate, getFieldsValue, setFieldsValue, resetFields }] = useForm({
    schemas: formSchemas,
    showActionButtonGroup: false,
  });

  const getValue: CreateModalDefineExposeType['getFieldsValue'] = async () => {
    await validate();
    await unref(attributeConfigurationElRef)?.validate();
    const value = getFieldsValue() || {};
    const clientProperties = unref(attributeConfigurationElRef)?.getFieldsValue();
    return {
      ...value,
      [RabbitmqFieldsEnum.CLIENT_PROPERTIES]: clientProperties,
    };
  };

  const setValue: CreateModalDefineExposeType['setFieldsValue'] = (value) => {
    resetFields();
    setFieldsValue(value);
    unref(attributeConfigurationElRef)?.setFieldsValue(
      value?.[RabbitmqFieldsEnum.CLIENT_PROPERTIES]
    );
  };

  defineExpose({
    setFieldsValue: setValue,
    getFieldsValue: getValue,
  } as CreateModalDefineExposeType);
</script>

<template>
  <BasicForm @register="register">
    <template #clientProperties="{ field, model }">
      <AttributeConfiguration
        ref="attributeConfigurationElRef"
        v-model:value="model[field]"
        keyLabel="Key"
        valueLabel="Value"
        :allowEmpty="true"
      />
    </template>
  </BasicForm>
</template>