diff --git a/src/components/heading/heading.component.tsx b/src/components/heading/heading.component.tsx index 133ec8bd10..58ffa6bb8f 100644 --- a/src/components/heading/heading.component.tsx +++ b/src/components/heading/heading.component.tsx @@ -20,6 +20,7 @@ import { } from "./heading.style"; import useLocale from "../../hooks/__internal__/useLocale"; +export type HeadingType = "h1" | "h2" | "h3" | "h4" | "h5"; export interface HeadingProps extends MarginProps, TagProps { /** Child elements */ children?: React.ReactNode; @@ -31,6 +32,8 @@ export interface HeadingProps extends MarginProps, TagProps { subheader?: React.ReactNode; /** Defines the subtitle id for the heading. */ subtitleId?: string; + /** Defines the HTML heading element of the title. */ + headingType?: HeadingType; /** Defines the help text for the heading. */ help?: string; /** Defines the help link for the heading. */ @@ -66,6 +69,7 @@ export const Heading = ({ separator = false, subheader, subtitleId, + headingType = "h1", title, titleId, ...rest @@ -141,7 +145,7 @@ export const Heading = ({ diff --git a/src/components/heading/heading.spec.tsx b/src/components/heading/heading.spec.tsx index 68de7accc1..f9e07ab505 100644 --- a/src/components/heading/heading.spec.tsx +++ b/src/components/heading/heading.spec.tsx @@ -1,6 +1,6 @@ import React from "react"; import { shallow, mount, ReactWrapper } from "enzyme"; -import Heading from "."; +import Heading, { HeadingType } from "."; import { StyledHeader, StyledSubHeader, @@ -41,6 +41,19 @@ describe("Heading", () => { }); }); + describe("when headingType prop is provided", () => { + it.each(["h1", "h2", "h3", "h4", "h5"])( + "HTML heading element is correct when headingType is %s", + (headingType) => { + const wrapper = mount( + + ); + + expect(wrapper.find(headingType).text()).toBe("foo"); + } + ); + }); + describe("when the help prop is provided", () => { it("renders a help component", () => { const wrapper = mount( diff --git a/src/components/heading/heading.stories.mdx b/src/components/heading/heading.stories.mdx index 0d5d72ee65..07c7f829cc 100644 --- a/src/components/heading/heading.stories.mdx +++ b/src/components/heading/heading.stories.mdx @@ -42,6 +42,17 @@ import Heading from "carbon-react/lib/components/heading"; +### Heading with headingType + +The `headingType` prop sets the HTML heading element which is rendered on the page. It can take values `"h1"` up to `"h5"`. + + + + + + + + ### Heading without Divider diff --git a/src/components/heading/heading.test.js b/src/components/heading/heading.test.js index 51a798d958..dad444e6c3 100644 --- a/src/components/heading/heading.test.js +++ b/src/components/heading/heading.test.js @@ -152,6 +152,19 @@ context("Testing Heading component", () => { } ); + describe("when headingType prop is provided", () => { + it.each(["h1", "h2", "h3", "h4", "h5"])( + "should check HTML heading element is correct when headingType is %s", + (headingType) => { + CypressMountWithProviders( + + ); + + cy.get(headingType).contains("foo"); + } + ); + }); + describe("should render Heading component and check accessibility issues", () => { it("should check heading accessibility", () => { CypressMountWithProviders(); diff --git a/src/components/heading/index.ts b/src/components/heading/index.ts index 7fe192dc30..5ef54b939d 100755 --- a/src/components/heading/index.ts +++ b/src/components/heading/index.ts @@ -1,2 +1,2 @@ export { default } from "./heading.component"; -export type { HeadingProps } from "./heading.component"; +export type { HeadingProps, HeadingType } from "./heading.component"; diff --git a/src/components/settings-row/settings-row.component.js b/src/components/settings-row/settings-row.component.js index 2b57297dc6..a2e0aa5439 100644 --- a/src/components/settings-row/settings-row.component.js +++ b/src/components/settings-row/settings-row.component.js @@ -17,6 +17,7 @@ const marginPropTypes = filterStyledSystemMarginProps( const SettingsRow = ({ title, + headingType = "h4", description, children, className, @@ -28,6 +29,7 @@ const SettingsRow = ({ return ( { describe("render", () => { const name = "foobar-row"; const title = "Some Title"; + const description = Some descriptive text; const childId = "my_child"; const children = ; let wrapper = shallow( @@ -58,7 +59,6 @@ describe("SettingsRow", () => { }); describe("when description is provided", () => { - const description = Some descriptive text; let head; beforeEach(() => { @@ -77,6 +77,25 @@ describe("SettingsRow", () => { }); }); + describe("when headingType prop is provided", () => { + it.each(["h1", "h2", "h3", "h4", "h5"])( + "HTML heading element is correct when headingType is %s", + (headingType) => { + wrapper = mount( + + Content for settings + + ); + + expect(wrapper.find(headingType).text()).toBe(title); + } + ); + }); + describe("when title is not provided", () => { it("does not render a header", () => { wrapper = shallow( diff --git a/src/components/settings-row/settings-row.stories.mdx b/src/components/settings-row/settings-row.stories.mdx index 6d91c84993..df4ae739f9 100644 --- a/src/components/settings-row/settings-row.stories.mdx +++ b/src/components/settings-row/settings-row.stories.mdx @@ -44,6 +44,17 @@ All `children` are rendered in the input column to the right of the header colum +### with HeadingType + +The `headingType` prop sets the HTML heading element of the `title` within the component which is rendered on the page. It can take values `"h1"` up to `"h5"`. + + + + Content for settings + + + + ## Props ### Settings Row diff --git a/src/components/settings-row/settings-row.test.js b/src/components/settings-row/settings-row.test.js index e1df5a44a3..bf634f4f69 100644 --- a/src/components/settings-row/settings-row.test.js +++ b/src/components/settings-row/settings-row.test.js @@ -27,6 +27,21 @@ context("Tests for SettingsRow component", () => { } ); + it.each(["h1", "h2", "h3", "h4", "h5"])( + "should check HTML heading element is correct when headingType is %s", + (headingType) => { + CypressMountWithProviders( + + ); + + cy.get(headingType).contains("foo"); + } + ); + it.each(testData)( "should check %s as description for SettingsRow component", (description) => {