From e749f68feffad70c1a2372407728b2e5df9e664b Mon Sep 17 00:00:00 2001 From: adroslice Date: Sun, 22 Oct 2023 22:16:08 +0200 Subject: [PATCH] WIP DropdownInput - Moved some setup to onMounted - Added local prop bindings - Strict to loose key comparison - Generalized form style for .form as well - Test page updated to include NicerForm and Selects --- .../src/components/inputs/DropdownInput.vue | 65 ++++++++++--------- client/src/global-styles/components.css | 7 +- client/src/pages/test.vue | 17 +++-- 3 files changed, 50 insertions(+), 39 deletions(-) diff --git a/client/src/components/inputs/DropdownInput.vue b/client/src/components/inputs/DropdownInput.vue index 8812948..78db4fa 100644 --- a/client/src/components/inputs/DropdownInput.vue +++ b/client/src/components/inputs/DropdownInput.vue @@ -43,42 +43,47 @@ const emit = defineEmits<{ (e: 'update:options', value: Options): void, }>(); -// Hook up two-way bindings +// Local Variables for Two-Way bindings let modelValue = $ref(multiple ? props.modelValue ?? [] : props.modelValue); -watch(() => props.modelValue, async (val) => { - if (equals(val, modelValue)) return; - if (isNullish(val)) return modelValue = val; // Cant be unknown - - // Run search provider if key unknown, log and reject if still so - let knownKeys = Object.keys(options); - if (multiple) { - let unknownKeys = (val as Index[]).filter(key => !knownKeys.includes(key.toString())); - if (!unknownKeys.length) return modelValue = val; - - await performSearch(unknownKeys); - knownKeys = Object.keys(options); - unknownKeys = (val as Index[]).filter(key => !knownKeys.includes(key.toString())); - for (let key of unknownKeys) console.warn(`Unknown key in DropdownInput:`, key/*, options*/); - return modelValue = (val as Index[]).filter(key => knownKeys.includes(key.toString())); - } - if (!knownKeys.includes(val.toString())) { - await performSearch([val]); - knownKeys = Object.keys(options); - if (!knownKeys.includes(val.toString())) - return console.warn(`Unknown key in DropdownInput:`, val/*, options*/); - } - modelValue = val; -}, { deep: true, immediate: true }); -watch($$(modelValue), () => emit('update:modelValue', modelValue), { deep: true }); let search = $ref(props.search); -watch(() => props.search, (val) => { if (!equals(val, search)) search = val; }, { deep: true }); +let options = $ref(props.options); +// Sync from v-model +onMounted(() => { + watch(() => props.modelValue, async (val) => { + if (equals(val, modelValue)) return; + if (isNullish(val)) return modelValue = val; // Cant be unknown + + // Run search provider if key unknown, log and reject if still so + let knownKeys = Object.keys(options); + if (multiple) { + let unknownKeys = (val as Index[]).filter(key => !knownKeys.includes(key.toString())); + if (!unknownKeys.length) return modelValue = val; + + await performSearch(unknownKeys); + knownKeys = Object.keys(options); + unknownKeys = (val as Index[]).filter(key => !knownKeys.includes(key.toString())); + for (let key of unknownKeys) console.warn(`Unknown key in DropdownInput:`, key/*, options*/); + return modelValue = (val as Index[]).filter(key => knownKeys.includes(key.toString())); + } + if (!knownKeys.includes(val.toString())) { + await performSearch([val]); + knownKeys = Object.keys(options); + if (!knownKeys.includes(val.toString())) + return console.warn(`Unknown key in DropdownInput:`, val/*, options*/); + } + modelValue = val; + }, { deep: true, immediate: true }); + watch(() => props.search, (val) => { if (!equals(val, search)) search = val; }, { deep: true }); + watch(() => props.options, (val) => { if (!equals(val, options)) options = val; }, { deep: true }); +}); +// Sync to v-model +watch($$(modelValue), () => emit('update:modelValue', modelValue), { deep: true }); watch($$(search), () => { emit('update:search', search); expand(); }, { deep: true }); -let options = $ref(props.options); -watch(() => props.options, (val) => { if (!equals(val, options)) options = val; }, { deep: true }); watch($$(options), () => emit('update:options', options), { deep: true }); + watch($$(multiple), () => modelValue = multiple ? [] : null ); // --- Everything Else --- @@ -200,7 +205,7 @@ function handleKeydown(e: KeyboardEvent) {