1- import React , { useState , useEffect , Children , cloneElement , memo } from 'react' ;
1+ import React , {
2+ useState ,
3+ useEffect ,
4+ Children ,
5+ cloneElement ,
6+ memo ,
7+ } from 'react' ;
28import { StepperContext } from './UseStepper' ;
39import { classNames } from '../common' ;
410
@@ -23,19 +29,11 @@ export type StepperProps = {
2329 * Defines the className of the entire stepper.
2430 */
2531 stepperClassName ?: string ;
26- /**
27- * Defines the className of the header container.
28- */
29- headerContainerClassNames ?: string ;
3032 /**
3133 * Defines the style of all StepHeader elements from the parent.
3234 * To style them independently, use className on the StepHeader element.
3335 */
3436 headerClassName ?: string ;
35- /**
36- * Defines the className of the content container.
37- */
38- contentContainerClassNames ?: string ;
3937 /**
4038 * Defines the style of all StepContent elements from the parent.
4139 * To style them independently, use className on the StepContent element.
@@ -47,86 +45,97 @@ export type StepperProps = {
4745 orientation ?: 'horizontal' | 'vertical' ;
4846} ;
4947
50- export const Stepper = memo ( ( {
51- children,
52- separator,
53- selectedStep = 0 ,
54- activeStepClassName,
55- stepperClassName,
56- headerClassName,
57- contentClassName,
58- orientation = 'horizontal' ,
59- } : StepperProps ) => {
60- const [ activeStep , setActiveStep ] = useState ( selectedStep ) ;
61-
62- useEffect ( ( ) => {
63- setActiveStep ( selectedStep ) ;
64- } , [ selectedStep ] ) ;
48+ export const Stepper = memo (
49+ ( {
50+ children,
51+ separator,
52+ selectedStep = 0 ,
53+ activeStepClassName,
54+ stepperClassName,
55+ headerClassName,
56+ contentClassName,
57+ orientation = 'horizontal' ,
58+ } : StepperProps ) => {
59+ const [ activeStep , setActiveStep ] = useState ( selectedStep ) ;
6560
66- const onClickHandler = ( index : number ) => setActiveStep ( index ) ;
61+ useEffect ( ( ) => {
62+ setActiveStep ( selectedStep ) ;
63+ } , [ selectedStep ] ) ;
6764
68- const steps : JSX . Element [ ] = [ ] ;
65+ const onClickHandler = ( index : number ) => setActiveStep ( index ) ;
6966
70- Children . forEach ( children , ( child , index ) => {
71- if ( child . type . name === 'Step' ) {
72- let stepHeader : JSX . Element | null = null ;
73- let stepContent : JSX . Element | null = null ;
67+ const steps : JSX . Element [ ] = [ ] ;
7468
75- Children . forEach ( child . props . children , ( child ) => {
76- if ( child . type . name === 'StepHeader' ) {
77- const headerClasses = classNames ( [
78- { 'dcx-active-step' : index === activeStep } ,
79- { [ `${ activeStepClassName } ` ] : index === activeStep } ,
80- headerClassName ,
81- ] ) ;
69+ Children . forEach ( children , ( child , index ) => {
70+ if ( child . type . name === 'Step' ) {
71+ let stepHeader : JSX . Element | null = null ;
72+ let stepContent : JSX . Element | null = null ;
8273
83- stepHeader = cloneElement ( child , {
84- key : `header-${ index } ` ,
85- _index : index ,
86- headerClassName : headerClasses ,
87- 'aria-selected' : index === activeStep ? 'true' : 'false' ,
88- 'aria-posinset' : index + 1 ,
89- 'aria-setsize' : Children . count ( children ) ,
90- tabIndex : index === activeStep ? '0' : '-1' ,
91- onClick : ( ) => onClickHandler ( index ) ,
92- } ) ;
93- } else if ( child . type . name === 'StepContent' ) {
94- stepContent = cloneElement ( child , {
95- key : `content-${ index } ` ,
96- className : contentClassName ,
97- visible : index === activeStep ,
98- } ) ;
99- }
100- } ) ;
74+ Children . forEach ( child . props . children , ( child ) => {
75+ if ( child . type . name === 'StepHeader' ) {
76+ const headerClasses = classNames ( [
77+ { 'dcx-active-step' : index === activeStep } ,
78+ { [ `${ activeStepClassName } ` ] : index === activeStep } ,
79+ headerClassName ,
80+ ] ) ;
10181
102- if ( stepHeader && stepContent ) {
103- steps . push (
104- < div key = { `step-${ index } ` } className = "dcx-step" >
105- { stepHeader }
106- { stepContent }
107- </ div >
108- ) ;
82+ stepHeader = cloneElement ( child , {
83+ key : `header-${ index } ` ,
84+ _index : index ,
85+ headerClassName : headerClasses ,
86+ 'aria-selected' : index === activeStep ? 'true' : 'false' ,
87+ 'aria-posinset' : index + 1 ,
88+ 'aria-setsize' : Children . count ( children ) ,
89+ tabIndex : index === activeStep ? '0' : '-1' ,
90+ onClick : ( ) => onClickHandler ( index ) ,
91+ } ) ;
92+ } else if ( child . type . name === 'StepContent' ) {
93+ stepContent = cloneElement ( child , {
94+ key : `content-${ index } ` ,
95+ className : contentClassName ,
96+ visible : index === activeStep ,
97+ } ) ;
98+ }
99+ } ) ;
109100
110- if ( separator && index < children . length - 1 ) {
101+ if ( stepHeader && stepContent ) {
111102 steps . push (
112- cloneElement ( separator , { key : `separator-${ index } ` , className : 'dcx-separator' } )
103+ < div key = { `step-${ index } ` } className = "dcx-step" >
104+ < div key = { `header-${ index } ` } className = "dcx-step-header" >
105+ { stepHeader }
106+ </ div >
107+ < div key = { `content-${ index } ` } className = "dcx-step-content" >
108+ { stepContent }
109+ </ div >
110+ </ div >
113111 ) ;
112+
113+ if ( separator && index < children . length - 1 ) {
114+ steps . push (
115+ cloneElement ( separator , {
116+ key : `separator-${ index } ` ,
117+ className : 'dcx-separator' ,
118+ } )
119+ ) ;
120+ }
114121 }
115122 }
116- }
117- } ) ;
123+ } ) ;
118124
119- const containerClasses = classNames ( [
120- 'dcx-stepper' ,
121- orientation === 'horizontal' ? 'dcx-horizontal-stepper' : 'dcx-vertical-stepper' ,
122- stepperClassName ,
123- ] ) ;
125+ const containerClasses = classNames ( [
126+ 'dcx-stepper' ,
127+ orientation === 'horizontal'
128+ ? 'dcx-horizontal-stepper'
129+ : 'dcx-vertical-stepper' ,
130+ stepperClassName ,
131+ ] ) ;
124132
125- return (
126- < StepperContext . Provider value = { { activeStep, changeActiveStep : onClickHandler } } >
127- < div className = { containerClasses } >
128- { steps }
129- </ div >
130- </ StepperContext . Provider >
131- ) ;
132- } ) ;
133+ return (
134+ < StepperContext . Provider
135+ value = { { activeStep, changeActiveStep : onClickHandler } }
136+ >
137+ < div className = { containerClasses } > { steps } </ div >
138+ </ StepperContext . Provider >
139+ ) ;
140+ }
141+ ) ;
0 commit comments