Skip to content

Commit

Permalink
feat(TextInput): adding new prop: rightElementClassName (#710)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced a new prop, `rightElementClassName`, for enhanced styling
of the right element in the TextInput component.
- **Bug Fixes**
- Adjusted padding logic to accommodate the width of the right element,
ensuring proper layout.
- **Tests**
- Added new test cases for rendering with custom classes and updated
existing tests for clarity and coverage.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
aversini authored Sep 30, 2024
1 parent d65d57b commit 0374ebe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/ui-textinput/src/components/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const TextInput = React.forwardRef<HTMLInputElement, TextInputProps>(
helperText = "",

rightElement,
rightElementClassName,
spacing,
size = "md",

Expand Down Expand Up @@ -62,6 +63,7 @@ export const TextInput = React.forwardRef<HTMLInputElement, TextInputProps>(
spacing,
mode,
size,
rightElementClassName,
});

/* c8 ignore start - ResizeObserver is tough to test... */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export type TextInputProps = {
* elements, such a Button.
*/
rightElement?: React.ReactElement;
/**
* Extra classes to add to the right element container.
*/
rightElementClassName?: string;
} & CommonTextInputProps;

export type TextInputMaskProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ describe("TextInput modifiers", () => {
}
});

it("should render a text input with an input class", async () => {
it("should render a text input with a custom input class", async () => {
render(
<TextInput
label="toto"
Expand All @@ -260,6 +260,20 @@ describe("TextInput modifiers", () => {
await screen.findByText("right element");
});

it("should render a text input with a right element custom class", async () => {
render(
<TextInput
label="toto"
name="toto"
rightElementClassName="toto"
rightElement={<div data-testid="txtnpt-1">right element</div>}
/>,
);
const rightEl = (await screen.findByTestId("txtnpt-1")).parentElement;
expect(rightEl?.className).toContain("toto");
expect(rightEl?.className).toContain("absolute");
});

it("should render a disabled text input", async () => {
render(<TextInput label="hello world" name="toto" disabled />);
const label = await screen.findAllByText("hello world");
Expand Down
6 changes: 5 additions & 1 deletion packages/ui-textinput/src/components/TextInput/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type getTextInputClassesProps = {
size: Size;

className?: string;
rightElementClassName?: string;
inputClassName?: string;
} & SpacingProps;

Expand Down Expand Up @@ -154,6 +155,7 @@ export const getTextInputClasses = ({
mode,
focusMode,
size,
rightElementClassName,
}: getTextInputClassesProps) => {
const wrapper = raw
? className
Expand Down Expand Up @@ -215,7 +217,9 @@ export const getTextInputClasses = ({
disabled,
});

const rightElement = raw ? undefined : "absolute right-3";
const rightElement = raw
? undefined
: clsx("absolute right-3", rightElementClassName);

return {
wrapper,
Expand Down

0 comments on commit 0374ebe

Please sign in to comment.