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

introduce: [#436] add RightIcon #437

Merged
merged 5 commits into from
Dec 22, 2022
Merged
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
33 changes: 31 additions & 2 deletions src/docs/pages/FormsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,42 @@ const FormsPage: FC = () => {
),
},
{
title: 'Input element with icon',
title: 'Input element with icon on the left side',
code: (
<div>
<div className="mb-2 block">
<Label htmlFor="email4" value="Your email" />
</div>
<TextInput id="email4" type="email" placeholder="name@flowbite.com" required icon={HiMail} />
<TextInput id="email4" type="email" icon={HiMail} placeholder="name@flowbite.com" required />
</div>
),
},
{
title: 'Input element with icon on the right side',
code: (
<div>
<div className="mb-2 block">
<Label htmlFor="email4" value="Your email" />
</div>
<TextInput id="email4" type="email" rightIcon={HiMail} placeholder="name@flowbite.com" required />
</div>
),
},
{
title: 'Input element with icon on both sides',
code: (
<div>
<div className="mb-2 block">
<Label htmlFor="email4" value="Your email" />
</div>
<TextInput
id="email4"
type="email"
icon={HiMail}
rightIcon={HiMail}
placeholder="name@flowbite.com"
required
/>
</div>
),
},
Expand Down
6 changes: 6 additions & 0 deletions src/lib/components/TextInput/TextInput.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render } from '@testing-library/react';
import { HiEye } from 'react-icons/hi';
import { describe, expect, it } from 'vitest';
import { TextInput } from './TextInput';

Expand All @@ -9,5 +10,10 @@ describe.concurrent('Components / Text input', () => {

expect(textInput).toBeInTheDocument();
});
it('should have Icon if selected ', () => {
const page = render(<TextInput rightIcon={HiEye} />).getByTestId('right-icon');

expect(page).toBeInTheDocument();
});
});
});
12 changes: 12 additions & 0 deletions src/lib/components/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ export interface FlowbiteTextInputTheme {
base: string;
svg: string;
};
rightIcon: {
base: string;
svg: string;
};
input: {
base: string;
sizes: TextInputSizes;
colors: TextInputColors;
withIcon: FlowbiteBoolean;
withRightIcon: FlowbiteBoolean;
withAddon: FlowbiteBoolean;
withShadow: FlowbiteBoolean;
};
Expand All @@ -41,6 +46,7 @@ export interface TextInputProps extends Omit<ComponentProps<'input'>, 'ref' | 'c
helperText?: ReactNode;
addon?: ReactNode;
icon?: FC<ComponentProps<'svg'>>;
rightIcon?: FC<ComponentProps<'svg'>>;
color?: keyof TextInputColors;
theme?: DeepPartial<FlowbiteTextInputTheme>;
}
Expand All @@ -53,6 +59,7 @@ export const TextInput = forwardRef<HTMLInputElement, TextInputProps>(
helperText,
addon,
icon: Icon,
rightIcon: RightIcon
color = 'gray',
className,
theme: customTheme = {},
Expand All @@ -72,6 +79,11 @@ export const TextInput = forwardRef<HTMLInputElement, TextInputProps>(
<Icon className={theme.field.icon.svg} />
</div>
)}
{RightIcon && (
<div data-testid="right-icon" className={theme.field.rightIcon.base}>
<RightIcon className={theme.field.rightIcon.svg} />
</div>
)}
<input
className={classNames(
theme.field.input.base,
Expand Down
8 changes: 8 additions & 0 deletions src/lib/theme/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ const theme: FlowbiteTheme = {
base: 'pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3',
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',
svg: 'h-5 w-5 text-gray-500 dark:text-gray-400',
},
input: {
base: 'block w-full border disabled:cursor-not-allowed disabled:opacity-50',
sizes: {
Expand All @@ -473,6 +477,10 @@ const theme: FlowbiteTheme = {
success:
'border-green-500 bg-green-50 text-green-900 placeholder-green-700 focus:border-green-500 focus:ring-green-500 dark:border-green-400 dark:bg-green-100 dark:focus:border-green-500 dark:focus:ring-green-500',
},
withRightIcon: {
on: 'pr-10',
off: '',
},
withIcon: {
on: 'pl-10',
off: '',
Expand Down