Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(input): missing clear button with file input type #4599

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-seas-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heroui/input": patch
---

fix clear button with file input type (#4592)
13 changes: 9 additions & 4 deletions packages/components/input/src/use-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,26 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
handleValueChange,
);

const isFileTypeInput = type === "file";
const hasUploadedFiles = ((domRef?.current as HTMLInputElement)?.files?.length ?? 0) > 0;
const isFilledByDefault = ["date", "time", "month", "week", "range"].includes(type!);
const isFilled = !isEmpty(inputValue) || isFilledByDefault;
const isFilled = !isEmpty(inputValue) || isFilledByDefault || hasUploadedFiles;
const isFilledWithin = isFilled || isFocusWithin;
const isHiddenType = type === "hidden";
const isMultiline = originalProps.isMultiline;
const isFileTypeInput = type === "file";

const baseStyles = clsx(classNames?.base, className, isFilled ? "is-filled" : "");

const handleClear = useCallback(() => {
setInputValue("");
if (isFileTypeInput) {
(domRef.current as HTMLInputElement).value = "";
} else {
setInputValue("");
}

onClear?.();
domRef.current?.focus();
}, [setInputValue, onClear]);
}, [setInputValue, onClear, isFileTypeInput]);

// if we use `react-hook-form`, it will set the input value using the ref in register
// i.e. setting ref.current.value to something which is uncontrolled
Expand Down
Loading