File tree Expand file tree Collapse file tree 5 files changed +62
-14
lines changed
Expand file tree Collapse file tree 5 files changed +62
-14
lines changed Original file line number Diff line number Diff line change 8585 }
8686}
8787
88- .stepper-vertical {
88+ .stepper-vertical :not ( .stepper-horizontal ) {
8989 .p-list__item {
9090 padding-bottom : 0 ;
9191 padding-top : 0 ;
Original file line number Diff line number Diff line change 1+ .switch-help-text {
2+ margin-left : 2.5rem ;
3+ }
Original file line number Diff line number Diff 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+ } ;
Original file line number Diff line number Diff line change 11import 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
55import 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} ) ;
Original file line number Diff line number Diff line change 11import React from "react" ;
2+ import classNames from "classnames" ;
23import type { HTMLProps , ReactNode } from "react" ;
3-
44import type { PropsWithSpread } from "types" ;
5+ import { useId } from "react" ;
6+ import "./Switch.scss" ;
57
68export 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<
2333export 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
You can’t perform that action at this time.
0 commit comments