-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathStep.tsx
187 lines (171 loc) · 5.3 KB
/
Step.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* eslint react/prop-types: 0 */
import * as React from 'react';
import classNames from 'classnames';
import KeyCode from 'rc-util/lib/KeyCode';
import type { Status, Icons } from './interface';
import type { StepIconRender, ProgressDotRender } from './Steps';
function isString(str: any): str is string {
return typeof str === 'string';
}
export interface StepProps {
prefixCls?: string;
className?: string;
style?: React.CSSProperties;
wrapperStyle?: React.CSSProperties;
iconPrefix?: string;
active?: boolean;
disabled?: boolean;
stepIndex?: number;
stepNumber?: number;
status?: Status;
title?: React.ReactNode;
subTitle?: React.ReactNode;
description?: React.ReactNode;
tailContent?: React.ReactNode;
icon?: React.ReactNode;
icons?: Icons;
onClick?: React.MouseEventHandler<HTMLDivElement>;
onStepClick?: (index: number) => void;
progressDot?: ProgressDotRender | boolean;
stepIcon?: StepIconRender;
render?: (stepItem: React.ReactElement) => React.ReactNode;
}
const Step: React.FC<StepProps> = (props) => {
const {
className,
prefixCls,
style,
active,
status,
iconPrefix,
icon,
wrapperStyle,
stepNumber,
disabled,
description,
title,
subTitle,
progressDot,
stepIcon,
tailContent,
icons,
stepIndex,
onStepClick,
onClick,
render,
...restProps
} = props;
// ========================= Click ==========================
const clickable = !!onStepClick && !disabled;
const accessibilityProps: {
role?: string;
tabIndex?: number;
onClick?: React.MouseEventHandler<HTMLDivElement>;
onKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;
} = {};
if (clickable) {
accessibilityProps.role = 'button';
accessibilityProps.tabIndex = 0;
accessibilityProps.onClick = (e) => {
onClick?.(e);
onStepClick(stepIndex);
};
accessibilityProps.onKeyDown = (e) => {
const { which } = e;
if (which === KeyCode.ENTER || which === KeyCode.SPACE) {
onStepClick(stepIndex);
}
};
}
// ========================= Render =========================
const renderIconNode = () => {
let iconNode: React.ReactNode;
const iconClassName = classNames(`${prefixCls}-icon`, `${iconPrefix}icon`, {
[`${iconPrefix}icon-${icon}`]: icon && isString(icon),
[`${iconPrefix}icon-check`]:
!icon && status === 'finish' && ((icons && !icons.finish) || !icons),
[`${iconPrefix}icon-cross`]:
!icon && status === 'error' && ((icons && !icons.error) || !icons),
});
const iconDot = <span className={`${prefixCls}-icon-dot`} />;
// `progressDot` enjoy the highest priority
if (progressDot) {
if (typeof progressDot === 'function') {
iconNode = (
<span className={`${prefixCls}-icon`}>
{progressDot(iconDot, {
index: stepNumber - 1,
status,
title,
description,
})}
</span>
);
} else {
iconNode = <span className={`${prefixCls}-icon`}>{iconDot}</span>;
}
} else if (icon && !isString(icon)) {
iconNode = <span className={`${prefixCls}-icon`}>{icon}</span>;
} else if (icons && icons.finish && status === 'finish') {
iconNode = <span className={`${prefixCls}-icon`}>{icons.finish}</span>;
} else if (icons && icons.error && status === 'error') {
iconNode = <span className={`${prefixCls}-icon`}>{icons.error}</span>;
} else if (icon || status === 'finish' || status === 'error') {
iconNode = <span className={iconClassName} />;
} else {
iconNode = <span className={`${prefixCls}-icon`}>{stepNumber}</span>;
}
if (stepIcon) {
iconNode = stepIcon({
index: stepNumber - 1,
status,
title,
description,
node: iconNode,
});
}
return iconNode;
};
const mergedStatus = status || 'wait';
const classString = classNames(
`${prefixCls}-item`,
`${prefixCls}-item-${mergedStatus}`,
className,
{
[`${prefixCls}-item-custom`]: icon,
[`${prefixCls}-item-active`]: active,
[`${prefixCls}-item-disabled`]: disabled === true,
},
);
const stepItemStyle: React.CSSProperties = { ...style };
let stepNode: React.ReactNode = (
<div {...restProps} className={classString} style={stepItemStyle}>
<div onClick={onClick} {...accessibilityProps} className={`${prefixCls}-item-container`}>
<div className={`${prefixCls}-item-tail`}>{tailContent}</div>
<div className={`${prefixCls}-item-icon`}>{renderIconNode()}</div>
<div className={`${prefixCls}-item-content`}>
<div className={`${prefixCls}-item-title`}>
{title}
{subTitle && (
<div
title={typeof subTitle === 'string' ? subTitle : undefined}
className={`${prefixCls}-item-subtitle`}
>
{subTitle}
</div>
)}
</div>
{description && <div className={`${prefixCls}-item-description`}>{description}</div>}
</div>
</div>
</div>
);
if (render) {
stepNode = (render(stepNode) || null) as React.ReactElement;
}
return stepNode;
};
if (process.env.NODE_ENV !== 'production') {
Step.displayName = 'rc-step';
}
export default Step;