Przeglądaj źródła

feat(ApiTree): 完善ApiTree组件的重置回显功能. close #2307

invalid w 1 rok temu
rodzic
commit
a0d4b10a1f

+ 15 - 3
src/components/Form/src/components/ApiTree.vue

@@ -1,5 +1,5 @@
 <template>
-  <a-tree v-bind="getAttrs" @select="handleChange">
+  <a-tree v-bind="getAttrs" @select="handleChange" v-model:selectedKeys="state">
     <template #[item]="data" v-for="item in Object.keys($slots)">
       <slot :name="item" v-bind="data || {}"></slot>
     </template>
@@ -14,6 +14,7 @@
   import { get } from 'lodash-es';
   import { propTypes } from '/@/utils/propTypes';
   import { DataNode } from 'ant-design-vue/es/tree';
+  import { useRuleFormItem } from '/@/hooks/component/useFormItem';
 
   export default defineComponent({
     name: 'ApiTree',
@@ -24,12 +25,16 @@
       immediate: { type: Boolean, default: true },
       resultField: propTypes.string.def(''),
       afterFetch: { type: Function as PropType<AnyFunction> },
+      value: [Array, Object, String, Number],
     },
-    emits: ['options-change', 'change'],
+    emits: ['options-change', 'change', 'update:value'],
     setup(props, { attrs, emit }) {
       const treeData = ref<DataNode[]>([]);
       const isFirstLoaded = ref<Boolean>(false);
       const loading = ref(false);
+      const emitData = ref<any[]>([]);
+
+      const [state] = useRuleFormItem(props, 'value', 'change', emitData);
       const getAttrs = computed(() => {
         return {
           ...(props.api ? { treeData: unref(treeData) } : {}),
@@ -41,6 +46,13 @@
       }
 
       watch(
+        () => state.value,
+        (v) => {
+          emit('update:value', v);
+        },
+      );
+
+      watch(
         () => props.params,
         () => {
           !unref(isFirstLoaded) && fetch();
@@ -82,7 +94,7 @@
         isFirstLoaded.value = true;
         emit('options-change', treeData.value);
       }
-      return { getAttrs, loading, handleChange };
+      return { getAttrs, loading, handleChange, state };
     },
   });
 </script>

+ 3 - 0
src/components/Form/src/hooks/useFormEvents.ts

@@ -415,6 +415,9 @@ function getDefaultValue(
   if (!defaultValue && schema && checkIsRangeSlider(schema)) {
     defaultValue = [0, 0];
   }
+  if (!defaultValue && schema && schema.component === 'ApiTree') {
+    defaultValue = [];
+  }
   return defaultValue;
 }