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
Open
Show file tree
Hide file tree
Changes from 4 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
8 changes: 7 additions & 1 deletion packages/ui/src/components/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface TextInputProps extends Omit<ComponentProps<"input">, "ref" | "c
color?: DynamicStringEnumKeysOf<FlowbiteTextInputColors>;
helperText?: ReactNode;
icon?: FC<ComponentProps<"svg">>;
onRightIconClick?: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
rightIcon?: FC<ComponentProps<"svg">>;
shadow?: boolean;
sizing?: DynamicStringEnumKeysOf<FlowbiteTextInputSizes>;
Expand All @@ -60,6 +61,7 @@ export const TextInput = forwardRef<HTMLInputElement, TextInputProps>(
color = "gray",
helperText,
icon: Icon,
onRightIconClick,
rightIcon: RightIcon,
shadow,
sizing = "md",
Expand All @@ -82,7 +84,11 @@ export const TextInput = forwardRef<HTMLInputElement, TextInputProps>(
</div>
)}
{RightIcon && (
<div data-testid="right-icon" className={theme.field.rightIcon.base}>
<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}
>

<RightIcon className={theme.field.rightIcon.svg} />
</div>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/TextInput/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const textInputTheme: FlowbiteTextInputTheme = createTheme({
svg: "h-5 w-5 text-gray-500 dark:text-gray-400",
},
rightIcon: {
base: "pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3",
base: "absolute inset-y-0 right-0 flex items-center pr-3",
svg: "h-5 w-5 text-gray-500 dark:text-gray-400",
},
input: {
Expand Down
Loading