Skip to content

Commit f534058

Browse files
fix: mark placeholder option as selected in react SfSelect (#3263)
* fix: mark placeholder option as selected in react SfSelect * chore: changeset * refactor: add internal value to vue SfSelect * chore: changeset for vue * refactor: remove redundant <script setup /> from vue components * refactor: internal state in SfSelect * refactor: remove disabled and selected attributes * chore: remove disabled in vue placeholder option * Revert "refactor: remove redundant <script setup /> from vue components" This reverts commit dafb778. * refactor: set inheritAttrs through defineOptions
1 parent da07cce commit f534058

File tree

6 files changed

+37
-20
lines changed

6 files changed

+37
-20
lines changed

.changeset/shiny-camels-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@storefront-ui/vue': patch
3+
---
4+
5+
[FIXED] Fixed `<SfSelect />` value not updated when no `v-model` is used on the component.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@storefront-ui/react': patch
3+
---
4+
5+
[FIXED] Fixed `<SfSelect />` placeholder not appearing initially when no value had been selected.

packages/sfui/frameworks/react/components/SfSelect/SfSelect.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export default function SfSelect(props: SfSelectProps) {
6464
>
6565
{placeholder && (
6666
<option
67-
disabled
6867
hidden
6968
value=""
7069
className={classNames('bg-neutral-300 text-sm', {

packages/sfui/frameworks/vue/components/SfInput/SfInput.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<script lang="ts">
2-
export default {
3-
inheritAttrs: false,
4-
};
52
const getSizeClasses = {
63
[SfInputSize.sm]: ' h-[32px]',
74
[SfInputSize.base]: 'h-[40px]',
@@ -14,6 +11,10 @@ import type { PropType, ConcreteComponent } from 'vue';
1411
import { computed, ref, toRefs } from 'vue';
1512
import { SfInputSize, useFocusVisible } from '@storefront-ui/vue';
1613
14+
defineOptions({
15+
inheritAttrs: false,
16+
});
17+
1718
const props = defineProps({
1819
modelValue: {
1920
type: [String, Number],

packages/sfui/frameworks/vue/components/SfScrollable/SfScrollable.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
<script lang="ts">
2-
export default {
3-
inheritAttrs: false,
4-
};
5-
</script>
61
<script lang="ts" setup>
72
import { computed, toRefs, type PropType, type ConcreteComponent, reactive } from 'vue';
83
import {
@@ -21,6 +16,10 @@ import {
2116
type ScrollableOptions,
2217
} from '@storefront-ui/vue';
2318
19+
defineOptions({
20+
inheritAttrs: false,
21+
});
22+
2423
const props = defineProps({
2524
tag: {
2625
type: [String, Object] as PropType<string | ConcreteComponent>,

packages/sfui/frameworks/vue/components/SfSelect/SfSelect.vue

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
<script lang="ts">
2-
export default {
3-
inheritAttrs: false,
4-
};
5-
</script>
61
<script lang="ts" setup>
7-
import { type PropType, computed } from 'vue';
2+
import { type PropType, ref, toRefs, computed } from 'vue';
83
import { SfSelectSize, SfIconExpandMore, useFocusVisible, useDisclosure } from '@storefront-ui/vue';
94
5+
defineOptions({
6+
inheritAttrs: false,
7+
});
8+
109
const props = defineProps({
1110
size: {
1211
type: String as PropType<`${SfSelectSize}`>,
@@ -41,12 +40,22 @@ const emit = defineEmits<{
4140
(event: 'update:modelValue', param: string): void;
4241
}>();
4342
43+
const { modelValue } = toRefs(props);
4444
const { isOpen, close, open } = useDisclosure();
4545
const { isFocusVisible } = useFocusVisible();
4646
47-
const modelProxy = computed({
48-
get: () => props.modelValue,
49-
set: (value: string) => emit('update:modelValue', value),
47+
/*
48+
Internal state has been implemented due to native select's element
49+
value disappearing under certain circumstances. It's important to
50+
keep it here, or to always pass modelValue to the component.
51+
*/
52+
const internalState = ref<string>(modelValue.value);
53+
const selectModel = computed({
54+
get: () => modelValue.value || internalState.value,
55+
set: (value) => {
56+
emit('update:modelValue', value);
57+
internalState.value = value;
58+
},
5059
});
5160
</script>
5261

@@ -62,8 +71,8 @@ const modelProxy = computed({
6271
data-testid="select"
6372
>
6473
<select
74+
v-model="selectModel"
6575
:required="required"
66-
v-model="modelProxy"
6776
:disabled="disabled"
6877
:class="[
6978
'appearance-none disabled:cursor-not-allowed cursor-pointer pl-4 pr-3.5 text-neutral-900 ring-inset focus:ring-primary-700 focus:ring-2 outline-none bg-transparent rounded-md ring-1 ring-neutral-300 hover:ring-primary-700 active:ring-2 active:ring-primary-700 disabled:bg-disabled-100 disabled:text-disabled-900 disabled:ring-disabled-200',
@@ -83,7 +92,6 @@ const modelProxy = computed({
8392
>
8493
<option
8594
v-if="placeholder"
86-
disabled
8795
hidden
8896
class="text-sm bg-neutral-300"
8997
value=""

0 commit comments

Comments
 (0)