feat: add ability to fetch in this function
This commit is contained in:
parent
a534bfaa13
commit
8520e2d8f5
1 changed files with 21 additions and 7 deletions
|
|
@ -75,20 +75,34 @@ export function formatNumberDecimal(num: number, point: number): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function selectOptionFilter(
|
export function selectOptionFilter(
|
||||||
list: Record<string, any>[],
|
list: Record<string, unknown>[],
|
||||||
filterField?: string,
|
filterField?: string,
|
||||||
|
prepare?: (
|
||||||
|
...args: unknown[]
|
||||||
|
) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>,
|
||||||
) {
|
) {
|
||||||
const options = ref<typeof list>([]);
|
const options = ref<typeof list>([]);
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
const data = await prepare?.();
|
||||||
|
if (data) options.value = list = data;
|
||||||
|
})();
|
||||||
|
|
||||||
const filter = ((value, update) => {
|
const filter = ((value, update) => {
|
||||||
if (value === '') update(() => (options.value = list));
|
if (value === '') update(() => (options.value = list));
|
||||||
else
|
else
|
||||||
update(() => {
|
update(() => {
|
||||||
options.value = list.filter(
|
options.value = list.filter((v) => {
|
||||||
(v) =>
|
const label = v[filterField || 'label'];
|
||||||
v[filterField || 'label']
|
|
||||||
.toLocaleLowerCase()
|
if (typeof label !== 'string') {
|
||||||
.indexOf(value.toLocaleLowerCase()) > -1,
|
throw new Error('Label must be of type string.');
|
||||||
);
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
label.toLocaleLowerCase().indexOf(value.toLocaleLowerCase()) > -1
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}) satisfies QSelect['onFilter'];
|
}) satisfies QSelect['onFilter'];
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue