Skip to content

Commit

Permalink
Merge pull request #761 from petermakowski/allow-custom-help-text-class
Browse files Browse the repository at this point in the history
feat: allow custom help text class
  • Loading branch information
petermakowski authored Apr 20, 2022
2 parents e3f24ad + bfb3949 commit 15f5a5b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/components/Field/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export type Props = {
* Help text to show below the field.
*/
help?: ReactNode;
/**
* Optional class(es) to pass to the help text element.
*/
helpClassName?: ReactNode;
/**
* An id to give to the help element.
*/
Expand Down Expand Up @@ -80,11 +84,12 @@ export type Props = {
const generateHelpText = ({
help,
helpId,
helpClassName,
isTickElement,
}: Pick<Props, "help" | "helpId" | "isTickElement">) =>
}: Pick<Props, "help" | "helpId" | "helpClassName" | "isTickElement">) =>
help ? (
<p
className={classNames("p-form-help-text", {
className={classNames("p-form-help-text", helpClassName, {
"is-tick-element": isTickElement,
})}
id={helpId}
Expand Down Expand Up @@ -138,6 +143,7 @@ const generateContent = ({
labelFirst,
labelNode,
help,
helpClassName,
error,
caution,
success,
Expand All @@ -159,6 +165,7 @@ const generateContent = ({
{generateHelpText({
helpId,
help,
helpClassName,
isTickElement,
})}
{generateError(error, caution, success, validationId)}
Expand All @@ -172,6 +179,7 @@ const Field = ({
error,
forId,
help,
helpClassName,
helpId,
isSelect,
isTickElement,
Expand All @@ -198,6 +206,7 @@ const Field = ({
labelFirst,
labelNode,
help,
helpClassName,
error,
caution,
success,
Expand Down
6 changes: 6 additions & 0 deletions src/components/Input/Input.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import Input from "./Input";
type: "text",
},
},
helpClassName: {
control: {
type: "text",
},
},
label: {
control: {
type: "text",
Expand Down Expand Up @@ -67,6 +72,7 @@ An input field where the user can enter data, which can vary in many ways, depen
placeholder: "[email protected]",
label: "Email address",
help: "Additional description for the field",
helpClassName: "u-no-margin--bottom",
}}
>
{Template.bind({})}
Expand Down
26 changes: 19 additions & 7 deletions src/components/Input/Input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ import React from "react";
import Input from "./Input";

describe("Input", () => {
it("can add additional classes", () => {
const wrapper = shallow(<Input type="text" className="extra-class" />);
const className = wrapper.find("input").prop("className");
expect(className.includes("p-form-validation__input")).toBe(true);
expect(className.includes("extra-class")).toBe(true);
});

it("moves the label for radio buttons", () => {
const wrapper = shallow(<Input type="radio" />);
expect(wrapper.prop("label")).toBe("");
Expand Down Expand Up @@ -112,4 +105,23 @@ describe("Input RTL", () => {
render(<Input help={help} type="checkbox" />);
expect(screen.getByRole("checkbox")).toHaveAccessibleDescription(help);
});

it("can add additional classes", () => {
const { container } = render(
<Input
type="text"
className="extra-class"
help="additional description"
helpClassName="additional-help-text-class"
/>
);
expect(screen.getByRole("textbox")).toHaveClass(
"p-form-validation__input",
"extra-class"
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.querySelector(".p-form-help-text")).toHaveClass(
"additional-help-text-class"
);
});
});
6 changes: 6 additions & 0 deletions src/components/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export type Props = PropsWithSpread<
* Help text to show below the field.
*/
help?: ReactNode;
/**
* Optional class(es) to pass to the help text element.
*/
helpClassName?: ReactNode;
/**
* The id of the input.
*/
Expand Down Expand Up @@ -71,6 +75,7 @@ const Input = ({
className,
error,
help,
helpClassName,
id,
label,
labelClassName,
Expand Down Expand Up @@ -138,6 +143,7 @@ const Input = ({
error={error}
forId={id}
help={help}
helpClassName={helpClassName}
helpId={helpId}
isTickElement={type === "checkbox" || type === "radio"}
label={fieldLabel}
Expand Down

0 comments on commit 15f5a5b

Please sign in to comment.