Skip to content

Commit

Permalink
fix(HstSelect): allow any type of value
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Jul 21, 2022
1 parent b0dc96d commit 1e198a8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
23 changes: 12 additions & 11 deletions packages/histoire-controls/src/components/select/CustomSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,30 @@ import { HstControlOption } from '../../types'
const props = defineProps<{
modelValue: string
options: Record<string, string> | string[] | HstControlOption[]
options: Record<string, any> | string[] | HstControlOption[]
}>()
const emits = defineEmits<{
(e: 'update:modelValue', value: string): void
}>()
const formattedOptions: ComputedRef<Record<string, string>> = computed(() => {
const formattedOptions: ComputedRef<[any, string][]> = computed(() => {
if (Array.isArray(props.options)) {
return Object.fromEntries(props.options.map((value: string | HstControlOption) => {
if (typeof value === 'string') {
return [value, value]
return props.options.map(option => {
if (typeof option === 'string') {
return [option, option] as [string, string]
} else {
return [value.value, value.label]
return [option.value, option.label] as [any, string]
}
}))
})
} else {
return Object.entries(props.options)
}
return props.options
})
const selectedLabel = computed(() => formattedOptions.value[props.modelValue])
const selectedLabel = computed(() => formattedOptions.value.find(([value]) => value === props.modelValue)?.[1])
function selectValue (value: string, hide: () => void) {
function selectValue (value: any, hide: () => void) {
emits('update:modelValue', value)
hide()
}
Expand All @@ -60,7 +61,7 @@ function selectValue (value: string, hide: () => void) {
<template #popper="{ hide }">
<div class="htw-flex htw-flex-col htw-bg-gray-50 dark:htw-bg-gray-700">
<div
v-for="( label, value ) in formattedOptions"
v-for="[value, label] of formattedOptions"
v-bind="{ ...$attrs, class: null, style: null }"
:key="label"
class="htw-px-2 htw-py-1 htw-cursor-pointer hover:htw-bg-primary-100 dark:hover:htw-bg-primary-700"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { HstControlOption } from '../../types'
defineProps<{
title?: string
modelValue: string
options: Record<string, string> | string[] | HstControlOption[]
options: Record<string, any> | string[] | HstControlOption[]
}>()
const emits = defineEmits<{
(e: 'update:modelValue', value: string): void
(e: 'update:modelValue', value: any): void
}>()
</script>

Expand Down
2 changes: 1 addition & 1 deletion packages/histoire-controls/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface HstControlOption {
label: string
value: string
value: any
}

0 comments on commit 1e198a8

Please sign in to comment.