Skip to content

Commit 4791920

Browse files
authored
feat: add help text to switch component (#1202)
1 parent 42c4773 commit 4791920

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

src/components/Stepper/Step/Step.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
}
8686
}
8787

88-
.stepper-vertical {
88+
.stepper-vertical:not(.stepper-horizontal) {
8989
.p-list__item {
9090
padding-bottom: 0;
9191
padding-top: 0;

src/components/Switch/Switch.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.switch-help-text {
2+
margin-left: 2.5rem;
3+
}

src/components/Switch/Switch.stories.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@ export const Disabled: Story = {
2727
label: "Disabled switch",
2828
},
2929
};
30+
31+
export const HelpText: Story = {
32+
name: "Help Text",
33+
34+
args: {
35+
label: "Switch with help text",
36+
help: "This is some help text",
37+
},
38+
};

src/components/Switch/Switch.test.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { render } from "@testing-library/react";
2+
import { render, screen } from "@testing-library/react";
33
// import userEvent from "@testing-library/user-event";
44

55
import Switch from "./Switch";
@@ -32,4 +32,12 @@ describe("<Switch />", () => {
3232
"disabled",
3333
);
3434
});
35+
36+
it("renders a switch with help text", () => {
37+
render(
38+
<Switch label="Switch with help text" help="This is some help text" />,
39+
);
40+
41+
expect(screen.getByText("This is some help text")).toBeInTheDocument();
42+
});
3543
});

src/components/Switch/Switch.tsx

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React from "react";
2+
import classNames from "classnames";
23
import type { HTMLProps, ReactNode } from "react";
3-
44
import type { PropsWithSpread } from "types";
5+
import { useId } from "react";
6+
import "./Switch.scss";
57

68
export type Props = PropsWithSpread<
79
{
@@ -13,6 +15,14 @@ export type Props = PropsWithSpread<
1315
* Whether the switch is disabled or not
1416
*/
1517
disabled?: boolean;
18+
/**
19+
* Help text to show below the field.
20+
*/
21+
help?: ReactNode;
22+
/**
23+
* Optional class(es) to pass to the help text element.
24+
*/
25+
helpClassName?: string;
1626
},
1727
HTMLProps<HTMLInputElement>
1828
>;
@@ -23,20 +33,38 @@ export type Props = PropsWithSpread<
2333
export const Switch = ({
2434
label,
2535
disabled = false,
36+
help,
37+
helpClassName,
2638
...inputProps
2739
}: Props): React.JSX.Element => {
40+
const helpId = useId();
41+
2842
return (
29-
<label className="p-switch">
30-
<input
31-
type="checkbox"
32-
className="p-switch__input"
33-
role="switch"
34-
disabled={disabled}
35-
{...inputProps}
36-
></input>
37-
<span className="p-switch__slider"></span>
38-
<span className="p-switch__label">{label}</span>
39-
</label>
43+
<>
44+
<label className="p-switch">
45+
<input
46+
type="checkbox"
47+
className="p-switch__input"
48+
role="switch"
49+
disabled={disabled}
50+
{...inputProps}
51+
></input>
52+
<span className="p-switch__slider"></span>
53+
<span className="p-switch__label">{label}</span>
54+
</label>
55+
{help && (
56+
<p
57+
className={classNames(
58+
"p-form-help-text",
59+
"switch-help-text",
60+
helpClassName,
61+
)}
62+
id={helpId}
63+
>
64+
{help}
65+
</p>
66+
)}
67+
</>
4068
);
4169
};
4270

0 commit comments

Comments
 (0)