Skip to content

Commit c27ab88

Browse files
authored
feat: support onChange (#71)
* support onChange * update node version * add test case * update style
1 parent 7b3d706 commit c27ab88

File tree

10 files changed

+103
-7
lines changed

10 files changed

+103
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ assets/**/*.css
2525
build
2626
lib
2727
es
28+
coverage

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ notifications:
88
99

1010
node_js:
11-
- node
11+
- 10
1212

1313
before_install:
1414
- |

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ online example: http://react-component.github.io/steps/examples/
9292
<td></td>
9393
<td>spicify the default finish icon and error icon</td>
9494
</tr>
95+
<tr>
96+
<td>onChange</td>
97+
<td>(current: number) => void</td>
98+
<td></td>
99+
<td>Trigger when Step changed</td>
100+
</tr>
95101
</tbody>
96102
</table>
97103

assets/index.less

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
flex: 1;
2020
overflow: hidden;
2121

22+
&[role='button'] {
23+
cursor: pointer;
24+
transition: all .3s;
25+
26+
&:hover {
27+
opacity: 0.7;
28+
}
29+
}
30+
2231
&:last-child {
2332
flex: none;
2433
}

examples/simple.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ import Steps, { Step } from 'rc-steps';
77
const container = document.getElementById('__react-content');
88
const description = '这里是多信息的描述啊描述啊描述啊描述啊哦耶哦耶哦耶哦耶哦耶哦耶哦耶哦耶哦耶哦耶哦耶哦耶';
99

10+
const ControlSteps = () => {
11+
const [current, setCurrent] = React.useState(0);
12+
return (
13+
<Steps
14+
current={current}
15+
onChange={(val) => {
16+
setCurrent(val);
17+
}}
18+
>
19+
<Step title="已完成" />
20+
<Step title="进行中" />
21+
<Step title="待运行" description="Hello World!" />
22+
<Step title="待运行" />
23+
</Steps>
24+
);
25+
};
26+
1027
ReactDOM.render(
1128
<div>
1229
<Steps current={1}>
@@ -27,5 +44,7 @@ ReactDOM.render(
2744
<Step title="待运行" description={description} />
2845
<Step title="待运行" description={description} />
2946
</Steps>
30-
</div>
31-
, container);
47+
<ControlSteps />
48+
</div>,
49+
container,
50+
);

now.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": 2,
3+
"name": "rc-steps",
4+
"builds": [
5+
{
6+
"src": "package.json",
7+
"use": "@now/static-build",
8+
"config": { "distDir": "build" }
9+
}
10+
]
11+
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"lint:fix": "rc-tools run lint --fix",
4444
"test": "jest",
4545
"prepublish": "rc-tools run guard",
46-
"coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls"
46+
"coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls",
47+
"now-build": "npm run build"
4748
},
4849
"jest": {
4950
"setupFiles": [

src/Step.jsx

+25-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default class Step extends React.Component {
2424
PropTypes.string,
2525
]),
2626
stepNumber: PropTypes.string,
27+
stepIndex: PropTypes.number,
2728
description: PropTypes.any,
2829
title: PropTypes.any,
2930
progressDot: PropTypes.oneOfType([
@@ -35,7 +36,20 @@ export default class Step extends React.Component {
3536
finish: PropTypes.node,
3637
error: PropTypes.node,
3738
}),
39+
onClick: PropTypes.func,
40+
onStepClick: PropTypes.func,
3841
};
42+
43+
onClick = (...args) => {
44+
const { onClick, onStepClick, stepIndex } = this.props;
45+
46+
if (onClick) {
47+
onClick(...args);
48+
}
49+
50+
onStepClick(stepIndex);
51+
};
52+
3953
renderIconNode() {
4054
const {
4155
prefixCls, progressDot, stepNumber, status, title, description, icon,
@@ -79,7 +93,7 @@ export default class Step extends React.Component {
7993
status = 'wait', iconPrefix, icon, wrapperStyle,
8094
adjustMarginRight, stepNumber,
8195
description, title, progressDot, tailContent,
82-
icons,
96+
icons, stepIndex, onStepClick, onClick,
8397
...restProps,
8498
} = this.props;
8599

@@ -96,8 +110,18 @@ export default class Step extends React.Component {
96110
if (adjustMarginRight) {
97111
stepItemStyle.marginRight = adjustMarginRight;
98112
}
113+
114+
const accessibilityProps = {};
115+
if (onStepClick) {
116+
accessibilityProps.role = 'button';
117+
accessibilityProps.tabIndex = 0;
118+
accessibilityProps.onClick = this.onClick;
119+
}
120+
99121
return (
100122
<div
123+
onClick={onClick}
124+
{...accessibilityProps}
101125
{...restProps}
102126
className={classString}
103127
style={stepItemStyle}

src/Steps.jsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default class Steps extends Component {
2727
finish: PropTypes.node,
2828
error: PropTypes.node,
2929
}),
30+
onChange: PropTypes.func,
3031
};
3132
static defaultProps = {
3233
prefixCls: 'rc-steps',
@@ -66,6 +67,14 @@ export default class Steps extends Component {
6667
this.calcStepOffsetWidth.cancel();
6768
}
6869
}
70+
71+
onStepClick = (current) => {
72+
const { onChange } = this.props;
73+
if (onChange) {
74+
onChange(current);
75+
}
76+
};
77+
6978
calcStepOffsetWidth = () => {
7079
if (isFlexSupported()) {
7180
return;
@@ -92,7 +101,7 @@ export default class Steps extends Component {
92101
const {
93102
prefixCls, style = {}, className, children, direction,
94103
labelPlacement, iconPrefix, status, size, current, progressDot, initial,
95-
icons,
104+
icons, onChange,
96105
...restProps,
97106
} = this.props;
98107
const { lastStepOffsetWidth, flexSupported } = this.state;
@@ -116,11 +125,13 @@ export default class Steps extends Component {
116125
const stepNumber = initial + index;
117126
const childProps = {
118127
stepNumber: `${stepNumber + 1}`,
128+
stepIndex: stepNumber,
119129
prefixCls,
120130
iconPrefix,
121131
wrapperStyle: style,
122132
progressDot,
123133
icons,
134+
onStepClick: onChange && this.onStepClick,
124135
...child.props,
125136
};
126137
if (!flexSupported && direction !== 'vertical' && index !== lastIndex) {

tests/index.test.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { render } from 'enzyme';
2+
import { mount, render } from 'enzyme';
33
import Steps, { Step } from '../src';
44

55
describe('Steps', () => {
@@ -166,4 +166,18 @@ describe('Steps', () => {
166166
expect(wrapper).toMatchSnapshot();
167167
});
168168
});
169+
170+
it('onChange', () => {
171+
const onChange = jest.fn();
172+
const wrapper = mount(
173+
<Steps onChange={onChange}>
174+
<Step />
175+
<Step />
176+
<Step />
177+
</Steps>
178+
);
179+
180+
wrapper.find(Step).at(1).simulate('click');
181+
expect(onChange).toBeCalledWith(1);
182+
});
169183
});

0 commit comments

Comments
 (0)