11import { default as AntdUpload } from "antd/es/upload" ;
22import { default as Button } from "antd/es/button" ;
33import { UploadFile , UploadChangeParam , UploadFileStatus , RcFile } from "antd/es/upload/interface" ;
4- import { useState , useEffect } from "react" ;
4+ import { useState , useMemo } from "react" ;
55import styled , { css } from "styled-components" ;
66import { trans } from "i18n" ;
77import _ from "lodash" ;
@@ -11,8 +11,7 @@ import {
1111 multiChangeAction ,
1212} from "lowcoder-core" ;
1313import { hasIcon } from "comps/utils" ;
14- import { messageInstance } from "lowcoder-design/src/components/GlobalInstances" ;
15- import { resolveValue , resolveParsedValue , commonProps } from "./fileComp" ;
14+ import { resolveValue , resolveParsedValue , commonProps , validateFile , CaptureResolution } from "./fileComp" ;
1615import { FileStyleType , AnimationStyleType , heightCalculator , widthCalculator } from "comps/controls/styleControlConstants" ;
1716import { ImageCaptureModal } from "./ImageCaptureModal" ;
1817import { v4 as uuidv4 } from "uuid" ;
@@ -149,9 +148,11 @@ interface DraggerUploadProps {
149148 prefixIcon : any ;
150149 suffixIcon : any ;
151150 forceCapture : boolean ;
151+ captureResolution : CaptureResolution ;
152152 minSize : number ;
153153 maxSize : number ;
154154 maxFiles : number ;
155+ fileNamePattern : string ;
155156 uploadType : "single" | "multiple" | "directory" ;
156157 text : string ;
157158 dragHintText ?: string ;
@@ -162,25 +163,27 @@ interface DraggerUploadProps {
162163
163164export const DraggerUpload = ( props : DraggerUploadProps ) => {
164165 const { dispatch, files, style, autoHeight, animationStyle } = props ;
165- const [ fileList , setFileList ] = useState < UploadFile [ ] > (
166- files . map ( ( f ) => ( { ...f , status : "done" } ) ) as UploadFile [ ]
167- ) ;
166+ // Track only files currently being uploaded (not yet in props.files)
167+ const [ uploadingFiles , setUploadingFiles ] = useState < UploadFile [ ] > ( [ ] ) ;
168168 const [ showModal , setShowModal ] = useState ( false ) ;
169169 const isMobile = checkIsMobile ( window . innerWidth ) ;
170170
171- useEffect ( ( ) => {
172- if ( files . length === 0 && fileList . length !== 0 ) {
173- setFileList ( [ ] ) ;
174- }
175- } , [ files ] ) ;
171+ // Derive fileList from props.files (source of truth) + currently uploading files
172+ const fileList = useMemo < UploadFile [ ] > ( ( ) => [
173+ ... ( files . map ( ( f ) => ( { ... f , status : "done" as const } ) ) as UploadFile [ ] ) ,
174+ ... uploadingFiles ,
175+ ] , [ files , uploadingFiles ] ) ;
176176
177177 const handleOnChange = ( param : UploadChangeParam ) => {
178- const uploadingFiles = param . fileList . filter ( ( f ) => f . status === "uploading" ) ;
179- if ( uploadingFiles . length !== 0 ) {
180- setFileList ( param . fileList ) ;
178+ const currentlyUploading = param . fileList . filter ( ( f ) => f . status === "uploading" ) ;
179+ if ( currentlyUploading . length !== 0 ) {
180+ setUploadingFiles ( currentlyUploading ) ;
181181 return ;
182182 }
183183
184+ // Clear uploading state when all uploads complete
185+ setUploadingFiles ( [ ] ) ;
186+
184187 let maxFiles = props . maxFiles ;
185188 if ( props . uploadType === "single" ) {
186189 maxFiles = 1 ;
@@ -240,8 +243,6 @@ export const DraggerUpload = (props: DraggerUploadProps) => {
240243 props . onEvent ( "parse" ) ;
241244 } ) ;
242245 }
243-
244- setFileList ( uploadedFiles . slice ( - maxFiles ) ) ;
245246 } ;
246247
247248 return (
@@ -254,21 +255,11 @@ export const DraggerUpload = (props: DraggerUploadProps) => {
254255 $auto = { autoHeight }
255256 capture = { props . forceCapture }
256257 openFileDialogOnClick = { ! ( props . forceCapture && ! isMobile ) }
257- beforeUpload = { ( file ) => {
258- if ( ! file . size || file . size <= 0 ) {
259- messageInstance . error ( `${ file . name } ` + trans ( "file.fileEmptyErrorMsg" ) ) ;
260- return AntdUpload . LIST_IGNORE ;
261- }
262-
263- if (
264- ( ! ! props . minSize && file . size < props . minSize ) ||
265- ( ! ! props . maxSize && file . size > props . maxSize )
266- ) {
267- messageInstance . error ( `${ file . name } ` + trans ( "file.fileSizeExceedErrorMsg" ) ) ;
268- return AntdUpload . LIST_IGNORE ;
269- }
270- return true ;
271- } }
258+ beforeUpload = { ( file ) => validateFile ( file , {
259+ minSize : props . minSize ,
260+ maxSize : props . maxSize ,
261+ fileNamePattern : props . fileNamePattern ,
262+ } ) }
272263 onChange = { handleOnChange }
273264 >
274265 < p className = "ant-upload-drag-icon" >
@@ -301,6 +292,7 @@ export const DraggerUpload = (props: DraggerUploadProps) => {
301292
302293 < ImageCaptureModal
303294 showModal = { showModal }
295+ captureResolution = { props . captureResolution as CaptureResolution }
304296 onModalClose = { ( ) => setShowModal ( false ) }
305297 onImageCapture = { async ( image ) => {
306298 setShowModal ( false ) ;
0 commit comments