@@ -29,7 +29,7 @@ interface IProps {
2929 onError ?: ( error : EUploadError ) => void ;
3030}
3131
32- let UploadWrapper : FC < IProps > = React . memo ( ( props ) => {
32+ export let useUploadTrigger = ( props : IProps ) => {
3333 let inputElement = useRef ( null ) ;
3434
3535 let inputAccepts = props . accept ;
@@ -40,6 +40,59 @@ let UploadWrapper: FC<IProps> = React.memo((props) => {
4040 }
4141 }
4242
43+ let ui = (
44+ < input
45+ title = ""
46+ className = { styleInput }
47+ ref = { inputElement }
48+ type = "file"
49+ accept = { inputAccepts }
50+ multiple = { props . multiple }
51+ onChange = { async ( e ) => {
52+ if ( props . onChange ) {
53+ const fileList = Array . from ( e . target . files ) ; // copy to array before resetting
54+
55+ e . target . files = null ; // without resetting, not able to trigger a change after failed
56+
57+ if ( fileList . length > 0 ) {
58+ const files : File [ ] = [ ] ;
59+
60+ fileList . forEach ( ( file ) => {
61+ const fileExtension = file . name . split ( "." ) . pop ( ) ;
62+ if ( props . acceptedFileTypes && ! props . acceptedFileTypes . includes ( fileExtension ) ) {
63+ if ( props . onError ) {
64+ props . onError ( EUploadError . unsupportedFileType ) ;
65+ }
66+ message . error ( interpolateLocale ( uploadingLocales . unsupportedFileType , { type : props . acceptedFileTypes . join ( ", " ) } ) ) ;
67+ return ;
68+ }
69+ files . push ( file ) ;
70+ } ) ;
71+
72+ props . onChange ( files ) ;
73+ }
74+ }
75+ } }
76+ />
77+ ) ;
78+
79+ let onUpload = ( ) => {
80+ if ( inputElement . current != null ) {
81+ inputElement . current . click ( ) ;
82+ } else {
83+ console . error ( "input element for files is not mounted!" ) ;
84+ }
85+ } ;
86+
87+ return {
88+ ui,
89+ onUpload,
90+ } ;
91+ } ;
92+
93+ let UploadWrapper : FC < IProps > = React . memo ( ( props ) => {
94+ let uploadPlugin = useUploadTrigger ( props ) ;
95+
4396 /** Plugins */
4497 /** Methods */
4598 /** Effects */
@@ -48,48 +101,11 @@ let UploadWrapper: FC<IProps> = React.memo((props) => {
48101 < div
49102 className = { cx ( styleWrapper , props . className ) }
50103 onClick = { ( ) => {
51- if ( inputElement . current != null ) {
52- inputElement . current . click ( ) ;
53- } else {
54- console . error ( "Input element is missing in upload wrapper" ) ;
55- }
104+ uploadPlugin . onUpload ( ) ;
56105 } }
57106 >
58107 { props . children }
59-
60- < input
61- title = ""
62- className = { styleInput }
63- ref = { inputElement }
64- type = "file"
65- accept = { inputAccepts }
66- multiple = { props . multiple }
67- onChange = { async ( e ) => {
68- if ( props . onChange ) {
69- const fileList = Array . from ( e . target . files ) ; // copy to array before resetting
70-
71- e . target . files = null ; // without resetting, not able to trigger a change after failed
72-
73- if ( fileList . length > 0 ) {
74- const files : File [ ] = [ ] ;
75-
76- fileList . forEach ( ( file ) => {
77- const fileExtension = file . name . split ( "." ) . pop ( ) ;
78- if ( props . acceptedFileTypes && ! props . acceptedFileTypes . includes ( fileExtension ) ) {
79- if ( props . onError ) {
80- props . onError ( EUploadError . unsupportedFileType ) ;
81- }
82- message . error ( interpolateLocale ( uploadingLocales . unsupportedFileType , { type : props . acceptedFileTypes . join ( ", " ) } ) ) ;
83- return ;
84- }
85- files . push ( file ) ;
86- } ) ;
87-
88- props . onChange ( files ) ;
89- }
90- }
91- } }
92- />
108+ { uploadPlugin . ui }
93109 </ div >
94110 ) ;
95111} ) ;
0 commit comments