From 446038d37fe1fef7c4b9c3042f3fd1e56de35ad3 Mon Sep 17 00:00:00 2001 From: Temmie Date: Mon, 11 Aug 2025 22:08:14 +0200 Subject: [PATCH] feat: add support to select default radio button based on key also simplifies the radio button code, making it easier to parse --- components/Settings/AppSettings/Language.vue | 4 +- components/UserInterface/RadioButtons.vue | 61 ++++++++++++++------ 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/components/Settings/AppSettings/Language.vue b/components/Settings/AppSettings/Language.vue index f7d2a63..1466873 100644 --- a/components/Settings/AppSettings/Language.vue +++ b/components/Settings/AppSettings/Language.vue @@ -5,9 +5,8 @@

TIME FORMAT

@@ -20,7 +19,6 @@ import RadioButtons from '~/components/UserInterface/RadioButtons.vue'; import type { TimeFormat } from '~/types/settings'; const timeFormatTextStrings: TimeFormat[] = ["Auto", "4:18 PM", "16:18"] -const timeFormatSelectedIndex = timeFormatTextStrings.indexOf(settingsLoad().timeFormat ?? "Auto") diff --git a/components/UserInterface/RadioButtons.vue b/components/UserInterface/RadioButtons.vue index c36b5d0..0f05dc9 100644 --- a/components/UserInterface/RadioButtons.vue +++ b/components/UserInterface/RadioButtons.vue @@ -1,8 +1,12 @@ @@ -13,42 +17,65 @@ const radioButtonsContainer = ref() const props = defineProps<{ textStrings: string[], - buttonCount: number, - defaultButtonIndex: number, + defaultButtonKey?: string, + defaultButtonIndex?: number, callback: CallableFunction, }>(); -// makes an array from 0 to buttonCount - 1 -const indices = Array.from({ length: props.buttonCount }, (_, i) => i) - -// select default selected button onMounted(async () => { await nextTick() - if (props.defaultButtonIndex != undefined && radioButtonsContainer.value) { + // select default selected button + if (radioButtonsContainer.value) { const children = radioButtonsContainer.value.children - const defaultButton = children.item(props.defaultButtonIndex) - defaultButton?.classList.add("selected-radio-button") - defaultButton?.children.item(0)?.classList.add("selected-radio-button-radio") + + // set the button based on key + if (props.defaultButtonKey != undefined) { + const newIndex = props.textStrings.indexOf(props.defaultButtonKey) + const defaultButton = children.item(newIndex) + if (defaultButton) { + selectButton(defaultButton) + return // note the return if you're extending this + } + } + // if that fails, set it based on index, defaulting to 0 + const defaultButton = children.item(props.defaultButtonIndex ?? 0) + if (defaultButton) { + selectButton(defaultButton) + } } }) function onClick(clickedIndex: number) { - // remove selected-radio-button class from all buttons except the clicked one if (radioButtonsContainer.value) { + // remove selected-radio-button class from all buttons except the clicked one const children = radioButtonsContainer.value.children for (let i = 0; i < children.length; i++) { - children.item(i)?.classList.remove("selected-radio-button") - children.item(i)?.children.item(0)?.classList.remove("selected-radio-button-radio") + const button = children.item(i) + if (button) { + unSelectButton(button) + } } - children.item(clickedIndex)?.classList.add("selected-radio-button") - children.item(clickedIndex)?.children.item(0)?.classList.add("selected-radio-button-radio") + const button = children.item(clickedIndex) + if (button) { + selectButton(button) + } } props.callback(clickedIndex) } + +function unSelectButton(button: Element) { + button.classList.remove("selected-radio-button") + button.children.item(0)?.classList.remove("selected-radio-button-radio") +} + +function selectButton(button: Element) { + button.classList.add("selected-radio-button") + button.children.item(0)?.classList.add("selected-radio-button-radio") +}