Skip to content

Commit

Permalink
feat(ui-form): adding support for labelHidden prop (#457)
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**
- Added a `labelHidden` property to TextInput and TextInputMask
components to toggle label visibility while ensuring accessibility.
- **Refactor**
- Removed unused code related to button and icon components in
TextInputMask.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
aversini authored Apr 1, 2024
1 parent 22d058a commit cb58eac
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/documentation/src/Form/TextInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default {
inputClassName: "",
className: "",
mode: "system",
labelHidden: false,
},
argTypes: {
mode: {
Expand Down
1 change: 1 addition & 0 deletions packages/documentation/src/Form/TextInputMask.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default {
error: false,
inputClassName: "",
className: "",
labelHidden: false,
},
argTypes: {
focusMode: {
Expand Down
3 changes: 2 additions & 1 deletion packages/ui-form/src/components/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const TextInput = React.forwardRef<HTMLInputElement, TextInputProps>(
noBorder = false,

labelId,
labelHidden = false,
type = "text",

helperText = "",
Expand Down Expand Up @@ -79,7 +80,7 @@ export const TextInput = React.forwardRef<HTMLInputElement, TextInputProps>(
!raw && { style: { paddingRight: inputPaddingRight } })}
{...extraProps}
/>
{!raw && (
{!raw && !labelHidden && (
<label
aria-hidden={true}
htmlFor={inputId}
Expand Down
14 changes: 2 additions & 12 deletions packages/ui-form/src/components/TextInput/TextInputMask.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { useMergeRefs } from "@versini/ui-hooks";
// import { IconHide, IconShow } from "@versini/ui-icons";
import { LiveRegion } from "@versini/ui-private";
import React, { useEffect, useRef, useState } from "react";

// import { ButtonIcon } from "..";
import { TextInput } from "./TextInput";
import type { TextInputMaskProps } from "./TextInputTypes";

Expand All @@ -26,6 +24,7 @@ export const TextInputMask = React.forwardRef<
name,
disabled,
label,
labelHidden,
onMaskChange,

onChange,
Expand Down Expand Up @@ -164,6 +163,7 @@ export const TextInputMask = React.forwardRef<
ref={mergedInputRef}
name={name}
label={label}
labelHidden={labelHidden}
type={masked ? "password" : "text"}
disabled={disabled}
spacing={spacing}
Expand All @@ -178,16 +178,6 @@ export const TextInputMask = React.forwardRef<

disabled: disabled,
})}
// rightElement={
// <ButtonIcon
// label={buttonLabel}
// onClick={handleShowMaskButtonClick}
// onBlur={handleTextInputMaskBlur}
// disabled={disabled}
// >
// {masked ? <IconShow /> : <IconHide />}
// </ButtonIcon>
// }
{...otherProps}
/>

Expand Down
4 changes: 4 additions & 0 deletions packages/ui-form/src/components/TextInput/TextInputTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export type CommonTextInputProps = {
* CSS class(es) to add to the actual textarea element.
*/
inputClassName?: string;
/**
* Hides the label visually but retain accessible relationship with the TextInput.
*/
labelHidden?: boolean;
/**
* Id to use for the TextInput label.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ describe("TextInput modifiers", () => {
expect(input.className).toContain("border-transparent");
});

it("should render a text input with a hidden label", async () => {
render(
<TextInput
label="hello world"
name="toto"
labelHidden
data-testid="txtnpt-1"
/>,
);
const label = await screen.findAllByText("hello world");
screen.debug(label);
expect(label.length).toBe(1);
});

it("should render a raw text input with no styling", async () => {
render(<TextInput label="toto" name="toto" raw data-testid="txtnpt-1" />);
const input = await screen.findByTestId("txtnpt-1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,25 @@ describe("TextInputMask modifiers", () => {
expect(input.className).toContain("border-transparent");
});

it("should render a text input with a hidden label", async () => {
render(
<TextInputMask
label="hello world"
name="toto"
labelHidden
data-testid="txtnpt-1"
rightElement={
<ButtonIcon>
<IconHide />
</ButtonIcon>
}
/>,
);
const label = await screen.findAllByText("hello world");
screen.debug(label);
expect(label.length).toBe(1);
});

it("should render a raw text input with no styling", async () => {
render(
<TextInputMask
Expand Down

0 comments on commit cb58eac

Please sign in to comment.