Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Solve Pressing ESC doesn't clear input value
  • Loading branch information
linyonglu committed Feb 21, 2025
1 parent 943dfdb commit c5d0b2f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-pandas-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heroui/input": major
---

- FIX: #4850
12 changes: 12 additions & 0 deletions packages/components/input/src/use-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
[slots, isLabelHovered, labelProps, classNames?.label],
);

const handleKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Escape" && inputValue) {
setInputValue("");
onClear?.();
}
},
[inputValue, setInputValue, onClear],
);

const getInputProps: PropGetter = useCallback(
(props = {}) => {
return {
Expand All @@ -375,6 +385,7 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
),
"aria-readonly": dataAttr(originalProps.isReadOnly),
onChange: chain(inputProps.onChange, onChange),
onKeyDown: chain(props.onKeyDown, handleKeyDown),
ref: domRef,
};
},
Expand All @@ -392,6 +403,7 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
originalProps.isReadOnly,
originalProps.isRequired,
onChange,
handleKeyDown,
],
);

Expand Down

0 comments on commit c5d0b2f

Please sign in to comment.