Skip to content

Commit

Permalink
Components: Very-generic-input-widget: Fix selector not persistent
Browse files Browse the repository at this point in the history
Signed-off-by: Arturo Manzoli <[email protected]>
  • Loading branch information
ArturoManzoli committed Oct 21, 2024
1 parent 5cca995 commit 798d9c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
38 changes: 13 additions & 25 deletions src/components/custom-widget-elements/Dropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
unlistenCockpitActionVariable,
} from '@/libs/actions/data-lake'
import { useWidgetManagerStore } from '@/stores/widgetManager'
import { CustomWidgetElementOptions, CustomWidgetElementType } from '@/types/widgets'
import { CustomWidgetElementOptions, CustomWidgetElementType, SelectorOption } from '@/types/widgets'
const widgetStore = useWidgetManagerStore()
Expand All @@ -49,8 +49,7 @@ const props = defineProps<{
}>()
const element = toRefs(props).element
const lastSelectedOption = ref()
const selectedOption = ref()
const selectedOption = ref<SelectorOption | undefined>(props.element.options.lastSelected || undefined)
const options = computed(() => {
return (
Expand All @@ -60,27 +59,12 @@ const options = computed(() => {
)
})
watch(
() => element.value.options,
(newValue) => {
if (newValue.layout?.selectorOptions) {
selectedOption.value = newValue.layout.selectorOptions.find((option) => option.value === selectedOption.value)
}
},
{ deep: true, immediate: true }
)
watch(
() => widgetStore.editingMode,
(newValue) => {
if (newValue) {
lastSelectedOption.value = selectedOption.value
selectedOption.value = null
return
}
selectedOption.value = lastSelectedOption.value
}
)
watch(selectedOption, (newValue) => {
widgetStore.updateElementOptions(element.value.hash, {
...element.value.options,
lastSelected: newValue,
})
})
const handleSelection = (value: string | number | boolean): void => {
if (element.value.options.actionParameter?.id) {
Expand All @@ -93,6 +77,7 @@ onMounted(() => {
widgetStore.updateElementOptions(element.value.hash, {
cockpitAction: undefined,
actionParameter: undefined,
lastSelected: { name: '', value: '' },
layout: {
selectorOptions: [{ name: '', value: '' }],
align: 'start',
Expand All @@ -102,9 +87,12 @@ onMounted(() => {
}
if (element.value.options.actionParameter) {
listenCockpitActionVariable(element.value.options.actionParameter.name, (value) => {
selectedOption.value = value
selectedOption.value = options.value.find((option) => option.value === value)
})
}
if (element.value.options.lastSelected?.name !== '') {
selectedOption.value = element.value.options.lastSelected
}
})
onUnmounted(() => {
Expand Down
28 changes: 16 additions & 12 deletions src/types/widgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ export enum CustomWidgetElementContainers {
Right9 = '9-right',
}

export type SelectorOption = {
/**
* The name of the option
*/
name: string
/**
* The value of the option
*/
value: string
}

/**
* Options for the Cockpit Actions parameters
*/
Expand Down Expand Up @@ -359,25 +370,18 @@ export type CustomWidgetElementOptions = {
* Action parameter
*/
actionParameter: CockpitActionParameter
/**
* Last selected value
*/
lastSelected: SelectorOption
/**
* Layout options
*/
layout: {
/**
* Alignment of the element
*/
selectorOptions: [
{
/**
* The name of the option
*/
name: string
/**
* The value of the option
*/
value: string
}
]
selectorOptions: SelectorOption[]
/**
* Alignment of the element
*/
Expand Down

0 comments on commit 798d9c5

Please sign in to comment.