Skip to content

Commit

Permalink
refactor: internal state in SfSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
lsliwaradioluz committed Jan 17, 2025
1 parent dafb778 commit 5023f72
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/sfui/frameworks/vue/components/SfSelect/SfSelect.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { type PropType, watch, ref } from 'vue';
import { type PropType, ref, toRefs, computed } from 'vue';
import { SfSelectSize, SfIconExpandMore, useFocusVisible, useDisclosure } from '@storefront-ui/vue';
defineOptions({
Expand Down Expand Up @@ -40,12 +40,23 @@ const emit = defineEmits<{
(event: 'update:modelValue', param: string): void;
}>();
const { modelValue, invalid } = toRefs(props);
const { isOpen, close, open } = useDisclosure();
const { isFocusVisible } = useFocusVisible();
const internalValue = ref(props.modelValue);
watch(internalValue, (value) => emit('update:modelValue', value));
/*
Internal state has been implemented due to native select's element
value disappearing under certain circumstances. It's important to
keep it here, or to always pass modelValue to the component.
*/
const internalState = ref<string>(props.modelValue);
const selectModel = computed({
get: () => modelValue.value || internalState.value,
set: (value) => {
emit('update:modelValue', value);
internalState.value = value;
},
});
</script>

<template>
Expand All @@ -60,7 +71,7 @@ watch(internalValue, (value) => emit('update:modelValue', value));
data-testid="select"
>
<select
v-model="internalValue"
v-model="selectModel"
:required="required"
:disabled="disabled"
:class="[
Expand Down

0 comments on commit 5023f72

Please sign in to comment.