Skip to content

Commit

Permalink
Add initial start value for CountUp component (#580)
Browse files Browse the repository at this point in the history
* Add initial start value for CountUp component

* Using formattingFn for initial value, fix eslint command
  • Loading branch information
mmarkelov committed Nov 7, 2021
1 parent f31808b commit 239d009
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,28 +367,28 @@ export default function App() {
}
```

### Set accessibility properties for occupation period
### Set accessibility properties for occupation period

You can use callback properties to control accessibility:

```js
import React from "react";
import CountUp, { useCountUp } from "react-countup";
import React from 'react';
import CountUp, { useCountUp } from 'react-countup';

export default function App() {
useCountUp({ ref: "counter", end: 10, duration: 2 });
useCountUp({ ref: 'counter', end: 10, duration: 2 });
const [loading, setLoading] = React.useState(false);

const onStart = () => {
setLoading(true);
};

const onEnd = () => {
setLoading(false);
};

const containerProps = {
"aria-busy": loading
'aria-busy': loading,
};

return (
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
"typings": "build/index.d.ts",
"scripts": {
"format": "prettier --write \"*.md\" \"src/**/*.ts\"",
"format": "prettier --write \"*.md\" \"src/**/*.ts\" \"src/**/*.tsx\"",
"build": "rollup -c && tsc --emitDeclarationOnly --noEmit false --project src/tsconfig.json --outDir build",
"prepublishOnly": "yarn build",
"test": "jest"
Expand Down
29 changes: 25 additions & 4 deletions src/CountUp.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import React, { CSSProperties, ReactNode, ComponentPropsWithoutRef, useEffect } from 'react';
import React, {
CSSProperties,
ReactNode,
ComponentPropsWithoutRef,
useEffect,
} from 'react';
import { CallbackProps, CommonProps, RenderCounterProps } from './types';
import { useEventCallback } from './helpers/useEventCallback';
import useCountUp from './useCountUp';
Expand All @@ -9,11 +14,18 @@ export interface CountUpProps extends CommonProps, CallbackProps {
children?: (props: RenderCounterProps) => ReactNode;
style?: CSSProperties;
preserveValue?: boolean;
containerProps?: ComponentPropsWithoutRef<"span">
containerProps?: ComponentPropsWithoutRef<'span'>;
}

const CountUp: React.FC<CountUpProps> = (props) => {
const { className, redraw, containerProps, children, style, ...useCountUpProps } = props;
const {
className,
redraw,
containerProps,
children,
style,
...useCountUpProps
} = props;
const containerRef = React.useRef<HTMLElement>(null);
const isInitializedRef = React.useRef(false);

Expand Down Expand Up @@ -111,7 +123,16 @@ const CountUp: React.FC<CountUpProps> = (props) => {
}) as JSX.Element | null;
}

return <span className={className} ref={containerRef} style={style} {...containerProps} />;
return (
<span
className={className}
ref={containerRef}
style={style}
{...containerProps}
>
{props.start ? getCountUp().formattingFn(props.start) : ''}
</span>
);
};

export default CountUp;

0 comments on commit 239d009

Please sign in to comment.