Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(VSelects): recognize empty string as no value #20742

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ export const VAutocomplete = genericComponent<new <
const selectionIndex = shallowRef(-1)
const color = computed(() => vTextFieldRef.value?.color)
const label = computed(() => menu.value ? props.closeText : props.openText)
const { items, transformIn, transformOut } = useItems(props)
const { items, transformIn, transformOut, emptyValues } = useItems(props)
const { textColorClasses, textColorStyles } = useTextColor(color)
const search = useProxiedModel(props, 'search', '')
const model = useProxiedModel(
props,
'modelValue',
[],
v => transformIn(v === null ? [null] : wrapInArray(v)),
v => transformIn(v === null ? [null] : wrapInArray(v, emptyValues.value)),
v => {
const transformed = transformOut(v)
return props.multiple ? transformed : (transformed[0] ?? null)
Expand Down Expand Up @@ -385,7 +385,7 @@ export const VAutocomplete = genericComponent<new <
} else {
if (!props.multiple && search.value == null) model.value = []
menu.value = false
if (!model.value.some(({ title }) => title === search.value)) search.value = ''
search.value = ''
selectionIndex.value = -1
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/vuetify/src/components/VCombobox/VCombobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const VCombobox = genericComponent<new <
props,
'modelValue',
[],
v => transformIn(wrapInArray(v)),
v => transformIn(wrapInArray(v, [''])),
v => {
const transformed = transformOut(v)
return props.multiple ? transformed : (transformed[0] ?? null)
Expand Down
4 changes: 2 additions & 2 deletions packages/vuetify/src/components/VSelect/VSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ export const VSelect = genericComponent<new <
_menu.value = v
},
})
const { items, transformIn, transformOut } = useItems(props)
const { items, transformIn, transformOut, emptyValues } = useItems(props)
const model = useProxiedModel(
props,
'modelValue',
[],
v => transformIn(v === null ? [null] : wrapInArray(v)),
v => transformIn(v === null ? [null] : wrapInArray(v, emptyValues.value)),
v => {
const transformed = transformOut(v)
return props.multiple ? transformed : (transformed[0] ?? null)
Expand Down
7 changes: 5 additions & 2 deletions packages/vuetify/src/composables/list-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export function transformItems (props: Omit<ItemProps, 'items'>, items: ItemProp

export function useItems (props: ItemProps) {
const items = computed(() => transformItems(props, props.items))
const hasNullItem = computed(() => items.value.some(item => item.value === null))
const allValues = computed(() => items.value.map(item => item.value))
const hasNullItem = computed(() => allValues.value.includes(null))

function transformIn (value: any[]): ListItem[] {
if (!hasNullItem.value) {
Expand All @@ -120,5 +121,7 @@ export function useItems (props: ItemProps) {
: value.map(({ value }) => value)
}

return { items, transformIn, transformOut }
const emptyValues = computed(() => ['', null, undefined].filter(v => !allValues.value.includes(v)))

return { items, transformIn, transformOut, emptyValues }
}
5 changes: 3 additions & 2 deletions packages/vuetify/src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,12 @@ export function arrayDiff (a: any[], b: any[]): any[] {

type IfAny<T, Y, N> = 0 extends (1 & T) ? Y : N;
export function wrapInArray<T> (
v: T | null | undefined
v: T | null | undefined,
emptyValues: any[] = []
): T extends readonly any[]
? IfAny<T, T[], T>
: NonNullable<T>[] {
return v == null
return v == null || emptyValues.includes(v)
? []
: Array.isArray(v)
? v as any : [v]
Expand Down