@@ -27,21 +27,15 @@ export default function useUpload(props: TdUploadProps) {
2727 const { disabled, autoUpload, isBatchUpload } = props ;
2828 const { classPrefix } = useConfig ( ) ;
2929 const [ globalLocale , t ] = useLocaleReceiver ( 'upload' ) ;
30+
3031 const [ uploadValue , setUploadValue ] = useControlled ( props , 'files' , props . onChange ) ;
32+ const prevUploadValueRef = useRef ( uploadValue ) ;
33+
3134 const xhrReq = useRef < { files : UploadFile [ ] ; xhrReq : XMLHttpRequest } [ ] > ( [ ] ) ;
3235 const [ toUploadFiles , setToUploadFiles ] = useState < UploadFile [ ] > ( [ ] ) ;
3336 const [ sizeOverLimitMessage , setSizeOverLimitMessage ] = useState ( '' ) ;
3437 const [ update , forceUpdate ] = useState ( { } ) ;
3538
36- // 标记 setUploadValue 是否由组件内部主动触发
37- // - true:内部调用(如选择文件、上传成功、onRemove 等),不需要进行外部清空联动
38- // - false:外部主动修改 props.files,需要联动清空 toUploadFiles
39- const internalUpdateRef = useRef ( false ) ;
40- const updateInternalValue : typeof setUploadValue = ( value , ...args ) => {
41- internalUpdateRef . current = true ;
42- setUploadValue ( value , ...args ) ;
43- } ;
44-
4539 const locale = useMemo ( ( ) => merge ( { } , globalLocale , props . locale ) , [ globalLocale , props . locale ] ) ;
4640
4741 const tipsClasses = `${ classPrefix } -upload__tips ${ classPrefix } -size-s` ;
@@ -100,7 +94,7 @@ export default function useUpload(props: TdUploadProps) {
10094 if ( props . autoUpload ) {
10195 setToUploadFiles ( [ ...toFiles ] ) ;
10296 } else {
103- updateInternalValue ( [ ...uploadValue ] , {
97+ setUploadValue ( [ ...uploadValue ] , {
10498 e : p . event ,
10599 trigger,
106100 index : uploadValue . length ,
@@ -158,7 +152,7 @@ export default function useUpload(props: TdUploadProps) {
158152 const handleNotAutoUpload = ( toFiles : UploadFile [ ] ) => {
159153 const tmpFiles = props . multiple && ! isBatchUpload ? uploadValue . concat ( toFiles ) : toFiles ;
160154 if ( ! tmpFiles . length ) return ;
161- updateInternalValue ( tmpFiles , {
155+ setUploadValue ( tmpFiles , {
162156 trigger : 'add' ,
163157 index : uploadValue . length ,
164158 file : toFiles [ 0 ] ,
@@ -302,7 +296,7 @@ export default function useUpload(props: TdUploadProps) {
302296 } ) . then ( ( { status, data, list, failedFiles } ) => {
303297 setUploading ( false ) ;
304298 if ( status === 'success' ) {
305- updateInternalValue ( [ ...data . files ] , {
299+ setUploadValue ( [ ...data . files ] , {
306300 trigger : 'add' ,
307301 file : data . files [ 0 ] ,
308302 } ) ;
@@ -348,16 +342,16 @@ export default function useUpload(props: TdUploadProps) {
348342 // remove all files for batchUpload
349343 if ( isBatchUpload || ! props . multiple ) {
350344 props . onWaitingUploadFilesChange ?.( { files : [ ] , trigger : 'remove' } ) ;
351- updateInternalValue ( [ ] , changePrams ) ;
345+ setUploadValue ( [ ] , changePrams ) ;
352346 setToUploadFiles ( [ ] ) ;
353347 xhrReq . current = [ ] ;
354348 } else if ( ! props . autoUpload ) {
355349 const newUploadValue = uploadValue . filter ( ( _ , i ) => i !== p . index ) ;
356- updateInternalValue ( newUploadValue , changePrams ) ;
350+ setUploadValue ( newUploadValue , changePrams ) ;
357351 } else if ( p . index < uploadValue . length ) {
358352 // autoUpload 场景下, p.index < uploadValue.length 表示移除已经上传成功的文件;反之表示移除待上传列表文件
359353 const newUploadValue = uploadValue . filter ( ( _ , i ) => i !== p . index ) ;
360- updateInternalValue ( newUploadValue , changePrams ) ;
354+ setUploadValue ( newUploadValue , changePrams ) ;
361355 } else {
362356 const tmpFiles = [ ...toUploadFiles ] ;
363357 tmpFiles . splice ( p . index - uploadValue . length , 1 ) ;
@@ -383,7 +377,7 @@ export default function useUpload(props: TdUploadProps) {
383377 if ( autoUpload ) {
384378 setToUploadFiles ( [ ] ) ;
385379 } else {
386- updateInternalValue (
380+ setUploadValue (
387381 uploadValue . map ( ( item ) => {
388382 if ( item . status !== 'success' ) {
389383 return { ...item , status : 'waiting' } ;
@@ -404,19 +398,15 @@ export default function useUpload(props: TdUploadProps) {
404398 // 矫正数据格式为数组
405399 useEffect ( ( ) => {
406400 if ( ! Array . isArray ( uploadValue ) ) {
407- updateInternalValue ( [ ] , { trigger : 'default' } ) ;
401+ setUploadValue ( [ ] , { trigger : 'default' } ) ;
408402 }
409403 // eslint-disable-next-line react-hooks/exhaustive-deps
410404 } , [ uploadValue ] ) ;
411405
412406 useEffect ( ( ) => {
413- // 监听受控值 props.files 的引用变化:
414- // - 若变化由组件内部触发(internalUpdateRef === true),消费标记后跳过
415- // - 若变化由外部主动触发且新值为空数组,则同步清空内部维护的 toUploadFiles,确保失败/等待的文件能被一起清除
416- if ( internalUpdateRef . current ) {
417- internalUpdateRef . current = false ;
418- return ;
419- }
407+ const prev = prevUploadValueRef . current ;
408+ prevUploadValueRef . current = uploadValue ;
409+ if ( prev === uploadValue ) return ;
420410 const currentLen = Array . isArray ( uploadValue ) ? uploadValue . length : 0 ;
421411 if ( currentLen === 0 && toUploadFiles . length > 0 ) {
422412 setToUploadFiles ( [ ] ) ;
0 commit comments