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(VAutocomplete/VCombobox): allow delete in single selection mode #19387

Merged
merged 3 commits into from
Mar 13, 2024
Merged
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
17 changes: 11 additions & 6 deletions packages/vuetify/src/components/VAutocomplete/VAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,13 @@ export const VAutocomplete = genericComponent<new <
listRef.value?.focus('next')
}

if (!props.multiple) return

if (['Backspace', 'Delete'].includes(e.key)) {
if (
!props.multiple &&
hasSelectionSlot.value &&
model.value.length > 0
) return select(model.value[0], false)

if (selectionIndex.value < 0) {
if (e.key === 'Backspace' && !search.value) {
selectionIndex.value = length - 1
Expand All @@ -258,12 +262,13 @@ export const VAutocomplete = genericComponent<new <
}

const originalSelectionIndex = selectionIndex.value
const selectedItem = model.value[selectionIndex.value]
if (selectedItem && !selectedItem.props.disabled) select(selectedItem, false)
select(model.value[selectionIndex.value], false)

selectionIndex.value = originalSelectionIndex >= length - 1 ? (length - 2) : originalSelectionIndex
}

if (!props.multiple) return

if (e.key === 'ArrowLeft') {
if (selectionIndex.value < 0 && selectionStart > 0) return

Expand Down Expand Up @@ -325,8 +330,8 @@ export const VAutocomplete = genericComponent<new <
const isSelecting = shallowRef(false)

/** @param set - null means toggle */
function select (item: ListItem, set: boolean | null = true) {
if (item.props.disabled) return
function select (item: ListItem | undefined, set: boolean | null = true) {
if (!item || item.props.disabled) return

if (props.multiple) {
const index = model.value.findIndex(selection => props.valueComparator(selection.value, item.value))
Expand Down
19 changes: 12 additions & 7 deletions packages/vuetify/src/components/VCombobox/VCombobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export const VCombobox = genericComponent<new <
}
menu.value = !menu.value
}
// eslint-disable-next-line complexity
function onKeydown (e: KeyboardEvent) {
if (isComposingIgnoreKey(e) || props.readonly || form?.isReadonly.value) return

Expand Down Expand Up @@ -306,24 +307,28 @@ export const VCombobox = genericComponent<new <
if (hasSelectionSlot.value) _search.value = ''
}

if (!props.multiple) return

if (['Backspace', 'Delete'].includes(e.key)) {
if (
!props.multiple &&
hasSelectionSlot.value &&
model.value.length > 0
) return select(model.value[0], false)

if (selectionIndex.value < 0) {
if (e.key === 'Backspace' && !search.value) {
selectionIndex.value = length - 1
}

return
}

const originalSelectionIndex = selectionIndex.value
const selectedItem = model.value[selectionIndex.value]
if (selectedItem && !selectedItem.props.disabled) select(selectedItem, false)
select(model.value[selectionIndex.value], false)

selectionIndex.value = originalSelectionIndex >= length - 1 ? (length - 2) : originalSelectionIndex
}

if (!props.multiple) return

if (e.key === 'ArrowLeft') {
if (selectionIndex.value < 0 && selectionStart > 0) return

Expand Down Expand Up @@ -359,8 +364,8 @@ export const VCombobox = genericComponent<new <
}
}
/** @param set - null means toggle */
function select (item: ListItem, set: boolean | null = true) {
if (item.props.disabled) return
function select (item: ListItem | undefined, set: boolean | null = true) {
if (!item || item.props.disabled) return

if (props.multiple) {
const index = model.value.findIndex(selection => props.valueComparator(selection.value, item.value))
Expand Down
Loading