|
|
@@ -4,32 +4,10 @@ import {computed, defineComponent, ref, PropType} from 'vue';
|
|
|
import {selectProps, NSelect, NEmpty, NButton, useMessage} from 'naive-ui';
|
|
|
import {useField} from 'vee-validate';
|
|
|
import {debounce} from 'lodash-es';
|
|
|
-import { useI18n } from 'vue-i18n';
|
|
|
-import {BaseResultContent, BaseResult} from '@models';
|
|
|
-import {GetCustomer, addCustomer}from '@apis'
|
|
|
-import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query';
|
|
|
-
|
|
|
-async function mockGetList() {
|
|
|
- return await new Promise<{msg: '200', data: {id: string, name:string}[]}>(function(res){
|
|
|
- setTimeout(function() {
|
|
|
- res({
|
|
|
- msg: '200',
|
|
|
- data: [{id: '1', name: 'simon'}, {id: '2', name: 'david'}]
|
|
|
- });
|
|
|
- }, 2000);
|
|
|
- })
|
|
|
-}
|
|
|
-
|
|
|
-async function mockAdd(name: string){
|
|
|
- return await new Promise<{msg: '200', data: string}>(function(res){
|
|
|
- setTimeout(function() {
|
|
|
- res({
|
|
|
- msg: '200',
|
|
|
- data: '12',
|
|
|
- });
|
|
|
- }, 2000);
|
|
|
- })
|
|
|
-}
|
|
|
+import {useI18n} from 'vue-i18n';
|
|
|
+import {BaseResult, BaseResultContent} from '@models';
|
|
|
+import {addCustomer, getAddModel} from '@apis';
|
|
|
+import {useMutation, useQuery, useQueryClient} from '@tanstack/vue-query';
|
|
|
|
|
|
export default defineComponent({
|
|
|
name: 'LDSelectInput',
|
|
|
@@ -43,18 +21,21 @@ export default defineComponent({
|
|
|
required: true,
|
|
|
},
|
|
|
getFn: {
|
|
|
- type: Function as PropType<() => BaseResult<{name: string,id: string}[]>>,
|
|
|
+ type: Function as PropType<
|
|
|
+ () => BaseResult<{name: string, id: string}[]>
|
|
|
+ >,
|
|
|
required: true,
|
|
|
},
|
|
|
- addFn: {
|
|
|
- type: Function as PropType<() => BaseResult<{data:string}>>,
|
|
|
+ getAdd: {
|
|
|
+ type: Function as PropType<(name: string) => BaseResult<string>>,
|
|
|
required: true,
|
|
|
},
|
|
|
optional: Boolean,
|
|
|
multiple: selectProps.multiple,
|
|
|
},
|
|
|
setup(props) {
|
|
|
- const {value, setValue, errorMessage} = useField<string | string[]>(
|
|
|
+ const {value, setValue, errorMessage}
|
|
|
+ = useField<string | string[]>(
|
|
|
props.name,
|
|
|
void 0,
|
|
|
{validateOnMount: false, validateOnValueUpdate: false},
|
|
|
@@ -64,89 +45,75 @@ export default defineComponent({
|
|
|
const showOptions = ref(false);
|
|
|
|
|
|
// 请求options
|
|
|
- // const {isFetching, data} = useQuery({
|
|
|
- // queryKey: [GetCustomer.name],
|
|
|
- // async queryFn () {
|
|
|
- // const data = await GetCustomer();
|
|
|
-
|
|
|
- // if(data.msg === '200') return data.data.map(function({cCusName}, index) {
|
|
|
- // return {label: cCusName, value:(index+1).toString() };
|
|
|
- // });
|
|
|
-
|
|
|
- // return [];
|
|
|
- // },
|
|
|
- // initialData: [],
|
|
|
- // })
|
|
|
- //函数传过来有ts提示错误目前为解决
|
|
|
const {isFetching, data} = useQuery({
|
|
|
queryKey: [props.getFn.name],
|
|
|
- async queryFn () {
|
|
|
+ async queryFn() {
|
|
|
const data = await props.getFn();
|
|
|
-
|
|
|
- if(data.msg === '200') return data.data.map(function({name, id}) {
|
|
|
- return {label: name, value:id };
|
|
|
- });
|
|
|
+
|
|
|
+ if (data.msg === '200') {
|
|
|
+ return data.data.map(function({id, name}) {
|
|
|
+ return {label: name, value: id};
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
return [];
|
|
|
},
|
|
|
initialData: [],
|
|
|
- })
|
|
|
-
|
|
|
+ });
|
|
|
+ const filterValue = ref<string>('');
|
|
|
+ const filterOptions = computed(function() {
|
|
|
+ const options = data.value;
|
|
|
+ if (!filterValue.value) return options;
|
|
|
+
|
|
|
+ return options.filter(function(val) {
|
|
|
+ return (val?.label ?? '').includes(filterValue.value)
|
|
|
+ || (val?.value ?? '').includes(filterValue.value);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ const onDefaultSearch = debounce(
|
|
|
+ function(value: string) {
|
|
|
+ filterValue.value = value;
|
|
|
+ },
|
|
|
+ 0,
|
|
|
+ {
|
|
|
+ trailing: true,
|
|
|
+ leading: false,
|
|
|
+ },
|
|
|
+ );
|
|
|
+
|
|
|
// 新增options
|
|
|
const client = useQueryClient();
|
|
|
const {isLoading, mutate} = useMutation({
|
|
|
- mutationFn: addCustomer,
|
|
|
- onSuccess(data, variables){
|
|
|
- if(data.msg === '200'){
|
|
|
+ mutationFn: props.getAdd,
|
|
|
+ onSuccess(data, variables) {
|
|
|
+ if (data.msg === '200') {
|
|
|
const id = data.data;
|
|
|
const name = variables;
|
|
|
+
|
|
|
client.setQueryData<{label: string, value: string}[]>(
|
|
|
- [mockGetList.name],
|
|
|
+ [props.getFn.name],
|
|
|
function(data) {
|
|
|
return [...(data ?? []), {label: name, value: id}];
|
|
|
- }
|
|
|
+ },
|
|
|
);
|
|
|
-
|
|
|
setValue(id);
|
|
|
showOptions.value = false;
|
|
|
}
|
|
|
- }
|
|
|
+ },
|
|
|
});
|
|
|
const message = useMessage();
|
|
|
function onAdd() {
|
|
|
- if(!filterValue.value) return message.warning('请输入客户名称');
|
|
|
+ if (!filterValue.value) return message.warning('请输入客户名称');
|
|
|
|
|
|
mutate(filterValue.value);
|
|
|
}
|
|
|
|
|
|
function onUpdateShow(state: boolean) {
|
|
|
- if(isLoading.value) return;
|
|
|
+ if (isLoading.value) return;
|
|
|
|
|
|
showOptions.value = state;
|
|
|
}
|
|
|
|
|
|
- const filterValue = ref<string>('');
|
|
|
- const filterOptions = computed(function() {
|
|
|
- const options = data.value;
|
|
|
-
|
|
|
- if (!filterValue.value) return options;
|
|
|
-
|
|
|
- return options.filter(function(val) {
|
|
|
- console.log((val?.value as string ?? ''))
|
|
|
- return (val?.label as string ?? '').includes(filterValue.value)
|
|
|
- || (val?.value as string ?? '').includes(filterValue.value);
|
|
|
- });
|
|
|
- });
|
|
|
- const onDefaultSearch = debounce(
|
|
|
- function(value: string) {
|
|
|
- filterValue.value = value;
|
|
|
- },
|
|
|
- 0,
|
|
|
- {
|
|
|
- trailing: true,
|
|
|
- leading: false,
|
|
|
- },
|
|
|
- );
|
|
|
// 失去焦点之后判断是否有搜索结果
|
|
|
// 如果没有搜索结果将搜索值清空
|
|
|
// 防止没搜索到之后失去焦点再返回会显示空选项
|
|
|
@@ -158,9 +125,6 @@ export default defineComponent({
|
|
|
}
|
|
|
|
|
|
const {t} = useI18n();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
return () => (
|
|
|
<div class="ld-modal-field-wrapper">
|
|
|
<div class="ld-modal-field-group">
|
|
|
@@ -182,25 +146,29 @@ export default defineComponent({
|
|
|
onUpdateValue={setValue}
|
|
|
onSearch={onDefaultSearch}
|
|
|
onBlur={onBlur}
|
|
|
- on-update:show={onUpdateShow}
|
|
|
+ onUpdateShow={onUpdateShow}
|
|
|
>
|
|
|
- {{empty() {
|
|
|
- return <NEmpty description="未查到客户,点击新增按钮新增客户信息">
|
|
|
- {{extra() {
|
|
|
- return <NButton
|
|
|
- disabled={isLoading.value}
|
|
|
- loading={isLoading.value}
|
|
|
- onClick={onAdd}
|
|
|
- >
|
|
|
+ {{
|
|
|
+ empty() {
|
|
|
+ return <NEmpty description="未查到客户,点击新增按钮新增客户信息">
|
|
|
+ {{
|
|
|
+ extra() {
|
|
|
+ return <NButton
|
|
|
+ disabled={isLoading.value}
|
|
|
+ loading={isLoading.value}
|
|
|
+ onClick={onAdd}
|
|
|
+ >
|
|
|
新增
|
|
|
- </NButton>
|
|
|
- }}}
|
|
|
- </NEmpty>
|
|
|
- }}}
|
|
|
+ </NButton>;
|
|
|
+ },
|
|
|
+ }}
|
|
|
+ </NEmpty>;
|
|
|
+ },
|
|
|
+ }}
|
|
|
</NSelect>
|
|
|
</div>
|
|
|
<p class="ld-modal-filed-error">{errorMessage.value && t(errorMessage.value)}</p>
|
|
|
</div>
|
|
|
);
|
|
|
},
|
|
|
-});
|
|
|
+});
|