Skip to content

Commit

Permalink
fix(comp:cascader): the value is incorrect after clicking the clear i…
Browse files Browse the repository at this point in the history
…con (#1774)
  • Loading branch information
danranVm committed Dec 27, 2023
1 parent 4b39705 commit 337c894
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
14 changes: 14 additions & 0 deletions packages/components/cascader/__tests__/cascader.spec.ts
Expand Up @@ -229,6 +229,20 @@ describe('Cascader', () => {
expect(getAllOptionGroup(wrapper)[2].find('.ix-cascader-option-disabled').exists()).toBe(false)
expect(getAllOptionGroup(wrapper)[0].find('.ix-cascader-option-disabled').text()).toBe('Pro')
})

test('clearable work', async () => {
const onUpdateValue = vi.fn()
const wrapper = CascaderMount({
props: {
clearable: true,
'onUpdate:value': onUpdateValue,
},
})

await wrapper.find('.ix-selector-clear').trigger('click')

expect(onUpdateValue).toBeCalledWith([])
})
})

describe('multiple work', () => {
Expand Down
36 changes: 19 additions & 17 deletions packages/components/cascader/src/composables/useSelectedState.ts
Expand Up @@ -76,28 +76,30 @@ export function useSelectedState(
return [...indeterminateKeySet]
})

const setValue = (keys: VKey[]) => {
let currValue: VKey | VKey[] | VKey[][]
const getFullPath = (currKey: VKey) => {
const getDisabledFn = mergedGetDisabled.value
const dataMap = mergedDataMap.value
const fullPath = getParentKeys(dataMap, dataMap.get(currKey), true, getDisabledFn)
fullPath.push(currKey)
return fullPath
}

const getCurrValue = (keys: VKey[]) => {
if (!mergedFullPath.value) {
currValue = multiple.value ? keys : keys[0]
return multiple.value ? keys : keys[0]
}
if (keys.length === 0) {
return keys
}
if (multiple.value) {
return keys.map(getFullPath)
} else {
const getDisabledFn = mergedGetDisabled.value
if (!multiple.value) {
const currKey = keys[0]
const dataMap = mergedDataMap.value
currValue = getParentKeys(dataMap, dataMap.get(currKey), true, getDisabledFn)
currValue.push(currKey)
} else {
const dataMap = mergedDataMap.value
currValue = keys.map(currKey => {
const parentKeys = getParentKeys(dataMap, dataMap.get(currKey), true, getDisabledFn)
parentKeys.push(currKey)
return parentKeys
})
}
return getFullPath(keys[0])
}
}

const setValue = (keys: VKey[]) => {
const currValue = getCurrValue(keys)
const oldValue = toRaw(selectedKeys.value)
if (currValue !== oldValue) {
setSelectedKeys(currValue)
Expand Down

0 comments on commit 337c894

Please sign in to comment.