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

feat: NumberInput support long presses #735

Open
wants to merge 2 commits into
base: main
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
52 changes: 42 additions & 10 deletions src/components/input-elements/NumberInput/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const baseArrowClasses =
const enabledArrowClasses =
"cursor-pointer hover:text-tremor-content dark:hover:text-dark-tremor-content";

const stepDelay = 600;
const stepInterval = 100;

const NumberInput = React.forwardRef<HTMLInputElement, NumberInputProps>((props, ref) => {
const { onSubmit, enableStepper = true, disabled, onValueChange, onChange, ...other } = props;

Expand All @@ -38,6 +41,32 @@ const NumberInput = React.forwardRef<HTMLInputElement, NumberInputProps>((props,
setIsArrowUpPressed(false);
}, []);

// for long press to step up/down
const stepTimeoutRef = useRef<number | null>(null);
const onStopStepping = () => {
stepTimeoutRef && clearTimeout(stepTimeoutRef.current!);
};
const onStepMouseDown = React.useCallback((e: React.MouseEvent, up: boolean) => {
e.preventDefault();
onStopStepping();

up ? inputRef.current?.stepUp() : inputRef.current?.stepDown();
inputRef.current?.dispatchEvent(new Event("input", { bubbles: true }));

function step() {
up ? inputRef.current?.stepUp() : inputRef.current?.stepDown();
inputRef.current?.dispatchEvent(new Event("input", { bubbles: true }));
stepTimeoutRef.current = setTimeout(step, stepInterval) as unknown as number;
}
stepTimeoutRef.current = setTimeout(step, stepDelay) as unknown as number;
}, []);

React.useEffect(() => {
return () => {
onStopStepping();
};
}, []);

return (
<BaseInput
type="number"
Expand All @@ -46,6 +75,7 @@ const NumberInput = React.forwardRef<HTMLInputElement, NumberInputProps>((props,
makeInputClassName={makeClassName("NumberInput")}
onKeyDown={(e) => {
if (e.key === "Enter" && !e.ctrlKey && !e.altKey && !e.shiftKey) {
onStopStepping();
const value = inputRef.current?.value;
onSubmit?.(parseFloat(value ?? ""));
}
Expand Down Expand Up @@ -76,13 +106,14 @@ const NumberInput = React.forwardRef<HTMLInputElement, NumberInputProps>((props,
<div
tabIndex={-1}
onClick={(e) => e.preventDefault()}
onMouseDown={(e) => e.preventDefault()}
onTouchStart={(e) => e.preventDefault()}
onMouseUp={() => {
onMouseDown={(e) => {
e.preventDefault();
if (disabled) return;
inputRef.current?.stepDown();
inputRef.current?.dispatchEvent(new Event("input", { bubbles: true }));
onStepMouseDown(e, false);
}}
onTouchStart={(e) => e.preventDefault()}
onMouseUp={() => onStopStepping()}
onMouseLeave={() => onStopStepping()}
className={tremorTwMerge(
!disabled && enabledArrowClasses,
baseArrowClasses,
Expand All @@ -99,13 +130,14 @@ const NumberInput = React.forwardRef<HTMLInputElement, NumberInputProps>((props,
<div
tabIndex={-1}
onClick={(e) => e.preventDefault()}
onMouseDown={(e) => e.preventDefault()}
onTouchStart={(e) => e.preventDefault()}
onMouseUp={() => {
onMouseDown={(e) => {
e.preventDefault();
if (disabled) return;
inputRef.current?.stepUp();
inputRef.current?.dispatchEvent(new Event("input", { bubbles: true }));
onStepMouseDown(e, true);
}}
onTouchStart={(e) => e.preventDefault()}
onMouseUp={() => onStopStepping()}
onMouseLeave={() => onStopStepping()}
className={tremorTwMerge(
!disabled && enabledArrowClasses,
baseArrowClasses,
Expand Down
8 changes: 4 additions & 4 deletions src/tests/input-elements/NumberInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("NumberInput", () => {
const inputEl: HTMLInputElement = screen.getByTestId("base-input");
expect(inputEl.value).toBe("2");
const stepUp = screen.getByTestId("step-up");
fireEvent.mouseUp(stepUp);
fireEvent.mouseDown(stepUp);
expect(inputEl.value).toBe("2.1");
});

Expand All @@ -30,10 +30,10 @@ describe("NumberInput", () => {
const inputEl: HTMLInputElement = screen.getByTestId("base-input");
const stepUp = screen.getByTestId("step-up");
const stepDown = screen.getByTestId("step-down");
fireEvent.mouseUp(stepDown);
fireEvent.mouseDown(stepDown);
expect(inputEl.value).toBe("1");
fireEvent.mouseUp(stepUp);
fireEvent.mouseUp(stepUp);
fireEvent.mouseDown(stepUp);
fireEvent.mouseDown(stepUp);
expect(inputEl.value).toBe("2");
});
});
Loading