Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to pass through heading props #1895

Merged
merged 7 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,26 @@ describe('StepIndicator component', () => {
expect(stepIndicator).toBeInTheDocument()
expect(getByRole('heading', { level: 4 })).toBeInTheDocument()
})

it('allows props to be passed through to the heading element', () => {
const { queryByRole, queryByTestId } = render(
<StepIndicator
headingProps={{ id: 'my-id', className: 'my-custom-className' }}
>
<StepIndicatorStep label={step1} status="complete" />
<StepIndicatorStep label={step2} status="current" />
<StepIndicatorStep label={step3} status="incomplete" />
</StepIndicator>
)

const stepIndicator = queryByTestId('step-indicator')
const heading = queryByRole('heading', { level: 4 })

expect(stepIndicator).toBeInTheDocument()
expect(heading).toBeInTheDocument()
expect(heading).toHaveAttribute('id', 'my-id')
expect(heading).toHaveClass(
'usa-step-indicator__heading my-custom-className'
)
})
})
37 changes: 31 additions & 6 deletions src/components/stepindicator/StepIndicator/StepIndicator.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import classnames from 'classnames'
import { StepIndicatorStepProps } from '../StepIndicatorStep/StepIndicatorStep'
import classNames from 'classnames'
brandonlenz marked this conversation as resolved.
Show resolved Hide resolved

interface StepIndicatorProps {
showLabels?: boolean
Expand All @@ -10,6 +11,10 @@ interface StepIndicatorProps {
className?: string
divProps?: JSX.IntrinsicElements['div']
listProps?: JSX.IntrinsicElements['ol']
headingProps?: React.DetailedHTMLProps<
React.HTMLAttributes<HTMLHeadingElement>,
HTMLHeadingElement
>
headingLevel?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'
}
export const StepIndicator = (
Expand All @@ -23,20 +28,39 @@ export const StepIndicator = (
className,
divProps,
listProps,
headingProps,
headingLevel = 'h4',
} = props

const Heading = headingLevel

const classes = classnames(
const { className: additionalDivClasses, ...remainingDivProps } =
divProps || {}
const { className: additionalListClasses, ...remainingListProps } =
listProps || {}
const { className: additionalHeadingClasses, ...remainingHeadingProps } =
headingProps || {}

const divClasses = classnames(
'usa-step-indicator',
{
'usa-step-indicator--no-labels': !showLabels,
'usa-step-indicator--counters': counters === 'default',
'usa-step-indicator--counters-sm': counters === 'small',
'usa-step-indicator--center': centered,
},
className
className,
additionalDivClasses
)

const listClasses = classnames(
'usa-step-indicator__segments',
additionalListClasses
)

const headingClasses = classNames(
'usa-step-indicator__heading',
additionalHeadingClasses
)

const findCurrentStepIndex = (): number => {
Expand All @@ -50,15 +74,16 @@ export const StepIndicator = (

return (
<div
className={classes}
className={divClasses}
data-testid="step-indicator"
aria-label="progress"
{...divProps}>
<ol className="usa-step-indicator__segments" {...listProps}>
{...remainingDivProps}
>
<ol className={listClasses} {...remainingListProps}>
{children}
</ol>
<div className="usa-step-indicator__header">
<Heading className="usa-step-indicator__heading">
<Heading className={headingClasses} {...remainingHeadingProps}>
<span className="usa-step-indicator__heading-counter">
<span className="usa-sr-only">Step</span>
<span className="usa-step-indicator__current-step">
Expand Down