Skip to content
Draft
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
36 changes: 36 additions & 0 deletions packages/varlet-ui/src/data-table/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,42 @@ describe('test data-table component props', () => {
wrapper.unmount()
})

test('should sync every ancestor in a deeply nested tree', async () => {
const onUpdateCheckedRowKeys = vi.fn()
const deepTree = [
{
id: 1,
name: 'Level 1',
nodes: [
{
id: 2,
name: 'Level 2',
nodes: [{ id: 3, name: 'Level 3', nodes: [{ id: 4, name: 'Level 4', nodes: [{ id: 5, name: 'Leaf' }] }] }],
},
],
},
]
const wrapper = mount(VarDataTable, {
props: {
columns: [{ type: 'selection' }, ...columns],
data: deepTree,
pagination: false,
tree: true,
childrenKey: 'nodes',
expandedTreeRowKeys: [1, 2, 3, 4],
checkedRowKeys: [],
'onUpdate:checkedRowKeys': onUpdateCheckedRowKeys,
},
})

const checkboxes = wrapper.findAllComponents({ name: 'var-checkbox' })
checkboxes[5].vm.$emit('update:modelValue', true)
await nextTick()
expect(onUpdateCheckedRowKeys).toHaveBeenLastCalledWith([5, 4, 3, 2, 1])

wrapper.unmount()
})

test('should support non-cascading tree selection when cascade is false', async () => {
const onUpdateCheckedRowKeys = vi.fn()
const wrapper = mount(VarDataTable, {
Expand Down
30 changes: 19 additions & 11 deletions packages/varlet-ui/src/data-table/useSelectionColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,15 @@ export function useSelectionColumn({
return keys
}

function shouldAncestorBeChecked(row: Record<string, any>, nextKeys: Set<string | number>): boolean {
function shouldAncestorBeChecked(
row: Record<string, any>,
nextKeys: Set<string | number>,
checkedByRow: Map<Record<string, any>, boolean>,
): boolean {
if (checkedByRow.has(row)) {
return checkedByRow.get(row)!
}

const column = selectionColumn.value
const flatRow = treeRowMeta().rowByObject.get(row)

Expand All @@ -267,19 +275,19 @@ export function useSelectionColumn({
const selectable = isRowSelectable(row, flatRow.rowIndex, column)
const children = getTreeChildren(row)

if (!children.length) {
return selectable ? nextKeys.has(flatRow.key) : true
}
const checked = !children.length
? selectable
? nextKeys.has(flatRow.key)
: true
: selectable && children.every((child) => shouldAncestorBeChecked(child, nextKeys, checkedByRow))

return (
selectable &&
children.every((child) => {
return shouldAncestorBeChecked(child, nextKeys)
})
)
checkedByRow.set(row, checked)

return checked
}

function syncAncestorSelection(nextKeys: Set<string | number>, key: string | number) {
const checkedByRow = new Map<Record<string, any>, boolean>()
let parentKey = treeRowMeta().parentKeyByChild.get(key)

while (parentKey != null) {
Expand All @@ -289,7 +297,7 @@ export function useSelectionColumn({
break
}

if (shouldAncestorBeChecked(parentRow.row, nextKeys)) {
if (shouldAncestorBeChecked(parentRow.row, nextKeys, checkedByRow)) {
nextKeys.add(parentKey)
} else {
nextKeys.delete(parentKey)
Expand Down
33 changes: 32 additions & 1 deletion packages/varlet-ui/src/select/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mount } from '@vue/test-utils'
import { describe, expect, test, vi } from 'vite-plus/test'
import { createApp, h } from 'vue'
import { createApp, h, ref } from 'vue'
import { z } from 'zod'
import Select from '..'
import Option from '../../option'
Expand Down Expand Up @@ -372,6 +372,37 @@ test('select multiple value', async () => {
document.body.innerHTML = ''
})

test('select multiple object values from a reactive model', async () => {
const list = [{ id: 1 }, { id: 2 }]
const value = ref([list[0]])
const wrapper = mount(
{
...Wrapper,
setup: () => ({ list, value }),
template: `
<var-select multiple v-model="value">
<var-option label="first" :value="list[0]" />
<var-option label="second" :value="list[1]" />
</var-select>
`,
},
{ attachTo: document.body },
)

await wrapper.trigger('click')
const options = document.querySelectorAll('.var-option')
expect(options[0].querySelector('.var-checkbox--checked')).toBeTruthy()
expect(options[1].querySelector('.var-checkbox--checked')).toBeFalsy()

value.value = [list[1]]
await wrapper.vm.$nextTick()
expect(options[0].querySelector('.var-checkbox--checked')).toBeFalsy()
expect(options[1].querySelector('.var-checkbox--checked')).toBeTruthy()

wrapper.unmount()
document.body.innerHTML = ''
})

test('select multiple value in chips', async () => {
const onClose = vi.fn()
const onChange = vi.fn()
Expand Down
6 changes: 4 additions & 2 deletions packages/varlet-ui/src/select/useSelectController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isEmpty } from '@varlet/shared'
import { ref, watch } from 'vue'
import { ref, toRaw, watch } from 'vue'
import { type OptionProvider } from '../option/provide'

export interface UseSelectControllerOptions {
Expand Down Expand Up @@ -72,9 +72,11 @@ export function useSelectController(options: UseSelectControllerOptions) {
const options = optionProvidersGetter()

if (multiple) {
const selectedValues = new Set(toRaw(modelValue))

options.forEach((option) =>
option.sync(
modelValue.includes(getOptionProviderKey(option)),
selectedValues.has(toRaw(getOptionProviderKey(option))),
optionIsIndeterminate ? optionIsIndeterminate(option) : undefined,
),
)
Expand Down
13 changes: 10 additions & 3 deletions packages/varlet-use/src/useChildren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,23 @@ export function useChildren<P, C>(key: symbol | string) {
const length: ComputedRef<number> = computed(() => childInstances.length)

const sortInstances = () => {
const vNodes: any[] = flatVNodes(parentInstance.subTree)
const vNodes = flatVNodes(parentInstance.subTree)
const vNodeIndices = new Map<VNode, number>()

vNodes.forEach((vNode, index) => {
if (!vNodeIndices.has(vNode)) {
vNodeIndices.set(vNode, index)
}
})
const pairs = childInstances.map((instance, index) => ({
instance,
provider: childProviders[index],
}))

const getVNodeIndex = (instance: ComponentInternalInstance) => {
const index = vNodes.indexOf(instance.vnode)
const index = vNodeIndices.get(instance.vnode)

return index === -1 ? Number.MAX_SAFE_INTEGER : index
return index === undefined ? Number.MAX_SAFE_INTEGER : index
}

pairs.sort((a, b) => getVNodeIndex(a.instance) - getVNodeIndex(b.instance))
Expand Down
Loading