diff --git a/src/tree-select/__tests__/__snapshots__/index.test.jsx.snap b/src/tree-select/__tests__/__snapshots__/index.test.jsx.snap index 499c5bb68..86be716a8 100644 --- a/src/tree-select/__tests__/__snapshots__/index.test.jsx.snap +++ b/src/tree-select/__tests__/__snapshots__/index.test.jsx.snap @@ -465,49 +465,30 @@ exports[`TreeSelect > :props > :minCollapsedNum 1`] = ` exports[`TreeSelect > :props > :multiple 1`] = `
-
- - - - -
-
+ /> + > + 请选择 + diff --git a/src/tree-select/useTreeSelect.ts b/src/tree-select/useTreeSelect.ts index 31b6ac482..3dedea45c 100644 --- a/src/tree-select/useTreeSelect.ts +++ b/src/tree-select/useTreeSelect.ts @@ -12,7 +12,7 @@ import { TreeOptionData, TreeKeysType } from '../common'; import useVModel from '../hooks/useVModel'; import useDefaultValue from '../hooks/useDefaultValue'; import { SelectInputProps } from '../select-input'; -import { getNodeDataByValue } from './utils'; +import { getNodeDataByValue, normalizeArray } from './utils'; const DEFAULT_KEYS = { label: 'label', @@ -107,7 +107,7 @@ export default function useTreeSelect(props: TdTreeSelectProps, context: SetupCo // multiple tree select node info list function getMultipleNodeInfo(): TreeOptionData[] { const { value } = treeSelectValue; - const list = Array.isArray(value) ? value : [value]; + const list = normalizeArray(value); if (treeRef.value) { return list.map((item) => { const finalValue = typeof item === 'object' ? item.value : item; diff --git a/src/tree-select/utils.ts b/src/tree-select/utils.ts index eaf24c885..fe59984b4 100644 --- a/src/tree-select/utils.ts +++ b/src/tree-select/utils.ts @@ -1,6 +1,8 @@ +import isNil from 'lodash/isNil'; import lodashGet from 'lodash/get'; import lodashSet from 'lodash/set'; import { TreeOptionData, TreeKeysType } from '../common'; +import type { TreeSelectValue } from './type'; export function getNodeDataByValue( values: Array, @@ -47,4 +49,12 @@ export function getNodeDataByValue( return values.map((value) => results.get(value)); } +export function normalizeArray(value: TreeSelectValue) { + if (isNil(value)) { + return []; + } + + return Array.isArray(value) ? value : [value]; +} + export default {};