11/* eslint-disable complexity -- TODO: refactor */
22import styles from "./styles/index.module.scss" ;
3- import React , { useCallback , useMemo , useRef , forwardRef } from "react" ;
3+ import React , { useCallback , useMemo , useRef , forwardRef , useEffect } from "react" ;
44import classNames from "classnames" ;
55import { Text } from "../text" ;
66import { Button , type ButtonProps } from "../button" ;
@@ -42,6 +42,8 @@ interface FileUploaderProps
4242 ) => void ;
4343 /** Callback fired during file reading progress */
4444 onProgress ?: Parameters < typeof readFileList > [ 1 ] ;
45+ /** Try topen the file input immediately when the component is mounted */
46+ openImmediately ?: boolean ;
4547}
4648
4749/**
@@ -62,13 +64,16 @@ const FileUploader = forwardRef<HTMLInputElement, Readonly<FileUploaderProps>>(
6264 buttonProps,
6365 onChange,
6466 onProgress,
67+ openImmediately,
6568 ...otherProps
6669 } ,
6770 ref ,
6871 ) : React . ReactElement => {
6972 const inputRef = useRef < HTMLInputElement > ( null ) ;
7073 const isIconOnly = useMemo ( ( ) => Boolean ( icon ) && ! description , [ description , icon ] ) ;
7174
75+ const isFirstRender = useRef ( true ) ;
76+
7277 const handleKeyUp = useCallback ( ( e : React . KeyboardEvent < HTMLElement > ) : void => {
7378 if ( e . code === "Enter" ) inputRef . current ?. click ( ) ;
7479 } , [ ] ) ;
@@ -86,6 +91,14 @@ const FileUploader = forwardRef<HTMLInputElement, Readonly<FileUploaderProps>>(
8691 [ inputProps , onChange , onProgress ] ,
8792 ) ;
8893
94+ useEffect ( ( ) => {
95+ if ( openImmediately && isFirstRender . current ) inputRef . current ?. click ( ) ;
96+ } , [ openImmediately ] ) ;
97+
98+ useEffect ( ( ) => {
99+ isFirstRender . current = false ;
100+ } , [ ] ) ;
101+
89102 return (
90103 < div
91104 { ...otherProps }
0 commit comments