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 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
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={onRightIconClick}
>
Comment on lines +87 to +91
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}
>

<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