From 8520e2d8f5523c6c56237d1af76793e4c296eed8 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:29:58 +0700 Subject: [PATCH] feat: add ability to fetch in this function --- src/stores/utils/index.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/stores/utils/index.ts b/src/stores/utils/index.ts index 45b82b70..7dadc932 100644 --- a/src/stores/utils/index.ts +++ b/src/stores/utils/index.ts @@ -75,20 +75,34 @@ export function formatNumberDecimal(num: number, point: number): string { } export function selectOptionFilter( - list: Record[], + list: Record[], filterField?: string, + prepare?: ( + ...args: unknown[] + ) => Record[] | Promise[]>, ) { const options = ref([]); + + (async () => { + const data = await prepare?.(); + if (data) options.value = list = data; + })(); + const filter = ((value, update) => { if (value === '') update(() => (options.value = list)); else update(() => { - options.value = list.filter( - (v) => - v[filterField || 'label'] - .toLocaleLowerCase() - .indexOf(value.toLocaleLowerCase()) > -1, - ); + options.value = list.filter((v) => { + const label = v[filterField || 'label']; + + if (typeof label !== 'string') { + throw new Error('Label must be of type string.'); + } + + return ( + label.toLocaleLowerCase().indexOf(value.toLocaleLowerCase()) > -1 + ); + }); }); }) satisfies QSelect['onFilter'];