🔧 ui: BulmaSecret state machine rework

This commit is contained in:
Jörn-Michael Miehe 2025-12-28 01:55:01 +00:00
parent 7cca8c4825
commit 2aff1f3a99

View file

@ -1,17 +1,17 @@
<!-- eslint-disable vue/multi-word-component-names --> <!-- eslint-disable vue/multi-word-component-names -->
<template> <template>
<slot v-if="state === 'show'" name="default" /> <slot v-if="state === 'visible'" name="default" />
<span v-else>***</span> <span v-else>***</span>
<BulmaButton <BulmaButton
:class="`is-small is-${button_class} ml-2`" :class="`is-small is-${record.color} ml-2`"
:icon="['fas', `${button_icon}`]" :icon="['fas', `${record.icon}`]"
:busy="state === 'click'" :busy="state === 'clicked'"
@click="on_click" @click="on_click"
/> />
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref } from "vue"; import { computed, ref } from "vue";
import BulmaButton from "./Button.vue"; import BulmaButton from "./Button.vue";
@ -19,30 +19,21 @@ const emit = defineEmits<{
(event: "load"): void; (event: "load"): void;
}>(); }>();
const state = ref<"start" | "click" | "show">("start"); type State = "hidden" | "clicked" | "visible";
const button_class = ref<"primary" | "warning" | "danger">("primary"); const state = ref<State>("hidden");
const button_icon = ref<"eye-slash" | "eye">("eye-slash");
const state_map: Record<State, { color: string; icon: string; next: State }> = {
hidden: { color: "primary", icon: "eye-slash", next: "clicked" },
clicked: { color: "warning", icon: "eye-slash", next: "visible" },
visible: { color: "danger", icon: "eye", next: "hidden" },
} as const;
const record = computed(() => state_map[state.value] ?? state_map.hidden);
function on_click(): void { function on_click(): void {
switch (state.value) { state.value = record.value.next;
case "show":
state.value = "start";
button_class.value = "primary";
button_icon.value = "eye-slash";
break;
case "start": if (state.value === "visible") {
state.value = "click";
button_class.value = "warning";
button_icon.value = "eye-slash";
break;
case "click":
state.value = "show";
button_class.value = "danger";
button_icon.value = "eye";
emit("load"); emit("load");
break;
} }
} }
</script> </script>