Skip to content

Commit

Permalink
Merge pull request #148 from mmarkelov/Implement_preserveValues_feature
Browse files Browse the repository at this point in the history
Implement preserveValues feature
  • Loading branch information
glennreyes authored Jul 10, 2019
2 parents ff22770 + 3843c5c commit 7366ee4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Click [here](https://github.com/glennreyes/react-countup/tree/d0002932dac8a274f9
- [`end: number`](#end-number)
- [`prefix: string`](#prefix-string)
- [`redraw: boolean`](#redraw-boolean)
- [`preserveValue: boolean`](#preservevalue-boolean)
- [`separator: string`](#separator-string)
- [`start: number`](#start-number)
- [`suffix: string`](#suffix-string)
Expand Down Expand Up @@ -233,6 +234,12 @@ Forces count up transition on every component update.

Default: `false`

#### `preserveValue: boolean`

Save previously ended number to start every new animation from it.

Default: `false`

#### `separator: string`

Specifies character of thousands separator.
Expand Down
8 changes: 6 additions & 2 deletions src/CountUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CountUp extends Component {
suffix: PropTypes.string,
style: PropTypes.object,
useEasing: PropTypes.bool,
preserveValue: PropTypes.bool,
};

static defaultProps = {
Expand All @@ -41,6 +42,7 @@ class CountUp extends Component {
suffix: '',
style: undefined,
useEasing: true,
preserveValue: false,
};

componentDidMount() {
Expand Down Expand Up @@ -78,7 +80,9 @@ class CountUp extends Component {
// Only end value has changed, so reset and and re-animate with the updated
// end value.
if (this.props.end !== prevProps.end) {
this.instance.reset();
if (!this.props.preserveValue) {
this.instance.reset();
}
this.instance.update(this.props.end);
}
}
Expand All @@ -96,7 +100,7 @@ class CountUp extends Component {
warning(
this.containerRef.current &&
(this.containerRef.current instanceof HTMLElement ||
this.containerRef.current instanceof SVGTextElement),
this.containerRef.current instanceof SVGTextElement),
`Couldn't find attached element to hook the CountUp instance into! Try to attach "containerRef" from the render prop to a an HTMLElement, eg. <span ref={containerRef} />.`,
);
}
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/CountUp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ it('renders with autostart correctly', () => {
expect(container).toMatchSnapshot();
});

it('does not reset if preserveValue is true', () => {
const { container, rerender } = render(<CountUp end={10} preserveValue />);

rerender(<CountUp end={20} preserveValue />);

expect(container).toMatchSnapshot();
});

it('calls start correctly', () => {
const spy = {};

Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/__snapshots__/CountUp.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`does not reset if preserveValue is true 1`] = `
<div>
<span>
0
</span>
</div>
`;

exports[`re-renders change of duration value correctly 1`] = `
<div>
<span>
Expand Down

0 comments on commit 7366ee4

Please sign in to comment.