Skip to content
Closed
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
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 @@ -82,6 +82,10 @@ export interface Props<T extends HTMLInputElement | HTMLTextAreaElement = HTMLIn
* React aria onChange event.
*/
onValueChange?: (value: string) => void;
/**
* disable the number chnage on scrolling
*/
disableScrollChange?: boolean;
}

type AutoCapitalize = AriaTextFieldOptions<"input">["autoCapitalize"];
Expand Down Expand Up @@ -116,6 +120,7 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
validationBehavior = formValidationBehavior ?? globalContext?.validationBehavior ?? "native",
innerWrapperRef: innerWrapperRefProp,
onValueChange = () => {},
disableScrollChange,
...otherProps
} = props;

Expand All @@ -125,6 +130,11 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
},
[onValueChange],
);
const handleScrollingChange = (e: React.WheelEvent<HTMLInputElement>) => {
if (disableScrollChange) {
(e.target as HTMLInputElement).blur();
}
};
Comment on lines +133 to +137
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance scroll handler implementation

The current implementation can be improved for better performance and consistency:

  1. Only apply the behavior to number inputs
  2. Respect readonly and disabled states
  3. Add type safety
-  const handleScrollingChange = (e: React.WheelEvent<HTMLInputElement>) => {
-    if (disableScrollChange) {
-      (e.target as HTMLInputElement).blur();
-    }
-  };
+  const handleScrollingChange = useCallback((e: React.WheelEvent<HTMLInputElement>) => {
+    if (!disableScrollChange || type !== "number" || originalProps.isReadOnly || originalProps.isDisabled) {
+      return;
+    }
+    const input = e.currentTarget;
+    input.blur();
+  }, [disableScrollChange, type, originalProps.isReadOnly, originalProps.isDisabled]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const handleScrollingChange = (e: React.WheelEvent<HTMLInputElement>) => {
if (disableScrollChange) {
(e.target as HTMLInputElement).blur();
}
};
const handleScrollingChange = useCallback((e: React.WheelEvent<HTMLInputElement>) => {
if (!disableScrollChange || type !== "number" || originalProps.isReadOnly || originalProps.isDisabled) {
return;
}
const input = e.currentTarget;
input.blur();
}, [disableScrollChange, type, originalProps.isReadOnly, originalProps.isDisabled]);


const [isFocusWithin, setFocusWithin] = useState(false);

Expand Down Expand Up @@ -386,6 +396,7 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
"aria-readonly": dataAttr(originalProps.isReadOnly),
onChange: chain(inputProps.onChange, onChange),
ref: domRef,
onWheel: handleScrollingChange,
};
},
[
Expand All @@ -402,6 +413,7 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
originalProps.isReadOnly,
originalProps.isRequired,
onChange,
disableScrollChange,
],
);

Expand Down