Skip to content

fix: disable number change in case of scrolling#4257

Closed
ankit-ml12 wants to merge 1 commit into
heroui-inc:canaryfrom
ankit-ml12:feat/disable-scrolling-prop
Closed

fix: disable number change in case of scrolling#4257
ankit-ml12 wants to merge 1 commit into
heroui-inc:canaryfrom
ankit-ml12:feat/disable-scrolling-prop

Conversation

@ankit-ml12
Copy link
Copy Markdown

@ankit-ml12 ankit-ml12 commented Dec 7, 2024

Closes #4238

📝 Description

added a prop disableScrollChange to disable change in the input when the type is number

⛳️ Current behavior (updates)

disable change in number on scrolling with mouse wheel or touchpad, since it can change value without user noticing when in scrollable container

🚀 New behavior

prop added to disable if needed

💣 Is this a breaking change (Yes/No):

No

📝 Additional Information

Summary by CodeRabbit

  • New Features
    • Introduced an optional property disableScrollChange to enhance input control during scrolling.
    • Added functionality to prevent value changes when the input is scrolled, improving user experience.

@vercel
Copy link
Copy Markdown

vercel Bot commented Dec 7, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nextui-docs-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 7, 2024 6:25am
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 7, 2024 6:25am

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Dec 7, 2024

⚠️ No Changeset found

Latest commit: 7bb0ad8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link
Copy Markdown

vercel Bot commented Dec 7, 2024

@Ankit1ml is attempting to deploy a commit to the NextUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Dec 7, 2024

Walkthrough

The pull request introduces a new optional property, disableScrollChange, to the Props interface in the useInput function. This property aims to prevent value changes during scroll events. A new function, handleScrollingChange, is added to manage scroll events, which blurs the input when scrolling occurs if disableScrollChange is true. The getInputProps function is updated to incorporate the onWheel event handler, linking it to handleScrollingChange. These modifications enhance input behavior control during scrolling.

Changes

File Path Change Summary
packages/components/input/src/use-input.ts Added optional property disableScrollChange to Props interface, added handleScrollingChange function, and updated getInputProps to include onWheel event handler.

Possibly related PRs

Suggested reviewers

  • jrgarciadev
  • wingkwong

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
packages/components/input/src/use-input.ts (2)

85-88: Improve JSDoc documentation for the new property

The documentation for disableScrollChange needs enhancement:

  1. Fix the typo in "chnage"
  2. Add type information
  3. Include example usage
  4. Document default value
  /**
-  * disable  the number chnage on scrolling
+  * Disables value changes triggered by mouse wheel or touchpad scrolling when the input type is "number".
+  * This is useful when the input is placed within a scrollable container to prevent accidental value modifications.
+  * @default false
+  * @example
+  * ```tsx
+  * <Input type="number" disableScrollChange />
+  * ```
   */
  disableScrollChange?: boolean;

399-399: Optimize wheel event handler attachment

The wheel event handler should only be attached when necessary to optimize performance:

-        onWheel: handleScrollingChange,
+        ...(type === "number" ? { onWheel: handleScrollingChange } : {}),

This change ensures the wheel event handler is only attached to number inputs, reducing unnecessary event listeners and potential re-renders.

Also applies to: 416-416

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ac94e47 and 7bb0ad8.

📒 Files selected for processing (1)
  • packages/components/input/src/use-input.ts (5 hunks)
🔇 Additional comments (1)
packages/components/input/src/use-input.ts (1)

85-88: Add test coverage for the new functionality

To ensure reliability of the disableScrollChange feature, consider adding the following test cases:

  1. Verify that scrolling is disabled when the prop is true
  2. Verify that scrolling still works when the prop is false
  3. Verify behavior with readonly/disabled states
  4. Verify that non-number inputs are unaffected

Comment on lines +133 to +137
const handleScrollingChange = (e: React.WheelEvent<HTMLInputElement>) => {
if (disableScrollChange) {
(e.target as HTMLInputElement).blur();
}
};
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]);

@wingkwong
Copy link
Copy Markdown
Member

  1. please don't work on assigned issue
  2. we plan to have NumberField so we shouldn't add a new prop to input.

@wingkwong wingkwong closed this Dec 7, 2024
@ankit-ml12
Copy link
Copy Markdown
Author

Ok thanks @wingkwong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Number Input props

3 participants