mirror of
https://github.com/status-im/fathom.git
synced 2025-03-01 03:20:27 +00:00
animate numbers in count widget
This commit is contained in:
parent
f1a85e7d01
commit
ce00a975b4
@ -17,7 +17,7 @@ class CountWidget extends Component {
|
|||||||
super(props)
|
super(props)
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
value: '-',
|
value: 0.00,
|
||||||
loading: false,
|
loading: false,
|
||||||
before: props.before,
|
before: props.before,
|
||||||
after: props.after,
|
after: props.after,
|
||||||
@ -32,11 +32,35 @@ class CountWidget extends Component {
|
|||||||
this.setState({
|
this.setState({
|
||||||
before: newProps.before,
|
before: newProps.before,
|
||||||
after: newProps.after,
|
after: newProps.after,
|
||||||
value: '-',
|
|
||||||
});
|
});
|
||||||
this.fetchData();
|
this.fetchData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@bind
|
||||||
|
countUp(toValue) {
|
||||||
|
const duration = 1000;
|
||||||
|
const easeOutQuint = function (t) { return 1+(--t)*t*t*t*t };
|
||||||
|
const setState = this.setState.bind(this);
|
||||||
|
const startValue = this.state.value;
|
||||||
|
const diff = toValue - startValue;
|
||||||
|
let startTime;
|
||||||
|
|
||||||
|
const tick = function(t) {
|
||||||
|
if(!startTime) { startTime = t; }
|
||||||
|
let progress = ( t - startTime ) / duration;
|
||||||
|
let newValue = Math.round(startValue + (easeOutQuint(progress) * diff));
|
||||||
|
setState({
|
||||||
|
value: newValue,
|
||||||
|
})
|
||||||
|
|
||||||
|
if(progress < 1) {
|
||||||
|
window.requestAnimationFrame(tick);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.requestAnimationFrame(tick);
|
||||||
|
}
|
||||||
|
|
||||||
@bind
|
@bind
|
||||||
fetchData() {
|
fetchData() {
|
||||||
this.setState({ loading: true })
|
this.setState({ loading: true })
|
||||||
@ -50,33 +74,37 @@ class CountWidget extends Component {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(this.props.format) {
|
|
||||||
case "percentage":
|
|
||||||
d = numbers.formatPercentage(d)
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
case "number":
|
|
||||||
d = numbers.formatWithComma(d)
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "duration":
|
|
||||||
d = numbers.formatDuration(d)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
loading: false,
|
loading: false,
|
||||||
value: d,
|
|
||||||
})
|
})
|
||||||
|
this.countUp(d);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
render(props, state) {
|
render(props, state) {
|
||||||
|
let formattedValue = "-";
|
||||||
|
|
||||||
|
if(state.value > 0) {
|
||||||
|
switch(props.format) {
|
||||||
|
case "percentage":
|
||||||
|
formattedValue = numbers.formatPercentage(state.value)
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
case "number":
|
||||||
|
formattedValue = numbers.formatWithComma(state.value)
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "duration":
|
||||||
|
formattedValue = numbers.formatDuration(state.value)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class={"totals-detail " + ( state.loading ? "loading" : '')}>
|
<div class={"totals-detail " + ( state.loading ? "loading" : '')}>
|
||||||
<div class="total-heading">{props.title}</div>
|
<div class="total-heading">{props.title}</div>
|
||||||
<div class="total-numbers">{state.value}</div>
|
<div class="total-numbers">{formattedValue}</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user