Skip to content
Merged
12 changes: 2 additions & 10 deletions packages/ui/src/stepper/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,14 @@ export const StepperRoot = forwardRef< StepperRef, StepperRootProps >( function
},
} ) );

// Dev warnings: missing accessibility label, duplicate values, missing step
// Dev warnings: missing accessibility label, missing step.
// Duplicate step values are detected at registration time in useStepRegistration.
useEffect( () => {
if ( ! ariaLabel && ! ariaLabelledBy ) {
warning(
"[Stepper] Stepper requires either 'aria-label' or 'aria-labelledby' for accessibility."
);
}
const seenValues = new Set< string >();
for ( const s of steps ) {
if ( seenValues.has( s.value ) ) {
warning(
`[Stepper] Two steps share value '${ s.value }'. Each step must have a unique value.`
);
}
seenValues.add( s.value );
}
if ( value && steps.length > 0 && ! steps.some( ( s ) => s.value === value ) ) {
warning(
`[Stepper] No step found with value '${ value }'. Check that the value matches one of the registered step values.`
Expand Down
14 changes: 9 additions & 5 deletions packages/ui/src/stepper/step.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// packages/ui/src/stepper/step.tsx
import { Accordion } from '@base-ui/react/accordion';
import { forwardRef, useContext, useEffect, useLayoutEffect } from '@wordpress/element';
import { forwardRef, useContext, useEffect, useId, useLayoutEffect } from '@wordpress/element';
import { StepContext, StepperContext } from './context';
import type { StepContextValue, StepStatus } from './types';
import type { ReactNode } from 'react';
Expand Down Expand Up @@ -33,6 +33,10 @@ export const StepperStep = forwardRef< HTMLDivElement, StepperStepProps >( funct
orientation,
} = rootCtx;

// Stable per-mount id so each instance owns its own registration, even when
// two steps transiently share the same `value`.
const id = useId();

// Compute derived state
const isCurrent = value === activeValue;
const index = steps.findIndex( ( s ) => s.value === value );
Expand All @@ -47,17 +51,17 @@ export const StepperStep = forwardRef< HTMLDivElement, StepperStepProps >( funct
// useLayoutEffect ensures registration happens before the first browser paint,
// so totalSteps and index are correct on the first visible render.
useLayoutEffect( () => {
const deregister = registerStep( { value, status, disabled: isDisabled } );
const deregister = registerStep( id, { value, status, disabled: isDisabled } );
return deregister;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ value ] );
}, [ id, value ] );

// useEffect (not useLayoutEffect) is intentional: syncing metadata after
// commit does not affect the initial render. The redundant call on first
// mount is harmless — updateStep is a no-op when props have not changed.
useEffect( () => {
updateStep( { value, status, disabled: isDisabled } );
}, [ value, status, isDisabled, updateStep ] );
updateStep( id, { value, status, disabled: isDisabled } );
}, [ id, value, status, isDisabled, updateStep ] );

const stepCtx: StepContextValue = {
value,
Expand Down
95 changes: 89 additions & 6 deletions packages/ui/src/stepper/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,95 @@ describe( 'Stepper dev warnings', () => {
spy.mockRestore();
} );

// NOTE: A test for the duplicate-value warning ("Two steps share value '...'") is
// deliberately omitted. useStepRegistration prevents duplicate entries at registration
// time (it bails out if a step with the same value is already in state), so the
// steps array that the useEffect in root.tsx inspects will never contain duplicates.
// The warning is unreachable in a JSDOM environment via normal JSX rendering, making
// any such test a never-fails assertion that proves nothing about real behaviour.
// NOTE: warning() dedupes messages process-wide, so these tests use step values
// unique to this file to make sure the expected message has not fired elsewhere.
it( 'warns when two steps share the same value', async () => {
const spy = jest.spyOn( console, 'warn' ).mockImplementation( () => {} );
render(
<Stepper.Root orientation="vertical" value="dup-warn" aria-label="Test">
<Stepper.Step value="dup-warn">
<Stepper.Indicator />
</Stepper.Step>
<Stepper.Step value="dup-warn">
<Stepper.Indicator />
</Stepper.Step>
</Stepper.Root>
);
await waitFor( () => {
expect( spy ).toHaveBeenCalledWith(
expect.stringContaining( "Two steps share value 'dup-warn'" )
);
} );
spy.mockRestore();
} );

it( 'keeps the original step registered when a duplicate step unmounts', async () => {
const spy = jest.spyOn( console, 'warn' ).mockImplementation( () => {} );
function Dynamic() {
const [ showDuplicate, setShowDuplicate ] = useState( true );
return (
Comment thread
bogiii marked this conversation as resolved.
Resolved
<>
<button onClick={ () => setShowDuplicate( false ) }>Remove duplicate</button>
<Stepper.Root orientation="vertical" value="dup-unmount" aria-label="Test">
<Stepper.Step value="dup-unmount">
<Stepper.Indicator />
</Stepper.Step>
{ showDuplicate && (
<Stepper.Step value="dup-unmount">
<Stepper.Indicator />
</Stepper.Step>
) }
<Stepper.Step value="last">
<Stepper.Indicator />
</Stepper.Step>
</Stepper.Root>
</>
);
}
const user = userEvent.setup();
render( <Dynamic /> );
// The duplicate is rejected at registration time, so only two steps count.
await waitFor( () => expect( screen.getByText( 'Step 2 of 2' ) ).toBeInTheDocument() );
await user.click( screen.getByRole( 'button', { name: /remove duplicate/i } ) );
// Unmounting the rejected duplicate must not deregister the original step.
await waitFor( () => expect( screen.getByText( 'Step 2 of 2' ) ).toBeInTheDocument() );
expect( screen.getByText( 'Step 1 of 2' ) ).toBeInTheDocument();
spy.mockRestore();
} );

it( 'keeps the step registered when the original unmounts but a duplicate is still mounted', async () => {
const spy = jest.spyOn( console, 'warn' ).mockImplementation( () => {} );
function Dynamic() {
const [ showOriginal, setShowOriginal ] = useState( true );
return (
<>
<button onClick={ () => setShowOriginal( false ) }>Remove original</button>
<Stepper.Root orientation="vertical" value="dup-orig" aria-label="Test">
{ showOriginal && (
<Stepper.Step value="dup-orig">
<Stepper.Indicator />
</Stepper.Step>
) }
<Stepper.Step value="dup-orig">
<Stepper.Indicator />
</Stepper.Step>
<Stepper.Step value="last">
<Stepper.Indicator />
</Stepper.Step>
</Stepper.Root>
</>
);
}
const user = userEvent.setup();
render( <Dynamic /> );
// The duplicate is rejected at registration time, so only two steps count.
await waitFor( () => expect( screen.getByText( 'Step 2 of 2' ) ).toBeInTheDocument() );
await user.click( screen.getByRole( 'button', { name: /remove original/i } ) );
// The duplicate instance is still mounted, so the value must stay registered.
await waitFor( () => expect( screen.getByText( 'Step 2 of 2' ) ).toBeInTheDocument() );
expect( screen.getByText( 'Step 1 of 2' ) ).toBeInTheDocument();
spy.mockRestore();
} );
} );

describe( 'Stepper.Panel dev warnings', () => {
Expand Down
180 changes: 180 additions & 0 deletions packages/ui/src/stepper/test/use-step-registration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { renderHook, act } from '@testing-library/react';
import { useStepRegistration } from '../use-step-registration';
import type { StepMeta } from '../types';

describe( 'useStepRegistration duplicate values', () => {
let warnSpy: jest.SpyInstance;

beforeEach( () => {
// Duplicate values are a dev error and emit a one-time console warning;
// suppress it here so test output stays clean.
warnSpy = jest.spyOn( console, 'warn' ).mockImplementation( () => {} );
} );

afterEach( () => {
warnSpy.mockRestore();
} );

it( 'reflects the surviving instance metadata when a duplicate unmounts', () => {
const { result } = renderHook( () => useStepRegistration< StepMeta >() );
let deregisterDuplicate: () => void = () => {};
act( () => {
result.current.registerStep( 'a-1', { value: 'a', status: 'completed', disabled: false } );
deregisterDuplicate = result.current.registerStep( 'a-2', {
value: 'a',
status: 'error',
disabled: true,
} );
} );
// In real usage each Step's effect syncs its own props; the duplicate's
// sync must not overwrite the shared-value entry (the value-keyed bug).
act( () => {
result.current.updateStep( 'a-2', { value: 'a', status: 'error', disabled: true } );
} );
// Even while both are mounted, the canonical entry stays 'a-1', not the
// later writer 'a-2'.
expect( result.current.steps ).toEqual( [
{ value: 'a', status: 'completed', disabled: false },
] );
act( () => {
deregisterDuplicate();
} );
// ...and it remains correct after the duplicate unmounts.
expect( result.current.steps ).toEqual( [
{ value: 'a', status: 'completed', disabled: false },
] );
} );

it( 'keeps a value in its original slot when an earlier duplicate unmounts', () => {
const { result } = renderHook( () => useStepRegistration< StepMeta >() );
let deregisterFirstA: () => void = () => {};
act( () => {
deregisterFirstA = result.current.registerStep( 'a-1', { value: 'a', disabled: false } );
result.current.registerStep( 'b-1', { value: 'b', disabled: false } );
// Duplicate of 'a', registered after 'b'.
result.current.registerStep( 'a-2', { value: 'a', disabled: true } );
} );
act( () => {
deregisterFirstA();
} );
// 'a' keeps its first-appearance slot (index 0) rather than jumping to the
// end, and now reflects the surviving instance ('a-2').
expect( result.current.steps ).toEqual( [
{ value: 'a', disabled: true },
{ value: 'b', disabled: false },
] );
} );

it( 'keeps the value registered until the last instance unmounts', () => {
const { result } = renderHook( () => useStepRegistration< StepMeta >() );
let deregisterFirst: () => void = () => {};
act( () => {
deregisterFirst = result.current.registerStep( 'a-1', { value: 'a', disabled: false } );
result.current.registerStep( 'a-2', { value: 'a', disabled: false } );
} );
act( () => {
deregisterFirst();
} );
// One instance remains, so the value stays registered and reflects the
// surviving instance.
expect( result.current.steps ).toEqual( [ { value: 'a', disabled: false } ] );
} );
} );

describe( 'useStepRegistration duplicate warning', () => {
let warnSpy: jest.SpyInstance;

beforeEach( () => {
warnSpy = jest.spyOn( console, 'warn' ).mockImplementation( () => {} );
} );

afterEach( () => {
warnSpy.mockRestore();
} );

it( 'warns when a second instance registers an already-registered value', () => {
const { result } = renderHook( () => useStepRegistration< StepMeta >() );
act( () => {
result.current.registerStep( 'dup-1', { value: 'shared', disabled: false } );
result.current.registerStep( 'dup-2', { value: 'shared', disabled: false } );
} );
// Distinct values per test keep the warning util's message-level dedupe
// from masking this assertion across the file.
expect( warnSpy ).toHaveBeenCalledWith(
expect.stringContaining( "Two steps share value 'shared'" )
);
} );

it( 'does not warn when every step value is unique', () => {
const { result } = renderHook( () => useStepRegistration< StepMeta >() );
act( () => {
result.current.registerStep( 'u-1', { value: 'one', disabled: false } );
result.current.registerStep( 'u-2', { value: 'two', disabled: false } );
} );
expect( warnSpy ).not.toHaveBeenCalled();
} );
} );

describe( 'useStepRegistration updateStep', () => {
function setup() {
const hook = renderHook( () => useStepRegistration< StepMeta >() );
act( () => {
hook.result.current.registerStep( 'a', {
value: 'a',
status: 'completed',
disabled: false,
} );
hook.result.current.registerStep( 'b', { value: 'b', disabled: false } );
} );
return hook;
}

it( 'keeps the same steps array reference when meta has not changed', () => {
const { result } = setup();
const before = result.current.steps;
act( () => {
result.current.updateStep( 'a', { value: 'a', status: 'completed', disabled: false } );
} );
expect( result.current.steps ).toBe( before );
} );

it( 'updates the entry when a field value changes', () => {
const { result } = setup();
act( () => {
result.current.updateStep( 'a', { value: 'a', status: 'error', disabled: false } );
} );
expect( result.current.steps[ 0 ] ).toEqual( {
value: 'a',
status: 'error',
disabled: false,
} );
} );

it( 'updates the entry when a field present in the existing meta is absent from the new meta', () => {
const { result } = setup();
// `status` key intentionally omitted — the compare must not treat a
// removed field as "no change".
act( () => {
result.current.updateStep( 'a', { value: 'a', disabled: false } );
} );
expect( result.current.steps[ 0 ] ).toEqual( { value: 'a', disabled: false } );
expect( result.current.steps[ 0 ] ).not.toHaveProperty( 'status' );
} );

it( 'is a no-op for an id that was never registered', () => {
const { result } = setup();
const before = result.current.steps;
act( () => {
result.current.updateStep( 'missing', { value: 'missing', disabled: false } );
} );
expect( result.current.steps ).toBe( before );
} );

it( 'preserves registration order when updating a step', () => {
const { result } = setup();
act( () => {
result.current.updateStep( 'a', { value: 'a', status: 'error', disabled: true } );
} );
expect( result.current.steps.map( ( s ) => s.value ) ).toEqual( [ 'a', 'b' ] );
} );
} );
6 changes: 2 additions & 4 deletions packages/ui/src/stepper/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export type StepperContextValue = {
activationMode: 'auto' | 'manual';
steps: StepMeta[];
totalSteps: number;
registerStep: ( meta: StepMeta ) => () => void;
updateStep: ( meta: StepMeta ) => void;
registerStep: ( id: string, meta: StepMeta ) => () => void;
updateStep: ( id: string, meta: StepMeta ) => void;
registerTriggerRef: ( value: string, el: HTMLElement | null ) => void;
formatStepLabel: ( step: number, total: number, status?: StepStatus ) => string;
indicatorVariant: IndicatorVariant;
Expand Down Expand Up @@ -154,6 +154,4 @@ export type StepperPanelProps = ComponentProps< 'div' > & {
value?: string;
/** Keep panel mounted when inactive (works in both orientations) */
keepMounted?: boolean;
children: ReactNode;
className?: string;
};
Loading
Loading