fix: vnselect X cancelSignal

This commit is contained in:
Javier Segarra 2024-09-13 20:39:38 +02:00
parent 88e5dd08b8
commit e621810f62
2 changed files with 56 additions and 32 deletions

View File

@ -3,8 +3,9 @@ import { ref, toRefs, computed, watch, onMounted } from 'vue';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { useArrayData } from 'src/composables/useArrayData'; import { useArrayData } from 'src/composables/useArrayData';
import { useAttrs } from 'vue'; import { useAttrs } from 'vue';
// import FetchData from 'src/components/FetchData.vue';
const emit = defineEmits(['update:modelValue', 'update:options', 'remove']); const emit = defineEmits(['update:modelValue', 'update:options', 'remove']);
const $props = defineProps({ const $props = defineProps({
modelValue: { modelValue: {
type: [String, Number, Object], type: [String, Number, Object],
@ -93,10 +94,11 @@ const requiredFieldRule = (val) => val ?? t('globals.fieldRequired');
const { optionLabel, optionValue, optionFilter, optionFilterValue, options, modelValue } = const { optionLabel, optionValue, optionFilter, optionFilterValue, options, modelValue } =
toRefs($props); toRefs($props);
const myOptions = ref([]); const myOptions = ref($props.options);
const myOptionsOriginal = ref([]); const myOptionsOriginal = ref($props.options);
const vnSelectRef = ref(); const vnSelectRef = ref();
const isLoading = ref(false); const isLoading = ref(false);
const dataRef = ref();
const lastVal = ref(); const lastVal = ref();
const noOneText = t('globals.noOne'); const noOneText = t('globals.noOne');
const noOneOpt = ref({ const noOneOpt = ref({
@ -104,7 +106,6 @@ const noOneOpt = ref({
[optionLabel.value]: noOneText, [optionLabel.value]: noOneText,
}); });
const useURL = computed(() => $props.url);
const selectValue = computed({ const selectValue = computed({
get() { get() {
return $props.modelValue; return $props.modelValue;
@ -115,22 +116,34 @@ const selectValue = computed({
}, },
}); });
const useURL = computed(() => $props.url);
const $attrs = useAttrs(); const $attrs = useAttrs();
const arrayDataKey = const arrayDataKey =
$props.dataKey ?? (useURL.value?.length > 0 ? useURL.value : $attrs.label); $props.dataKey ?? ($props.url?.length > 0 ? $props.url : $attrs.name ?? $attrs.label);
const arrayData = useArrayData(arrayDataKey, { url: useURL.value }); const arrayData = useArrayData(arrayDataKey, { url: $props.url });
const getVal = (val) => ($props.useLike ? { like: `%${val}%` } : val);
watch(modelValue, (newValue) => { onMounted(async () => {
if (!myOptions.value.some((option) => option[optionValue.value] == newValue)) if ($props.focusOnMount) setTimeout(() => vnSelectRef.value.showPopup(), 300);
fetchFilter(newValue); setOptions(options.value);
if (useURL.value) await fetchFilter($props.modelValue);
});
watch(options, (newValue) => {
setOptions(newValue);
}); });
onMounted(() => { watch(modelValue, async (newValue) => {
if (!myOptions.value.some((option) => option[optionValue.value] == newValue))
await fetchFilter(newValue);
if ($props.noOne) myOptions.value.unshift(noOneOpt.value);
});
onMounted(async () => {
setOptions(options.value); setOptions(options.value);
if (useURL.value && $props.modelValue && !findKeyInOptions()) if ($props.url && $props.modelValue && !findKeyInOptions())
fetchFilter($props.modelValue); await fetchFilter($props.modelValue);
if ($props.focusOnMount) setTimeout(() => vnSelectRef.value.showPopup(), 300); if ($props.focusOnMount) setTimeout(() => vnSelectRef.value.showPopup(), 300);
}); });
@ -143,6 +156,7 @@ function setOptions(data, append = true) {
myOptions.value = JSON.parse(JSON.stringify(data)); myOptions.value = JSON.parse(JSON.stringify(data));
if (append) myOptionsOriginal.value = JSON.parse(JSON.stringify(data)); if (append) myOptionsOriginal.value = JSON.parse(JSON.stringify(data));
} }
function filter(val, options) { function filter(val, options) {
const search = val?.toString()?.toLowerCase(); const search = val?.toString()?.toLowerCase();
@ -163,12 +177,11 @@ function filter(val, options) {
return id == search || optionLabel.includes(search); return id == search || optionLabel.includes(search);
}); });
} }
// const dataRef = ref();
async function fetchFilter(val) { async function fetchFilter(val) {
if (!useURL.value) return; if (!$props.url) return;
const { fields, sortBy, include, limit } = $props;
const { fields, include, sortBy, limit } = $props;
const key = const key =
optionFilterValue.value ?? optionFilterValue.value ??
(new RegExp(/\d/g).test(val) (new RegExp(/\d/g).test(val)
@ -184,8 +197,11 @@ async function fetchFilter(val) {
}, {}); }, {});
} else defaultWhere = { [key]: getVal(val) }; } else defaultWhere = { [key]: getVal(val) };
const where = { ...(val ? defaultWhere : {}), ...$props.where }; const where = { ...(val ? defaultWhere : {}), ...$props.where };
const fetchOptions = { where, order: sortBy, include, limit }; const fetchOptions = { where, include, limit };
if (fields) fetchOptions.fields = fields; if (fields) fetchOptions.fields = fields;
if (sortBy) fetchOptions.order = sortBy;
// return dataRef.value.fetch(fetchOptions);
arrayData.store.userParams = $props.params; arrayData.store.userParams = $props.params;
arrayData.store.userFilter = fetchOptions; arrayData.store.userFilter = fetchOptions;
arrayData.store.order = fetchOptions.order; arrayData.store.order = fetchOptions.order;
@ -193,7 +209,7 @@ async function fetchFilter(val) {
arrayData.store.filter.skip = 0; arrayData.store.filter.skip = 0;
const { data } = await arrayData.fetch({ append: true, updateRouter: false }); const { data } = await arrayData.fetch({ append: true, updateRouter: false });
setOptions(data); setOptions(data);
return data; // return data;
} }
async function filterHandler(val, update) { async function filterHandler(val, update) {
@ -224,13 +240,19 @@ async function filterHandler(val, update) {
); );
} }
function nullishToTrue(value) {
return value ?? true;
}
const getVal = (val) => ($props.useLike ? { like: `%${val}%` } : val);
async function onScroll({ to, direction, from, index }) { async function onScroll({ to, direction, from, index }) {
const lastIndex = myOptions.value.length - 1; const lastIndex = myOptions.value.length - 1;
if (from === 0 && index === 0) return; if (from === 0 && index === 0) return;
if (!useURL.value) return; if (!useURL.value && !$props.fetchRef) return;
if (direction === 'decrease') return; if (direction === 'decrease') return;
if (to === lastIndex && !isLoading.value) { if (to === lastIndex && arrayData.store.hasMoreData && !isLoading.value) {
isLoading.value = true; isLoading.value = true;
await arrayData.loadMore(); await arrayData.loadMore();
setOptions(arrayData.store.data); setOptions(arrayData.store.data);
@ -238,12 +260,19 @@ async function onScroll({ to, direction, from, index }) {
isLoading.value = false; isLoading.value = false;
} }
} }
function nullishToTrue(value) {
return value ?? true;
}
</script> </script>
<template> <template>
<!-- <FetchData
ref="dataRef"
:url="$props.url"
@on-fetch="(data) => setOptions(data)"
:where="where || { [optionValue]: selectValue }"
:limit="limit"
:sort-by="sortBy"
:fields="fields"
:params="params"
/> -->
<QSelect <QSelect
:input-debounce="$attrs.url ? 300 : 0" :input-debounce="$attrs.url ? 300 : 0"
:loading="isLoading" :loading="isLoading"
@ -269,12 +298,7 @@ function nullishToTrue(value) {
<QIcon <QIcon
v-show="value" v-show="value"
name="close" name="close"
@click.stop=" @click.stop="value = null"
() => {
value = null;
emit('remove');
}
"
class="cursor-pointer" class="cursor-pointer"
size="xs" size="xs"
/> />

View File

@ -83,7 +83,7 @@ describe('VnSelect CALL FETCH URL', () => {
const canceller = new AbortController(); const canceller = new AbortController();
expect(axios.get).toHaveBeenCalledWith('Suppliers', { expect(axios.get).toHaveBeenCalledWith('Suppliers', {
params: { params: {
filter: '{"limit":"320","where":{"id":{"like":"%1%"}},"order":"name DESC","include":null,"fields":["name"],"skip":0}', filter: '{"limit":"320","where":{"id":{"like":"%1%"}},"include":null,"fields":["name"],"order":"name DESC","skip":0}',
}, },
signal: canceller.signal, signal: canceller.signal,
}); });
@ -124,7 +124,7 @@ describe('VnSelect NOT CALL FETCH URL', () => {
vm.selectValue = ''; vm.selectValue = '';
expect(vm.options.length).toEqual(0); expect(vm.options.length).toEqual(0);
expect(vm.useURL).toBeTruthy(); expect(vm.useURL).toBeTruthy();
expect(vm.myOptions.length).toEqual(0); expect(vm.myOptions.length).toEqual(3);
const canceller = new AbortController(); const canceller = new AbortController();
expect(axios.get).not.toHaveBeenCalledWith('Suppliers', { expect(axios.get).not.toHaveBeenCalledWith('Suppliers', {