From 2ecafae1e961914259ae9e4e86a1e14bb1c709a6 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Thu, 20 Apr 2023 21:00:51 +0530 Subject: [PATCH] refactor: simplify autocomplete --- fyo/utils/index.ts | 2 +- src/components/Controls/AutoComplete.vue | 250 +++++++---------------- src/components/Popover.vue | 3 +- src/utils/index.ts | 12 ++ src/utils/ui.ts | 8 + 5 files changed, 100 insertions(+), 175 deletions(-) diff --git a/fyo/utils/index.ts b/fyo/utils/index.ts index 35fef677..989fb6dd 100644 --- a/fyo/utils/index.ts +++ b/fyo/utils/index.ts @@ -128,7 +128,7 @@ function getRawOptionList(field: Field, doc: Doc | undefined | null) { return []; } - return getList(doc!); + return getList(doc); } export function getEmptyValuesByFieldTypes( diff --git a/src/components/Controls/AutoComplete.vue b/src/components/Controls/AutoComplete.vue index 5b608283..68084a27 100644 --- a/src/components/Controls/AutoComplete.vue +++ b/src/components/Controls/AutoComplete.vue @@ -1,5 +1,5 @@ - diff --git a/src/components/Popover.vue b/src/components/Popover.vue index add96b72..eebecab6 100644 --- a/src/components/Popover.vue +++ b/src/components/Popover.vue @@ -46,7 +46,6 @@ export default defineComponent({ emits: ['open', 'close'], props: { showPopup: Boolean as PropType, - right: Boolean, entryDelay: { type: Number, default: 0 }, exitDelay: { type: Number, default: 0 }, placement: { @@ -139,6 +138,7 @@ export default defineComponent({ if (this.isOpen) { return; } + this.isOpen = true; nextTick(() => { this.setupPopper(); @@ -149,6 +149,7 @@ export default defineComponent({ if (!this.isOpen) { return; } + this.isOpen = false; this.$emit('close'); }, diff --git a/src/utils/index.ts b/src/utils/index.ts index 61059459..73b7724d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -44,6 +44,18 @@ export function stringifyCircular( }); } +export function fuzzyFilter, K extends keyof T>( + items: T[], + prop: K, + filterString: string +): T[] { + return items + .map((item) => ({ ...fuzzyMatch(filterString, item[prop]), item })) + .filter(({ isMatch }) => isMatch) + .sort((a, b) => a.distance - b.distance) + .map(({ item }) => item); +} + export function fuzzyMatch(input: string, target: string) { const keywordLetters = [...input]; const candidateLetters = [...target]; diff --git a/src/utils/ui.ts b/src/utils/ui.ts index dbd8f307..aa8feba4 100644 --- a/src/utils/ui.ts +++ b/src/utils/ui.ts @@ -689,3 +689,11 @@ function getActionLabel(doc: Doc) { return doc.name || label; } + +export function getEventValue(e: Event) { + if (e.target instanceof HTMLInputElement) { + return e.target.value; + } + + return null; +} \ No newline at end of file