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(use-input): sync the inputValue with domRef.current.value when hovering the input #3481

Closed
wants to merge 12 commits into from
5 changes: 5 additions & 0 deletions .changeset/angry-icons-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/input": patch
---

sync the inputValue with domRef.current.value when hovering the input
17 changes: 17 additions & 0 deletions packages/components/input/__tests__/input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ describe("Input", () => {

expect(inputs[1]).toBeVisible();
});

it("should sync ref.current.value with input's value after hovering over the input", async () => {
const ref = React.createRef<HTMLInputElement>();

const {container} = render(<Input ref={ref} value="value" />);
const inputBase = container.querySelector("[data-slot='base']");
const input = inputBase && inputBase.querySelector("input");
jijiseong marked this conversation as resolved.
Show resolved Hide resolved
const user = userEvent.setup();

if (!input) throw new Error("input is null");
if (!ref.current) throw new Error("ref is null");

ref.current.value = "new value";
await user.hover(inputBase);

expect(input).toHaveValue("new value");
});
jijiseong marked this conversation as resolved.
Show resolved Hide resolved
});

describe("Input with React Hook Form", () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/components/input/src/use-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,13 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
isTextInput: true,
});

const {isHovered, hoverProps} = useHover({isDisabled: !!originalProps?.isDisabled});
const {isHovered, hoverProps} = useHover({
isDisabled: !!originalProps?.isDisabled,
onHoverStart: () => {
if (!domRef.current) return;
setInputValue(domRef.current.value);
},
});

const {focusProps: clearFocusProps, isFocusVisible: isClearButtonFocusVisible} = useFocusRing();

Expand Down