Skip to content

Commit

Permalink
refactor(VSelects): normalize select process as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
johnleider committed Feb 27, 2024
1 parent 12c9e27 commit b23c44f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
32 changes: 16 additions & 16 deletions packages/vuetify/src/components/VAutocomplete/VAutocomplete.tsx
Expand Up @@ -255,9 +255,8 @@ export const VAutocomplete = genericComponent<new <
}

const originalSelectionIndex = selectionIndex.value

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

selectionIndex.value = originalSelectionIndex >= length - 1 ? (length - 2) : originalSelectionIndex
}
Expand Down Expand Up @@ -322,34 +321,35 @@ export const VAutocomplete = genericComponent<new <

const isSelecting = shallowRef(false)

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

if (props.multiple) {
const index = model.value.findIndex(selection => props.valueComparator(selection.value, item.value))
add = index === -1
if (add) {
model.value = [...model.value, item]
} else {
const value = [...model.value]
const add = set == null ? !~index : set

if (~index) {
const value = add ? [...model.value, item] : [...model.value]
value.splice(index, 1)
model.value = value
} else if (add) {
model.value = [...model.value, item]
}

if (props.clearOnSelect) {
search.value = ''
}
} else {
const add = set !== false
model.value = add ? [item] : []

isSelecting.value = true

search.value = add ? item.title : ''

menu.value = false
isPristine.value = true

nextTick(() => (isSelecting.value = false))
// watch for search watcher to trigger
nextTick(() => {
menu.value = false
isPristine.value = true
})
}
}

Expand Down Expand Up @@ -487,7 +487,7 @@ export const VAutocomplete = genericComponent<new <
ref: itemRef,
key: index,
active: (highlightFirst.value && index === 0) ? true : undefined,
onClick: () => select(item),
onClick: () => select(item, null),
})

return slots.item?.({
Expand Down
2 changes: 2 additions & 0 deletions packages/vuetify/src/components/VCombobox/VCombobox.tsx
Expand Up @@ -354,6 +354,8 @@ export const VCombobox = genericComponent<new <
}
/** @param set - null means toggle */
function select (item: ListItem, set: boolean | null = true) {
if (item.props.disabled) return

if (props.multiple) {
const index = model.value.findIndex(selection => props.valueComparator(selection.value, item.value))
const add = set == null ? !~index : set
Expand Down
27 changes: 18 additions & 9 deletions packages/vuetify/src/components/VSelect/VSelect.tsx
Expand Up @@ -24,7 +24,7 @@ import { useProxiedModel } from '@/composables/proxiedModel'
import { makeTransitionProps } from '@/composables/transition'

// Utilities
import { computed, mergeProps, ref, shallowRef, watch } from 'vue'
import { computed, mergeProps, nextTick, ref, shallowRef, watch } from 'vue'
import {
ensureValidVNode,
genericComponent,
Expand Down Expand Up @@ -252,20 +252,29 @@ export const VSelect = genericComponent<new <
model.value = [item]
}
}
function select (item: ListItem, add = true) {

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

if (props.multiple) {
const index = model.value.findIndex(selection => props.valueComparator(selection.value, item.value))
add = index === -1
if (add) {
model.value = [...model.value, item]
} else {
const value = [...model.value]
const add = set == null ? !~index : set

if (~index) {
const value = add ? [...model.value, item] : [...model.value]
value.splice(index, 1)
model.value = value
} else if (add) {
model.value = [...model.value, item]
}
} else {
const add = set !== false
model.value = add ? [item] : []
menu.value = false

nextTick(() => {
menu.value = false
})
}
}
function onBlur (e: FocusEvent) {
Expand Down Expand Up @@ -401,7 +410,7 @@ export const VSelect = genericComponent<new <
const itemProps = mergeProps(item.props, {
ref: itemRef,
key: index,
onClick: () => select(item),
onClick: () => select(item, null),
})

return slots.item?.({
Expand Down

0 comments on commit b23c44f

Please sign in to comment.