Skip to content

Commit c78ef57

Browse files
committed
feat(file uploader): allow open file picker immideately
1 parent 75a75bf commit c78ef57

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

packages/components/base/src/file-uploader/FileUploader.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable complexity -- TODO: refactor */
22
import 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";
44
import classNames from "classnames";
55
import { Text } from "../text";
66
import { 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

Comments
 (0)