fix: VnSelect
gitea/salix-front/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Javier Segarra 2024-09-13 01:13:08 +02:00
parent 756fb4f9c3
commit 0da32618ac
2 changed files with 48 additions and 60 deletions

View File

@ -3,6 +3,7 @@ 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 '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({
@ -104,20 +105,45 @@ 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;
}, },
set(value) { set(value) {
arrayData.store.page = 0; // arrayData.store.page = 0;
arrayData.store.hasMoreData = true; // arrayData.store.hasMoreData = true;
// setOptions(myOptionsOriginal.value); setOptions(myOptionsOriginal.value);
emit('update:modelValue', value); emit('update:modelValue', value);
}, },
}); });
watch(options, (newValue) => { // watch(options, (newValue) => {
setOptions(newValue); // setOptions(newValue);
// });
// watch(modelValue, (newValue) => {
// if (!myOptions.value.some((option) => option[optionValue.value] == newValue))
// fetchFilter(newValue);
// });
// onMounted(() => {
// setOptions(options.value);
// if (useURL.value && $props.modelValue && !findKeyInOptions())
// fetchFilter($props.modelValue);
// if ($props.focusOnMount) setTimeout(() => vnSelectRef.value.showPopup(), 300);
// });
const $attrs = useAttrs();
const arrayDataKey =
$props.dataKey ?? (useURL.value?.length > 0 ? useURL.value : $attrs.label);
const arrayData = useArrayData(arrayDataKey, { url: useURL.value });
onMounted(async () => {
if ($props.focusOnMount) setTimeout(() => vnSelectRef.value.showPopup(), 300);
setOptions(options.value);
if (useURL.value) fetchFilter($props.modelValue);
}); });
watch(modelValue, (newValue) => { watch(modelValue, (newValue) => {
@ -127,43 +153,7 @@ watch(modelValue, (newValue) => {
onMounted(() => { onMounted(() => {
setOptions(options.value); setOptions(options.value);
if ($props.url && $props.modelValue && !findKeyInOptions()) if (useURL.value && $props.modelValue && !findKeyInOptions())
fetchFilter($props.modelValue);
if ($props.focusOnMount) setTimeout(() => vnSelectRef.value.showPopup(), 300);
});
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(() => {
setOptions(options.value);
if ($props.url && $props.modelValue && !findKeyInOptions())
fetchFilter($props.modelValue);
if ($props.focusOnMount) setTimeout(() => vnSelectRef.value.showPopup(), 300);
});
const useURL = computed(() => $props.url);
const $attrs = useAttrs();
const arrayDataKey =
$props.dataKey ?? ($props.url.length > 0 ? $props.url : $attrs.label);
const arrayData = useArrayData(arrayDataKey, { url: $props.url });
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(() => {
setOptions(options.value);
if ($props.url && $props.modelValue && !findKeyInOptions())
fetchFilter($props.modelValue); fetchFilter($props.modelValue);
if ($props.focusOnMount) setTimeout(() => vnSelectRef.value.showPopup(), 300); if ($props.focusOnMount) setTimeout(() => vnSelectRef.value.showPopup(), 300);
}); });
@ -177,7 +167,6 @@ 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();
@ -198,11 +187,12 @@ 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 (!$props.url || !dataRef.value) return; if (!useURL.value) 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)
@ -218,11 +208,10 @@ 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, include, limit }; const fetchOptions = { where, order: sortBy, include, limit };
if (fields) fetchOptions.fields = fields; if (fields) fetchOptions.fields = fields;
if (sortBy) fetchOptions.order = sortBy;
arrayData.store.userFilter = fetchOptions; arrayData.store.userFilter = fetchOptions;
arrayData.store.order = fetchOptions.order;
arrayData.store.skip = 0; arrayData.store.skip = 0;
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 });
@ -239,10 +228,7 @@ async function filterHandler(val, update) {
let newOptions; let newOptions;
if (!$props.defaultFilter) return update(); if (!$props.defaultFilter) return update();
if ( if (useURL.value) {
$props.url &&
($props.limit || (!$props.limit && Object.keys(myOptions.value).length === 0))
) {
newOptions = await fetchFilter(val); newOptions = await fetchFilter(val);
} else newOptions = filter(val, myOptionsOriginal.value); } else newOptions = filter(val, myOptionsOriginal.value);
update( update(
@ -265,9 +251,9 @@ 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 && !$props.fetchRef) return; if (!useURL.value) return;
if (direction === 'decrease') return; if (direction === 'decrease') return;
if (to === lastIndex && arrayData.store.hasMoreData && !isLoading.value) { if (to === lastIndex && !isLoading.value) {
isLoading.value = true; isLoading.value = true;
await arrayData.loadMore(); await arrayData.loadMore();
setOptions(arrayData.store.data); setOptions(arrayData.store.data);
@ -275,7 +261,6 @@ async function onScroll({ to, direction, from, index }) {
isLoading.value = false; isLoading.value = false;
} }
} }
function nullishToTrue(value) { function nullishToTrue(value) {
return value ?? true; return value ?? true;
} }
@ -284,16 +269,16 @@ const getVal = (val) => ($props.useLike ? { like: `%${val}%` } : val);
</script> </script>
<template> <template>
<FetchData <!-- <FetchData
ref="dataRef" ref="dataRef"
:url="$props.url" :url="useURL.value"
@on-fetch="(data) => setOptions(data)" @on-fetch="(data) => setOptions(data)"
:where="where || { [optionValue]: value }" :where="where || { [optionValue]: value }"
:limit="limit" :limit="limit"
:sort-by="sortBy" :sort-by="sortBy"
:fields="fields" :fields="fields"
:params="params" :params="params"
/> /> -->
<QSelect <QSelect
:input-debounce="$attrs.url ? 300 : 0" :input-debounce="$attrs.url ? 300 : 0"
:loading="isLoading" :loading="isLoading"

View File

@ -59,6 +59,9 @@ describe('VnSelect FETCH URL', () => {
optionLabel: 'name', optionLabel: 'name',
optionValue: 'id', optionValue: 'id',
url: 'Suppliers', url: 'Suppliers',
sortBy: 'name DESC',
limit: '320',
fields: ['name'],
}, },
global: { global: {
stubs: ['FetchData'], stubs: ['FetchData'],
@ -79,7 +82,7 @@ describe('VnSelect FETCH URL', () => {
const canceller = new AbortController(); const canceller = new AbortController();
expect(axios.get).toHaveBeenCalledWith('Suppliers', { expect(axios.get).toHaveBeenCalledWith('Suppliers', {
params: { params: {
filter: '{"order":"","limit":"30","skip":0}', filter: '{"limit":"320","where":{},"order":"name DESC","include":null,"fields":["name"],"skip":0}',
}, },
signal: canceller.signal, signal: canceller.signal,
}); });