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: Add 'onRightIconClick' prop on TextInput #1492

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ahsankhan26
Copy link

@ahsankhan26 ahsankhan26 commented Sep 23, 2024

Summary

This PR adds a prop on TextInput to support onClick for the right icon.
This allows users to easily create password inputs with the use of rightIcon prop.

Demo

password-input-example

Code Example

const PasswordInput = () => {
  const [showPass, setShowPass] = useState(false);

  return (
    <TextInput
      type={showPass ? "text" : "password"}
      rightIcon={showPass ? EyeOffIcon : EyeIcon}
      onRightIconClick={() => setShowPass(!showPass)}
    />
  );
};

Summary by CodeRabbit

  • New Features

    • Introduced a new optional prop onRightIconClick for the TextInput component, allowing users to define actions when the right icon is clicked.
  • Bug Fixes

    • Improved user experience by preventing clicks on the right icon when no action is defined, enhancing interaction reliability.
  • Style

    • Updated styling for the right icon in the TextInput, enhancing its visual integration and functionality.

Copy link

changeset-bot bot commented Sep 23, 2024

⚠️ No Changeset found

Latest commit: 061dc17

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

Copy link

vercel bot commented Sep 23, 2024

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

Name Status Preview Comments Updated (UTC)
flowbite-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 2, 2024 7:30am
flowbite-react-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 2, 2024 7:30am

Copy link
Contributor

coderabbitai bot commented Sep 23, 2024

Walkthrough

The changes introduce a new optional property, onRightIconClick, to the TextInputProps interface, allowing a callback function when the right icon is clicked. The TextInput component is updated to handle this prop, including an event handler for the right icon's container. Additionally, the styling for the right icon is modified to remove the pointer-events-none class, enabling interaction when the callback is defined.

Changes

Files Change Summary
packages/ui/src/components/TextInput/TextInput.tsx Added onRightIconClick prop to TextInputProps interface and updated the TextInput component to handle the new prop with an event handler for the right icon.
packages/ui/src/components/TextInput/theme.ts Modified the rightIcon class in the textInputTheme object by removing the pointer-events-none class, allowing interaction with the right icon.

Poem

In a field where icons play,
A click brings joy, come what may.
With a hop and a skip,
The right icon's tip,
Now dances with purpose, hooray! 🐇✨


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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 38913e5 and 5c1ffa7.

Files selected for processing (2)
  • packages/ui/src/components/TextInput/TextInput.tsx (3 hunks)
  • packages/ui/src/components/TextInput/theme.ts (1 hunks)
Additional comments not posted (4)
packages/ui/src/components/TextInput/theme.ts (1)

15-15: Removal of pointer-events-none enables click events on the right icon

The removal of the pointer-events-none class from the rightIcon.base property aligns with the PR objective of adding an onRightIconClick prop to the TextInput component. This change allows click events to be captured on the right icon, which is necessary for implementing features like toggling password visibility.

However, there are a few considerations:

  1. Ensure that this change is accompanied by appropriate updates in the component's logic to handle the click events.
  2. Consider adding a cursor style (e.g., cursor-pointer) to visually indicate that the icon is clickable.
  3. Verify that this change doesn't introduce any unintended side effects for existing uses of the TextInput component.

To ensure this change is properly implemented and doesn't cause regressions, please run the following verification script:

Verification successful

Conditional application of pointer-events-none is correctly implemented

The pointer-events-none class is now conditionally applied based on the presence of the onRightIconClick prop. This ensures that click events on the right icon are only enabled when an onRightIconClick handler is provided, aligning with the PR's objective.

No unintended uses of pointer-events-none remain when onRightIconClick is defined.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of onRightIconClick in TextInput component

# Test 1: Check if the TextInput component has been updated to handle onRightIconClick
echo "Checking TextInput component for onRightIconClick implementation:"
ast-grep --lang typescript --pattern 'interface TextInputProps {
  $$$
  onRightIconClick?: $_
  $$$
}'

# Test 2: Look for examples or tests demonstrating the use of onRightIconClick
echo "Searching for examples or tests using onRightIconClick:"
rg --type typescript "onRightIconClick" -C 5

# Test 3: Ensure no unintended uses of pointer-events-none remain for the right icon
echo "Checking for any remaining pointer-events-none on right icons:"
rg --type typescript "rightIcon.*pointer-events-none"

Length of output: 673


Script:

#!/bin/bash
# Description: Re-verify the implementation of onRightIconClick in TextInput component with corrected file types

# Test 1: Check if the TextInput component has been updated to handle onRightIconClick
echo "Checking TextInput component for onRightIconClick implementation:"
ast-grep --lang typescript --pattern 'interface TextInputProps {
  $$$
  onRightIconClick?: $_
  $$$
}'

# Test 2: Look for examples or tests demonstrating the use of onRightIconClick
echo "Searching for examples or tests using onRightIconClick:"
rg "onRightIconClick" -C 5 --type ts

# Test 3: Ensure no unintended uses of pointer-events-none remain for the right icon
echo "Checking for any remaining pointer-events-none on right icons:"
rg "rightIcon.*pointer-events-none" --type ts

Length of output: 3566

packages/ui/src/components/TextInput/TextInput.tsx (3)

49-49: LGTM: New prop onRightIconClick added correctly

The addition of the onRightIconClick prop to the TextInputProps interface is implemented correctly. It's defined as an optional function returning void, which is appropriate for a click event handler. This change aligns well with the PR objectives of enabling password visibility toggle functionality.


64-64: LGTM: onRightIconClick prop correctly destructured

The onRightIconClick prop is correctly added to the destructured props in the component. This makes the new prop available for use within the component, which is necessary for implementing the desired functionality.


Line range hint 1-114: Overall assessment: Well-implemented feature with minor suggestion

The implementation of the onRightIconClick prop in the TextInput component is well done and aligns perfectly with the PR objectives. The changes are clean, follow existing patterns, and improve the component's functionality without introducing any major issues.

Key points:

  1. The new prop is correctly added to the interface and component.
  2. The implementation in JSX is sound, with good UX considerations (disabling pointer events when no handler is provided).
  3. A small suggestion was made to add a null check for extra safety when calling onRightIconClick.

Great job on this feature implementation!

Copy link
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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 5c1ffa7 and 1965831.

📒 Files selected for processing (1)
  • packages/ui/src/components/TextInput/TextInput.tsx (3 hunks)
🔇 Additional comments (3)
packages/ui/src/components/TextInput/TextInput.tsx (3)

49-49: LGTM: New prop onRightIconClick added correctly

The addition of the onRightIconClick prop to the TextInputProps interface is well-implemented. It's correctly defined as an optional prop with the appropriate event type, which aligns with the PR objectives and enhances the component's functionality.


64-64: LGTM: onRightIconClick correctly destructured

The onRightIconClick prop is correctly destructured from the component's props, making it available for use within the component. This change is consistent with the addition of the new prop and follows the existing code structure.


Line range hint 1-114: Overall assessment: Good implementation with minor improvement suggested

The changes in this file successfully implement the new onRightIconClick functionality for the TextInput component. The new prop is correctly added to the interface, destructured in the component, and integrated into the rendering logic. The implementation aligns well with the PR objectives and follows existing code patterns.

A minor improvement was suggested for the onClick implementation to ensure consistent behavior and avoid unnecessary function creation. With this small change, the implementation will be even more robust and efficient.

Great job on enhancing the TextInput component's functionality!

Comment on lines 87 to 91
<div
data-testid="right-icon"
className={`${theme.field.rightIcon.base} ${!onRightIconClick ? "pointer-events-none" : ""}`}
onClick={(e) => onRightIconClick?.(e)}
>
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Suggestion: Improve onClick implementation

The changes to the right icon rendering logic look good overall. The addition of data-testid improves testability, and the conditional class for pointer events enhances UX. However, the onClick implementation could be further improved:

  1. Always pass the event object to onRightIconClick.
  2. Use optional chaining to ensure onRightIconClick exists before calling it.

Here's a suggested improvement:

- onClick={(e) => onRightIconClick?.(e)}
+ onClick={onRightIconClick ? (e) => onRightIconClick(e) : undefined}

This change ensures that:

  • The event object is always passed when onRightIconClick is called.
  • onRightIconClick is only set as the onClick handler if it's defined, avoiding unnecessary function creation.
📝 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
<div
data-testid="right-icon"
className={`${theme.field.rightIcon.base} ${!onRightIconClick ? "pointer-events-none" : ""}`}
onClick={(e) => onRightIconClick?.(e)}
>
<div
data-testid="right-icon"
className={`${theme.field.rightIcon.base} ${!onRightIconClick ? "pointer-events-none" : ""}`}
onClick={onRightIconClick ? (e) => onRightIconClick(e) : undefined}
>

Copy link
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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 1965831 and 061dc17.

📒 Files selected for processing (1)
  • packages/ui/src/components/TextInput/TextInput.tsx (3 hunks)
🔇 Additional comments (3)
packages/ui/src/components/TextInput/TextInput.tsx (3)

49-49: LGTM: New prop onRightIconClick correctly added

The new onRightIconClick prop is correctly defined as an optional function with the appropriate event type. This addition aligns well with the PR objectives and provides good type safety for users of the component.


64-64: LGTM: onRightIconClick correctly destructured

The onRightIconClick prop is correctly added to the destructured props, making it available for use within the component. Its placement among other similar props is appropriate.


Line range hint 1-118: Summary: Successfully implemented onRightIconClick with minor improvement suggested

The changes in this file successfully implement the new onRightIconClick functionality for the TextInput component, aligning well with the PR objectives. The new prop is correctly defined, destructured, and integrated into the component's logic. The addition of data-testid and the conditional class for pointer events are good improvements for testability and UX.

A minor improvement was suggested for the onClick implementation to enhance type safety and ensure proper event handling. Overall, the changes are well-implemented and ready for merging after addressing the suggested improvement.

Comment on lines +87 to +91
<div
data-testid="right-icon"
className={`${theme.field.rightIcon.base} ${!onRightIconClick ? "pointer-events-none" : ""}`}
onClick={onRightIconClick}
>
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve onClick implementation for better type safety and event handling

The changes to the right icon rendering logic look good overall. The addition of data-testid improves testability, and the conditional class for pointer events enhances UX. However, the onClick implementation can be further improved:

  1. Always pass the event object to onRightIconClick.
  2. Use a more type-safe approach to ensure onRightIconClick exists before setting it as the handler.

Here's a suggested improvement:

- onClick={onRightIconClick}
+ onClick={onRightIconClick ? (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => onRightIconClick(e) : undefined}

This change ensures that:

  • The event object is always passed when onRightIconClick is called.
  • onRightIconClick is only set as the onClick handler if it's defined, avoiding potential runtime errors.
  • The types are explicitly defined for better type safety.
📝 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
<div
data-testid="right-icon"
className={`${theme.field.rightIcon.base} ${!onRightIconClick ? "pointer-events-none" : ""}`}
onClick={onRightIconClick}
>
<div
data-testid="right-icon"
className={`${theme.field.rightIcon.base} ${!onRightIconClick ? "pointer-events-none" : ""}`}
onClick={onRightIconClick ? (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => onRightIconClick(e) : undefined}
>

@v0x12
Copy link

v0x12 commented Nov 14, 2024

Is there any reason why is this PR not accepted?

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.

3 participants