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(stepper): added stepper component #1500

Merged
merged 6 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
@@ -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);
Copy link
Contributor

@coderabbitai coderabbitai bot Jan 24, 2025

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:

  • Light theme: 3.39:1 (#8d8d95 on white)
  • Dark theme: 3.16:1 (#767680 on #202024)

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:

#!/bin/bash
# Search for the color variable definition
rg --type css "content-basic-disabled" -A 2

Length of output: 10416


Script:

#!/bin/bash
# Search for background color definitions in Stepper component
rg -B 2 -A 2 "background|surface" packages/react-components/src/components/Stepper/Stepper.module.scss

# Also search theme files for default surface colors
rg "surface-primary-default|surface-basic-default" packages/react-components/src/themes/

Length of output: 2032

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vladko-uxds changed ✅

}

&__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 packages/react-components/src/components/Stepper/Stepper.spec.tsx
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');
});
});
});
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 packages/react-components/src/components/Stepper/Stepper.tsx
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>
);
};
2 changes: 2 additions & 0 deletions packages/react-components/src/components/Stepper/index.ts
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';
20 changes: 20 additions & 0 deletions packages/react-components/src/components/Stepper/types.ts
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;
}
1 change: 1 addition & 0 deletions packages/react-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ export * from './components/UpdateBadge';
export * from './components/FileUploadProgress';
export * from './components/UploadBar';
export * from './components/FloatingPortal';
export * from './components/Stepper';
Loading