Skip to content

Commit

Permalink
Merge pull request #11 from slorenzo/add/auto-start-prop
Browse files Browse the repository at this point in the history
Added autoStart property
  • Loading branch information
Sebastián Lorenzo authored Jan 23, 2019
2 parents 00a4ad1 + 039021a commit 24808d0
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@
## v2.0.2

* Added onChange property

## v2.0.3

* Added autoStart property
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default Stopwatch;
- `hours`: Integer. Needs to be `0 >= hours`. (Required)
- `limit`: String. Need to have the following format `XX:XX:XX`. (Optional)
- `withLoop`: Boolean. If it is `true` when the watch is equal to `limit`, it makes a loop. (Optional)
- `autoStart`: Boolean. Start counting time. Default: true (Optional)
- `onCallback`: Function. If you need to do something when the watch is equal to `limit`. (Optional)
- `onChange`: Function. It returns the values each second. (Optional)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-stopwatch",
"version": "2.0.2",
"version": "2.0.3",
"description": "React Stopwatch component using Render Props",
"main": "dist/react-stopwatch.js",
"umd:main": "dist/react-stopwatch.umd.js",
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`ReactStopwatch It renders without breaking using render as children ele
ShallowWrapper {
Symbol(enzyme.__root__): [Circular],
Symbol(enzyme.__unrendered__): <ReactStopWatch
autoStart={true}
hours={0}
limit="00:00:10"
minutes={0}
Expand Down Expand Up @@ -67,6 +68,7 @@ exports[`ReactStopwatch It renders without breaking using render as property 1`]
ShallowWrapper {
Symbol(enzyme.__root__): [Circular],
Symbol(enzyme.__unrendered__): <ReactStopWatch
autoStart={true}
hours={0}
limit="00:00:10"
minutes={0}
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ describe('ReactStopwatch', () => {
}, ms('1.1s'));
});

it('should not auto start', (done) => {
const wrapper = shallow(
<Stopwatch
seconds={0}
minutes={0}
hours={0}
autoStart={false}
limit="00:00:10"
render={noop}
/>,
);

expect(wrapper.state().stateHours).toBe(0);
expect(wrapper.state().stateHours).toBe(0);
expect(wrapper.state().stateMinutes).toBe(0);

setTimeout(() => {
expect(wrapper.state().stateSeconds).toBe(0);
done();
}, ms('1.1s'));
});

it('should call to onCallback function', (done) => {
const handleCallback = () => {
expect(true).toBe(true);
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ReactStopWatch extends PureComponent<Props, State> {
static displayName = 'ReactStopWatch';

static defaultProps = {
autoStart: true,
withLoop: false,
onCallback: () => {},
onChange: () => {},
Expand Down Expand Up @@ -46,7 +47,10 @@ class ReactStopWatch extends PureComponent<Props, State> {
}

componentDidMount(): void {
this.timer();
const { autoStart } = this.props;
if (autoStart) {
this.timer();
}
}

componentWillUnmount(): void {
Expand Down
1 change: 1 addition & 0 deletions src/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type Props = {
minutes: number,
hours: number,
limit?: string,
autoStart?: boolean,
withLoop?: boolean,
onChange?: () => void,
onCallback: () => void,
Expand Down

0 comments on commit 24808d0

Please sign in to comment.