Skip to content

Commit 3e6c0b1

Browse files
committed
fix(SimpleList): v-model's event only emits string values
1 parent 83107f6 commit 3e6c0b1

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

packages/core/src/components/SimpleList/SimpleList.vue

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
</template>
1313

1414
<script lang="ts">
15-
export const SimpleListValueKey = Symbol('SimpleListValueKey') as InjectionKey<Ref<string | number | boolean | object | symbol | undefined | null>>;
15+
export const SimpleListValueKey = Symbol('SimpleListValueKey') as InjectionKey<WritableComputedRef<string | symbol | null>>;
1616
1717
export interface Props extends OUIAProps, /* @vue-ignore */ HTMLAttributes {
1818
/** Form element name */
1919
name?: string,
20+
modelValue?: string | null;
2021
required?: boolean;
2122
/** aria-label for the <ul> element that wraps the SimpleList items. */
2223
ariaLabel?: string;
@@ -26,7 +27,7 @@ export interface Props extends OUIAProps, /* @vue-ignore */ HTMLAttributes {
2627
<script lang="ts" setup>
2728
import styles from '@patternfly/react-styles/css/components/SimpleList/simple-list';
2829
29-
import { type Component, type InjectionKey, provide, type HTMLAttributes, ref, type Ref } from 'vue';
30+
import { type Component, type InjectionKey, provide, type HTMLAttributes, ref, type Ref, computed, type WritableComputedRef, watch } from 'vue';
3031
import { findChildrenVNodes } from '../../util';
3132
import Wrap from '../../helpers/Wrap.vue';
3233
import { useOUIAProps, type OUIAProps } from '../../helpers/ouia';
@@ -39,13 +40,32 @@ defineOptions({
3940
const props = defineProps<Props>();
4041
const ouiaProps = useOUIAProps({id: props.ouiaId, safe: props.ouiaSafe});
4142
42-
/** Value for the selected item */
43-
const value = defineModel<string | number | boolean | object | null>({ default: null });
43+
const emit = defineEmits<{
44+
/** Callback for when the model value changes */
45+
(e: 'update:modelValue', value: string): void;
46+
}>();
4447
4548
const slots = defineSlots<{
4649
default?: (props?: Record<never, never>) => any;
4750
}>();
4851
52+
/** Value for the selected item */
53+
const innerValue: Ref<string | symbol | null> = ref(props.modelValue ?? null);
54+
55+
const value = computed({
56+
get: () => innerValue.value,
57+
set: (v: string | symbol | null) => {
58+
innerValue.value = v;
59+
if (typeof v === 'string') {
60+
emit('update:modelValue', v);
61+
}
62+
},
63+
});
64+
65+
watch(() => props.modelValue, (v) => {
66+
innerValue.value = v ?? null;
67+
});
68+
4969
const grouped = ref(false);
5070
provide(SimpleListValueKey, value);
5171

0 commit comments

Comments
 (0)