ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import { computed, inject, onBeforeMount, ref, unref, watch } from 'vue';
 
import { useAttrs } from '@jnpf/hooks';
 
import { $t } from '@vben/locales';
 
import { globalShareState } from '@vben-core/shared/global-state';
 
import { Form } from 'ant-design-vue';
 
defineOptions({ inheritAttrs: false, name: 'JnpfRelationFormAttr' });
const props = defineProps(['value', 'showField', 'relationField', 'detailed', 'isStorage', 'disabled']);
const emit = defineEmits(['update:value']);
const formItemContext = Form.useInjectFormItemContext();
const { useGeneratorStore } = globalShareState.getStores();
const generatorStore = useGeneratorStore();
const emitter: any = inject('emitter');
const innerValue = ref<number | string | undefined>(undefined);
const attrs: any = useAttrs({ excludeDefaultKeys: false });
 
const getBindValue = computed(() => ({
  disabled: props.disabled,
  placeholder: props.isStorage ? $t('component.jnpf.relationFormAttr.storage') : $t('component.jnpf.relationFormAttr.unStorage'),
  readonly: true,
}));
const getStyle = computed(() => (Reflect.has(unref(attrs), 'style') ? unref(attrs).style : {}));
 
watch(
  () => generatorStore.getRelationData,
  (val) => {
    setValue(val);
  },
  {
    deep: true,
    immediate: true,
  },
);
 
function setValue(value) {
  if (props.isStorage || !props.showField || !props.relationField) return;
  const obj = value[props.relationField] || {};
  innerValue.value = obj[props.showField] || obj[props.showField] == 0 ? obj[props.showField] : '';
}
 
onBeforeMount(() => {
  emitter.on('setRelationData', (data) => {
    if (!props.isStorage || !props.showField || !props.relationField || props.relationField != data.jnpfRelationField) return;
    innerValue.value = data[props.showField] || data[props.showField] == 0 ? data[props.showField] : '';
    emit('update:value', innerValue.value);
    formItemContext.onFieldChange();
  });
});
</script>
 
<template>
  <div :style="getStyle">
    <a-input v-model:value="innerValue" v-bind="getBindValue" v-if="!detailed" />
    <p v-else>{{ innerValue }}</p>
  </div>
</template>