You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.4 KiB

4 months ago
<template>
<a-spin :spinning="treeState.loading">
<div class="ns-tree-form">
<div v-if="header" class="ns-tree-title">
<ns-icon :name="header.icon" size="14" />
<span>{{ header.title }}</span>
</div>
<div v-if="!formConfig?.schema">
<ns-form ref="formElRef" v-bind="formConfig" :model="formModel" @finish="formFinish" />
</div>
</div>
<div>
<ns-tree v-if="treeData?.length" v-bind="getBindValue" v-model:selectedKeys="selectedKeys">
<template #[item]="data" v-for="(item, index) in Object.keys($slots)" :key="index">
<slot :name="item" v-bind="data || {}"></slot>
</template>
</ns-tree>
</div>
</a-spin>
4 months ago
</template>
<script lang="ts" setup>
import { computed, reactive, ref, unref, useAttrs } from 'vue';
4 months ago
import { TreeDataItem } from 'ant-design-vue/es/tree/Tree';
import { useApi } from '/nerv-lib/use/use-api';
import { AxiosRequestConfig } from 'axios';
4 months ago
import { get } from 'lodash-es';
import { useRoute } from 'vue-router';
import { debounce } from 'lodash-es';
import { treeProps, treeFormProps } from '/nerv-lib/component/tree/props';
const formElRef = ref();
defineOptions({
4 months ago
name: 'NsTreeApi',
});
const props = defineProps(treeProps);
console.log(props);
const treeData = ref<TreeDataItem[]>([]);
3 months ago
const selectedKeys = ref(props.selectedKeys || []);
const { httpRequest } = useApi();
const requestConfig: AxiosRequestConfig = { method: 'get' };
const route = useRoute();
const attrs = useAttrs();
const formModel = reactive({});
const treeState = reactive({
loading: false,
});
const formConfig = computed(() => {
return {
...treeFormProps,
...props.formConfig,
};
});
const isSticky: any = computed(() => {
return props.isSticky ? 'sticky' : 'static';
});
const formFinish = debounce((data: object) => {
getData(data);
}, 200);
const getBindValue = computed(() => ({
...attrs,
...props,
treeData: treeData.value,
}));
const setLoading = (loading: boolean) => {
treeState.loading = loading;
};
const httpPrams = computed(() => {
return { ...route.params, ...route.query, ...props.defaultParams };
});
4 months ago
const getData = (params = {}) => {
const { api, transform, resultField } = props;
treeData.value = [];
if (!api) return;
setLoading(true);
httpRequest({
api,
params: { ...httpPrams.value, ...params },
pathParams: { ...route.params, ...route.query, ...params },
requestConfig: { ...requestConfig, data: { ...httpPrams.value, ...params } },
})
.then((res) => {
treeData.value = transform(get(res, resultField));
console.log(treeData.value, 'treeData.value');
})
.finally(() => {
setLoading(false);
});
};
getData();
4 months ago
</script>
<style lang="less" scoped>
@gap: 16px;
:deep(.ant-form-item) {
margin-bottom: @gap;
}
:deep(.ns-form::after) {
display: none;
}
.ns-tree-form {
position: v-bind(isSticky);
top: 0;
background-color: @white;
z-index: 2;
& ~ div {
padding: 0 @gap !important;
}
}
.ns-tree-title {
font-weight: bold;
user-select: text;
padding: @gap;
margin-bottom: @gap;
padding-bottom: 10px;
border-bottom: 1px solid #e9e9e9;
> span {
padding-left: 6px;
}
& ~ div {
padding: 0 @gap !important;
}
}
</style>