forked from Lapple/react-transitive-number
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsymbol.js
113 lines (106 loc) · 3.22 KB
/
symbol.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var React = require('react');
var D = React.createElement;
var findDOMNode = require('react-dom').findDOMNode
var createReactClass = require('create-react-class');
var Transition = require('./transition');
var transition = React.createFactory(Transition);
var Symbol = createReactClass({
getInitialState: function() {
return {
previous: null,
decrementing: false,
initialRender: true
};
},
componentDidMount: function() {
findDOMNode(this).addEventListener('transitionend', this.removePrevious);
},
componentWillUnmount: function() {
findDOMNode(this).removeEventListener('transitionend', this.removePrevious);
},
componentWillReceiveProps: function(nextProps) {
if (nextProps.symbol !== this.props.symbol) {
var decrementing = isDecrementing(this.props.symbol, nextProps.symbol);
this.setState({
previous: this.props.symbol,
decrementing: (
this.props.inverted ?
!decrementing :
decrementing
),
initialRender: false
});
}
},
shouldComponentUpdate: function(nextProps, nextState) {
return (
nextProps.symbol !== this.props.symbol ||
nextState.previous !== this.state.previous
);
},
removePrevious: function() {
this.setState({
previous: null
});
},
render: function() {
return D('span',
{
style: {
position: 'relative',
display: 'inline-block'
}
},
// Have to render this transparent spacer span to mitigate the issue
// when Safari would not animate transition if changing `position`
// from `absolute` to `static`.
this.renderSpacer(),
this.renderTransitionIn(),
this.renderTransitionOut()
);
},
renderSpacer: function() {
return D('span',
{
style: {
visibility: 'hidden'
}
},
this.props.symbol
);
},
renderTransitionIn: function() {
return transition({
value: this.props.symbol,
goingUp: this.state.decrementing,
animateEntrance: (
this.state.initialRender ?
this.props.enableInitialAnimation :
true
),
key: this.props.symbol
});
},
renderTransitionOut: function() {
if (this.state.previous !== null) {
return transition({
value: this.state.previous,
goingUp: this.state.decrementing,
out: true,
key: this.state.previous
});
}
return null;
}
});
module.exports = Symbol;
function isDecrementing(a, b) {
var numberA = Number(a);
var numberB = Number(b);
// Special case when going from 9 or 6 to 0 (and back).
if (Math.abs(numberB - numberA) !== 1) {
return numberA === 0;
} else {
return numberB < numberA;
}
}