Skip to content

Commit

Permalink
feat: support onChange (#71)
Browse files Browse the repository at this point in the history
* support onChange

* update node version

* add test case

* update style
  • Loading branch information
zombieJ committed May 24, 2019
1 parent 7b3d706 commit c27ab88
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ assets/**/*.css
build
lib
es
coverage
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ notifications:
- yesmeck@gmail.com

node_js:
- node
- 10

before_install:
- |
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ online example: http://react-component.github.io/steps/examples/
<td></td>
<td>spicify the default finish icon and error icon</td>
</tr>
<tr>
<td>onChange</td>
<td>(current: number) => void</td>
<td></td>
<td>Trigger when Step changed</td>
</tr>
</tbody>
</table>

Expand Down
9 changes: 9 additions & 0 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
flex: 1;
overflow: hidden;

&[role='button'] {
cursor: pointer;
transition: all .3s;

&:hover {
opacity: 0.7;
}
}

&:last-child {
flex: none;
}
Expand Down
23 changes: 21 additions & 2 deletions examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ import Steps, { Step } from 'rc-steps';
const container = document.getElementById('__react-content');
const description = '这里是多信息的描述啊描述啊描述啊描述啊哦耶哦耶哦耶哦耶哦耶哦耶哦耶哦耶哦耶哦耶哦耶哦耶';

const ControlSteps = () => {
const [current, setCurrent] = React.useState(0);
return (
<Steps
current={current}
onChange={(val) => {
setCurrent(val);
}}
>
<Step title="已完成" />
<Step title="进行中" />
<Step title="待运行" description="Hello World!" />
<Step title="待运行" />
</Steps>
);
};

ReactDOM.render(
<div>
<Steps current={1}>
Expand All @@ -27,5 +44,7 @@ ReactDOM.render(
<Step title="待运行" description={description} />
<Step title="待运行" description={description} />
</Steps>
</div>
, container);
<ControlSteps />
</div>,
container,
);
11 changes: 11 additions & 0 deletions now.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": 2,
"name": "rc-steps",
"builds": [
{
"src": "package.json",
"use": "@now/static-build",
"config": { "distDir": "build" }
}
]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"lint:fix": "rc-tools run lint --fix",
"test": "jest",
"prepublish": "rc-tools run guard",
"coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls"
"coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls",
"now-build": "npm run build"
},
"jest": {
"setupFiles": [
Expand Down
26 changes: 25 additions & 1 deletion src/Step.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default class Step extends React.Component {
PropTypes.string,
]),
stepNumber: PropTypes.string,
stepIndex: PropTypes.number,
description: PropTypes.any,
title: PropTypes.any,
progressDot: PropTypes.oneOfType([
Expand All @@ -35,7 +36,20 @@ export default class Step extends React.Component {
finish: PropTypes.node,
error: PropTypes.node,
}),
onClick: PropTypes.func,
onStepClick: PropTypes.func,
};

onClick = (...args) => {
const { onClick, onStepClick, stepIndex } = this.props;

if (onClick) {
onClick(...args);
}

onStepClick(stepIndex);
};

renderIconNode() {
const {
prefixCls, progressDot, stepNumber, status, title, description, icon,
Expand Down Expand Up @@ -79,7 +93,7 @@ export default class Step extends React.Component {
status = 'wait', iconPrefix, icon, wrapperStyle,
adjustMarginRight, stepNumber,
description, title, progressDot, tailContent,
icons,
icons, stepIndex, onStepClick, onClick,
...restProps,
} = this.props;

Expand All @@ -96,8 +110,18 @@ export default class Step extends React.Component {
if (adjustMarginRight) {
stepItemStyle.marginRight = adjustMarginRight;
}

const accessibilityProps = {};
if (onStepClick) {
accessibilityProps.role = 'button';
accessibilityProps.tabIndex = 0;
accessibilityProps.onClick = this.onClick;
}

return (
<div
onClick={onClick}
{...accessibilityProps}
{...restProps}
className={classString}
style={stepItemStyle}
Expand Down
13 changes: 12 additions & 1 deletion src/Steps.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class Steps extends Component {
finish: PropTypes.node,
error: PropTypes.node,
}),
onChange: PropTypes.func,
};
static defaultProps = {
prefixCls: 'rc-steps',
Expand Down Expand Up @@ -66,6 +67,14 @@ export default class Steps extends Component {
this.calcStepOffsetWidth.cancel();
}
}

onStepClick = (current) => {
const { onChange } = this.props;
if (onChange) {
onChange(current);
}
};

calcStepOffsetWidth = () => {
if (isFlexSupported()) {
return;
Expand All @@ -92,7 +101,7 @@ export default class Steps extends Component {
const {
prefixCls, style = {}, className, children, direction,
labelPlacement, iconPrefix, status, size, current, progressDot, initial,
icons,
icons, onChange,
...restProps,
} = this.props;
const { lastStepOffsetWidth, flexSupported } = this.state;
Expand All @@ -116,11 +125,13 @@ export default class Steps extends Component {
const stepNumber = initial + index;
const childProps = {
stepNumber: `${stepNumber + 1}`,
stepIndex: stepNumber,
prefixCls,
iconPrefix,
wrapperStyle: style,
progressDot,
icons,
onStepClick: onChange && this.onStepClick,
...child.props,
};
if (!flexSupported && direction !== 'vertical' && index !== lastIndex) {
Expand Down
16 changes: 15 additions & 1 deletion tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render } from 'enzyme';
import { mount, render } from 'enzyme';
import Steps, { Step } from '../src';

describe('Steps', () => {
Expand Down Expand Up @@ -166,4 +166,18 @@ describe('Steps', () => {
expect(wrapper).toMatchSnapshot();
});
});

it('onChange', () => {
const onChange = jest.fn();
const wrapper = mount(
<Steps onChange={onChange}>
<Step />
<Step />
<Step />
</Steps>
);

wrapper.find(Step).at(1).simulate('click');
expect(onChange).toBeCalledWith(1);
});
});

1 comment on commit c27ab88

@vercel
Copy link

@vercel vercel bot commented on c27ab88 May 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.