11/* eslint react/prop-types: 0 */
22import * as React from 'react' ;
33import classNames from 'classnames' ;
4+ import KeyCode from 'rc-util/lib/KeyCode' ;
45import type { Status , Icons } from './interface' ;
56import type { StepIconRender , ProgressDotRender } from './Steps' ;
67
@@ -29,7 +30,7 @@ export interface StepProps {
2930 onStepClick ?: ( index : number ) => void ;
3031 progressDot ?: ProgressDotRender | boolean ;
3132 stepIcon ?: StepIconRender ;
32- render ?: ( stepItem : React . ReactNode ) => React . ReactNode ;
33+ render ?: ( stepItem : React . ReactElement ) => React . ReactNode ;
3334}
3435
3536function Step ( props : StepProps ) {
@@ -58,14 +59,31 @@ function Step(props: StepProps) {
5859 ...restProps
5960 } = props ;
6061
61- const onInternalClick : React . MouseEventHandler < HTMLDivElement > = ( ...args ) => {
62- if ( onClick ) {
63- onClick ( ...args ) ;
64- }
62+ // ========================= Click ==========================
63+ const clickable = ! ! onStepClick && ! disabled ;
6564
66- onStepClick ( stepIndex ) ;
67- } ;
65+ const accessibilityProps : {
66+ role ?: string ;
67+ tabIndex ?: number ;
68+ onClick ?: React . MouseEventHandler < HTMLDivElement > ;
69+ onKeyDown ?: React . KeyboardEventHandler < HTMLDivElement > ;
70+ } = { } ;
71+ if ( clickable ) {
72+ accessibilityProps . role = 'button' ;
73+ accessibilityProps . tabIndex = 0 ;
74+ accessibilityProps . onClick = ( e ) => {
75+ onClick ?.( e ) ;
76+ onStepClick ( stepIndex ) ;
77+ } ;
78+ accessibilityProps . onKeyDown = ( e ) => {
79+ const { which } = e ;
80+ if ( which === KeyCode . ENTER || which === KeyCode . SPACE ) {
81+ onStepClick ( stepIndex ) ;
82+ }
83+ } ;
84+ }
6885
86+ // ========================= Render =========================
6987 const renderIconNode = ( ) => {
7088 let iconNode ;
7189 const iconClassName = classNames ( `${ prefixCls } -icon` , `${ iconPrefix } icon` , {
@@ -119,25 +137,19 @@ function Step(props: StepProps) {
119137
120138 const mergedStatus = status || 'wait' ;
121139
122- const classString = classNames ( `${ prefixCls } -item` , `${ prefixCls } -item-${ mergedStatus } ` , className , {
123- [ `${ prefixCls } -item-custom` ] : icon ,
124- [ `${ prefixCls } -item-active` ] : active ,
125- [ `${ prefixCls } -item-disabled` ] : disabled === true ,
126- } ) ;
140+ const classString = classNames (
141+ `${ prefixCls } -item` ,
142+ `${ prefixCls } -item-${ mergedStatus } ` ,
143+ className ,
144+ {
145+ [ `${ prefixCls } -item-custom` ] : icon ,
146+ [ `${ prefixCls } -item-active` ] : active ,
147+ [ `${ prefixCls } -item-disabled` ] : disabled === true ,
148+ } ,
149+ ) ;
127150 const stepItemStyle = { ...style } ;
128151
129- const accessibilityProps : {
130- role ?: string ;
131- tabIndex ?: number ;
132- onClick ?: React . MouseEventHandler < HTMLDivElement > ;
133- } = { } ;
134- if ( onStepClick && ! disabled ) {
135- accessibilityProps . role = 'button' ;
136- accessibilityProps . tabIndex = 0 ;
137- accessibilityProps . onClick = onInternalClick ;
138- }
139-
140- let stepNode : React . ReactNode = (
152+ let stepNode : React . ReactElement = (
141153 < div { ...restProps } className = { classString } style = { stepItemStyle } >
142154 < div onClick = { onClick } { ...accessibilityProps } className = { `${ prefixCls } -item-container` } >
143155 < div className = { `${ prefixCls } -item-tail` } > { tailContent } </ div >
@@ -160,12 +172,11 @@ function Step(props: StepProps) {
160172 </ div >
161173 ) ;
162174
163-
164175 if ( render ) {
165- stepNode = render ( stepNode ) || null ;
176+ stepNode = ( render ( stepNode ) || null ) as React . ReactElement ;
166177 }
167178
168- return stepNode as React . ReactElement ;
179+ return stepNode ;
169180}
170181
171182export default Step ;
0 commit comments