11import { useEffect , useMemo , useRef , useState } from 'react' ;
22import { isArray , isEqual , isFunction } from 'lodash-es' ;
33
4+ import { pathToKey } from '@tdesign/common-js/tree-v1/tree-node-model' ;
45import TreeStore from '@tdesign/common-js/tree-v1/tree-store' ;
56import type { TypeTreeNodeData } from '@tdesign/common-js/tree-v1/types' ;
67
78import useControlled from '../hooks/useControlled' ;
89import useDefaultProps from '../hooks/useDefaultProps' ;
910import { treeNodesEffect , treeStoreExpendEffect } from './core/effect' ;
10- import { getCascaderValue , getTreeValue , isEmptyValues , isValueInvalid } from './core/helper' ;
11+ import { getTreeValue , isEmptyValues , isValueInvalid } from './core/helper' ;
1112import { cascaderDefaultProps } from './defaultProps' ;
1213
1314import type { TreeOptionData } from '../common' ;
@@ -122,6 +123,7 @@ export const useCascaderContext = (originalProps: TdCascaderProps) => {
122123 ...keys ,
123124 children : typeof keys . children === 'string' ? keys . children : 'children' ,
124125 } ,
126+ allowDuplicateValue : props . valueType === 'full' ,
125127 onLoad : ( ) => {
126128 setTimeout ( ( ) => {
127129 store . refreshNodes ( ) ;
@@ -135,7 +137,7 @@ export const useCascaderContext = (originalProps: TdCascaderProps) => {
135137 } else {
136138 treeStore . reload ( options ) ;
137139 treeStore . refreshNodes ( ) ;
138- treeStoreExpendEffect ( treeStore , scopeVal , [ ] ) ;
140+ treeStoreExpendEffect ( treeStore , scopeVal , [ ] , props . valueType ) ;
139141 treeNodesEffect ( inputVal , treeStore , setTreeNodes , props . filter , isParentFilterable ) ;
140142 }
141143 } ;
@@ -161,14 +163,14 @@ export const useCascaderContext = (originalProps: TdCascaderProps) => {
161163
162164 // value 校验逻辑
163165 useEffect ( ( ) => {
164- const { setValue, multiple, valueType = 'single' } = cascaderContext ;
166+ const { setValue, multiple } = cascaderContext ;
165167
166168 if ( isValueInvalid ( innerValue , cascaderContext ) ) {
167169 setValue ( multiple ? [ ] : '' , 'invalid-value' ) ;
168170 }
169171
170172 if ( ! isEmptyValues ( innerValue ) ) {
171- setScopeVal ( getCascaderValue ( innerValue , valueType , multiple ) ) ;
173+ setScopeVal ( innerValue ) ;
172174 } else {
173175 setScopeVal ( multiple ? [ ] : '' ) ;
174176 }
@@ -181,8 +183,8 @@ export const useCascaderContext = (originalProps: TdCascaderProps) => {
181183
182184 useEffect ( ( ) => {
183185 if ( ! treeStore ) return ;
184- treeStoreExpendEffect ( treeStore , scopeVal , expend ) ;
185- } , [ treeStore , scopeVal , expend ] ) ;
186+ treeStoreExpendEffect ( treeStore , scopeVal , expend , props . valueType ) ;
187+ } , [ treeStore , scopeVal , expend , props . valueType ] ) ;
186188
187189 useEffect ( ( ) => {
188190 if ( ! treeStore ) return ;
@@ -191,8 +193,27 @@ export const useCascaderContext = (originalProps: TdCascaderProps) => {
191193
192194 useEffect ( ( ) => {
193195 if ( ! treeStore ) return ;
194- treeStore . replaceChecked ( getTreeValue ( scopeVal ) ) ;
195- } , [ options , scopeVal , treeStore , multiple ] ) ;
196+
197+ const { valueType } = props ;
198+
199+ const getCheckedKeys = ( ) : TreeNodeValue [ ] => {
200+ if ( valueType !== 'full' ) {
201+ return getTreeValue ( scopeVal ) ;
202+ }
203+
204+ const isValidPath = ( path : unknown ) : path is TreeNodeValue [ ] => Array . isArray ( path ) && path . length > 0 ;
205+
206+ // 多选模式
207+ if ( multiple && Array . isArray ( scopeVal ) ) {
208+ return ( scopeVal as TreeNodeValue [ ] [ ] ) . filter ( isValidPath ) . map ( pathToKey ) ;
209+ }
210+
211+ // 单选模式
212+ return isValidPath ( scopeVal ) ? [ pathToKey ( scopeVal ) ] : [ ] ;
213+ } ;
214+
215+ treeStore . replaceChecked ( getCheckedKeys ( ) ) ;
216+ } , [ options , scopeVal , treeStore , multiple , props ] ) ;
196217
197218 useEffect ( ( ) => {
198219 if ( ! innerPopupVisible && isFilterable ) {
@@ -212,32 +233,29 @@ export const useCascaderContext = (originalProps: TdCascaderProps) => {
212233 multiple : TdCascaderProps [ 'multiple' ] ,
213234 ) => {
214235 const { treeStore } = cascaderContext ;
236+ if ( ! treeStore ) return [ ] ;
237+
215238 const optionsData : TreeOptionData [ ] = [ ] ;
216239
217- if ( ! treeStore ) return optionsData ;
240+ const pushNodeData = ( node ?: TreeNode ) => {
241+ if ( node ?. data ) optionsData . push ( node . data ) ;
242+ } ;
218243
219244 if ( valueType === 'full' ) {
220- if ( multiple ) {
221- // 未来需支持全路径拼接搜索
222- arrValue . forEach ( ( value ) => {
223- if ( isArray ( value ) && value . length ) {
224- const nodeValue = value [ value . length - 1 ] ;
225- const [ node ] = treeStore . getNodes ( nodeValue ) || [ ] ;
226- node ?. data && optionsData . push ( node . data ) ;
227- }
228- } ) ;
229- } else if ( isArray ( arrValue ) && arrValue . length ) {
230- const nodeValue = arrValue [ arrValue . length - 1 ] ;
231- const [ node ] = treeStore . getNodes ( nodeValue ) || [ ] ;
232- node ?. data && optionsData . push ( node . data ) ;
233- }
234- } else if ( valueType === 'single' ) {
235- arrValue . forEach ( ( value ) => {
236- const [ node ] = treeStore . getNodes ( value ) || [ ] ;
237- node ?. data && optionsData . push ( node . data ) ;
245+ const values = multiple ? arrValue : [ arrValue ] ;
246+ values . forEach ( ( v ) => {
247+ if ( isArray ( v ) && v . length ) {
248+ pushNodeData ( treeStore . getNode ( v as TreeNodeValue [ ] ) ) ;
249+ }
238250 } ) ;
251+ return optionsData ;
239252 }
240253
254+ arrValue . forEach ( ( v ) => {
255+ const node = treeStore . getNodes ( v ) ?. [ 0 ] ;
256+ pushNodeData ( node ) ;
257+ } ) ;
258+
241259 return optionsData ;
242260 } ;
243261
0 commit comments