-
Notifications
You must be signed in to change notification settings - Fork 10
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(stepper): added stepper component #1500
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
packages/react-components/src/components/Stepper/Stepper.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
$base-class: 'stepper'; | ||
|
||
.#{$base-class} { | ||
&__counter { | ||
margin-bottom: var(--spacing-2); | ||
color: var(--content-basic-disabled); | ||
font-weight: var(--font-weight-medium); | ||
} | ||
|
||
&__steps__container { | ||
display: flex; | ||
flex-flow: row nowrap; | ||
} | ||
|
||
&__step { | ||
margin-right: var(--spacing-1); | ||
border-radius: var(--radius-1); | ||
width: 22px; | ||
height: 4px; | ||
|
||
&--completed { | ||
background-color: var(--content-basic-primary); | ||
} | ||
|
||
&--active { | ||
background-color: var(--surface-tertiary-hover); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/react-components/src/components/Stepper/Stepper.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
import { Stepper } from './Stepper'; | ||
|
||
describe('Stepper', () => { | ||
it('displays correct number of steps', () => { | ||
render(<Stepper activeStep={2} steps={4} />); | ||
|
||
const stepsContainer = screen.getByTestId('stepper-container'); | ||
|
||
expect(stepsContainer?.children.length).toBe(4); | ||
}); | ||
|
||
it('marks steps as completed and active correctly', () => { | ||
render(<Stepper activeStep={2} steps={3} />); | ||
|
||
const steps = screen.getByTestId('stepper-container').children; | ||
|
||
expect(steps[0].className).toContain('completed'); | ||
expect(steps[1].className).toContain('completed'); | ||
expect(steps[2].className).toContain('active'); | ||
dprzybylek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
it('accepts and applies custom className', () => { | ||
const customClass = 'custom-class'; | ||
render(<Stepper activeStep={1} steps={3} className={customClass} />); | ||
|
||
expect(screen.getByTestId('stepper')).toHaveClass(customClass); | ||
}); | ||
it('passes additional props to the main div', () => { | ||
render(<Stepper activeStep={1} steps={3} aria-label="progress" />); | ||
|
||
expect(screen.getByTestId('stepper')).toHaveAttribute( | ||
'aria-label', | ||
'progress' | ||
); | ||
}); | ||
describe('boundary values and normalization', () => { | ||
it('should set minimum steps value to 1 when negative value is provided', () => { | ||
render(<Stepper steps={-5} activeStep={1} />); | ||
|
||
expect(screen.getByTestId('stepper')).toHaveTextContent('1/1'); | ||
}); | ||
|
||
it('should set minimum steps value to 1 when zero is provided', () => { | ||
render(<Stepper steps={0} activeStep={1} />); | ||
|
||
expect(screen.getByTestId('stepper')).toHaveTextContent('1/1'); | ||
}); | ||
|
||
it('should normalize activeStep to 1 when negative value is provided', () => { | ||
render(<Stepper steps={5} activeStep={-3} />); | ||
|
||
expect(screen.getByTestId('stepper')).toHaveTextContent('1/5'); | ||
}); | ||
|
||
it('should normalize activeStep to steps value when exceeding number of steps', () => { | ||
render(<Stepper steps={3} activeStep={10} />); | ||
|
||
expect(screen.getByTestId('stepper')).toHaveTextContent('3/3'); | ||
}); | ||
|
||
it('should properly normalize both steps and activeStep', () => { | ||
render(<Stepper steps={-2} activeStep={-3} />); | ||
|
||
expect(screen.getByTestId('stepper')).toHaveTextContent('1/1'); | ||
}); | ||
|
||
it('should normalize activeStep to steps value when activeStep is a float', () => { | ||
render(<Stepper steps={5} activeStep={1.5} />); | ||
|
||
expect(screen.getByTestId('stepper')).toHaveTextContent('2/5'); | ||
}); | ||
it('should normalize steps to 1 when steps is a float', () => { | ||
render(<Stepper steps={1.5} activeStep={1} />); | ||
|
||
expect(screen.getByTestId('stepper')).toHaveTextContent('1/2'); | ||
}); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
packages/react-components/src/components/Stepper/Stepper.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { type Meta, type StoryObj } from '@storybook/react'; | ||
|
||
import { Stepper } from './Stepper'; | ||
|
||
const meta: Meta<typeof Stepper> = { | ||
title: 'Components/Stepper', | ||
component: Stepper, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Stepper>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
activeStep: 1, | ||
steps: 6, | ||
}, | ||
}; | ||
|
||
export const MiddleProgress: Story = { | ||
args: { | ||
activeStep: 3, | ||
steps: 6, | ||
}, | ||
}; | ||
|
||
export const Completed: Story = { | ||
args: { | ||
activeStep: 6, | ||
steps: 6, | ||
}, | ||
}; |
55 changes: 55 additions & 0 deletions
55
packages/react-components/src/components/Stepper/Stepper.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as React from 'react'; | ||
|
||
import cx from 'clsx'; | ||
|
||
import { Text } from '../Typography'; | ||
|
||
import { StepperProps } from './types'; | ||
|
||
import styles from './Stepper.module.scss'; | ||
|
||
const baseClass = 'stepper'; | ||
|
||
export const Stepper: React.FC<StepperProps> = ({ | ||
activeStep, | ||
steps, | ||
className, | ||
'data-testid': dataTestId = 'stepper', | ||
...divProps | ||
}) => { | ||
const normalizedSteps = Math.round(Math.max(1, steps)); | ||
|
||
const normalizedActiveStep = Math.round( | ||
Math.min(Math.max(1, activeStep), normalizedSteps) | ||
); | ||
|
||
const mergedClassNames = cx(styles[baseClass], className); | ||
|
||
return ( | ||
<div className={mergedClassNames} data-testid={dataTestId} {...divProps}> | ||
<Text size="sm" className={styles[`${baseClass}__counter`]}> | ||
{normalizedActiveStep}/{normalizedSteps} | ||
</Text> | ||
<div | ||
className={styles[`${baseClass}__steps__container`]} | ||
data-testid={`${dataTestId}-container`} | ||
> | ||
{Array.from({ length: normalizedSteps }, (_, index) => { | ||
const stepNumber = index + 1; | ||
const isCompleted = stepNumber <= normalizedActiveStep; | ||
const isActive = stepNumber > normalizedActiveStep; | ||
|
||
return ( | ||
<div | ||
key={stepNumber} | ||
className={cx(styles[`${baseClass}__step`], { | ||
[styles[`${baseClass}__step--completed`]]: isCompleted, | ||
[styles[`${baseClass}__step--active`]]: isActive, | ||
})} | ||
dprzybylek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/> | ||
); | ||
})} | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { Stepper } from './Stepper'; | ||
JoannaSikora marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export type { StepperProps } from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as React from 'react'; | ||
|
||
export interface StepperProps extends React.HTMLAttributes<HTMLDivElement> { | ||
/** | ||
* Current active step (1-based) | ||
*/ | ||
activeStep: number; | ||
/** | ||
* Total number of steps | ||
*/ | ||
steps: number; | ||
/** | ||
* Custom class name | ||
*/ | ||
className?: string; | ||
/** | ||
* Optional test id for testing | ||
*/ | ||
'data-testid'?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Color contrast ratios are insufficient for accessibility
The disabled text color fails WCAG AA requirements in both themes:
Minimum required contrast ratio for normal text is 4.5:1.
🔗 Analysis chain
Verify color contrast for counter text.
Ensure that
--content-basic-disabled
provides sufficient contrast against the background for accessibility.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 10416
Script:
Length of output: 2032
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vladko-uxds
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use another one for those step number 1/3...
--Content-Basic-Placeholder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vladko-uxds changed ✅