| 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 |
1×
1×
6×
1×
1×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| 'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _class, _temp;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _cssAnimation = require('css-animation');
var _framework = require('../framework');
var _framework2 = _interopRequireDefault(_framework);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function getNumberArray(num) {
return num ? num.toString().split('').reverse().map(function (i) {
return Number(i);
}) : [];
}
exports.default = _framework2.default.Component((_temp = _class = function (_React$Component) {
_inherits(_class, _React$Component);
function _class(props) {
_classCallCheck(this, _class);
var _this = _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this, props));
_this.state = {
animateStarted: true,
count: props.count
};
return _this;
}
_createClass(_class, [{
key: 'getPositionByNum',
value: function getPositionByNum(num, i) {
if (this.state.animateStarted) {
return 10 + num;
}
var currentDigit = getNumberArray(this.state.count)[i];
var lastDigit = getNumberArray(this.lastCount)[i];
// 同方向则在同一侧切换数字
if (this.state.count > this.lastCount) {
if (currentDigit >= lastDigit) {
return 10 + num;
}
return 20 + num;
}
if (currentDigit <= lastDigit) {
return 10 + num;
}
return num;
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
var _this2 = this;
if ('count' in nextProps) {
if (this.state.count === nextProps.count) {
return;
}
this.lastCount = this.state.count;
// 复原数字初始位置
this.setState({
animateStarted: true
}, function () {
// 等待数字位置复原完毕
// 开始设置完整的数字
setTimeout(function () {
_this2.setState({
animateStarted: false,
count: nextProps.count
}, function () {
_this2.props.onAnimated();
});
}, 5);
});
}
}
}, {
key: 'renderNumberList',
value: function renderNumberList() {
var childrenToReturn = [];
for (var i = 0; i < 30; i++) {
childrenToReturn.push(_react2.default.createElement(
'p',
{ key: i, style: this.props.styles.number },
i % 10
));
}
return childrenToReturn;
}
}, {
key: 'renderCurrentNumber',
value: function renderCurrentNumber(num, i) {
var position = this.getPositionByNum(num, i);
var height = this.props.height;
var removeTransition = this.state.animateStarted || getNumberArray(this.lastCount)[i] === undefined;
return (0, _react.createElement)('span', {
key: i,
style: Object.assign({
transition: removeTransition && 'none',
WebkitTransform: 'translate3d(0, ' + -position * height + 'px, 0)',
transform: 'translate3d(0, ' + -position * height + 'px, 0)',
height: height
}, this.props.styles.only)
}, this.renderNumberList());
}
}, {
key: 'renderNumberElement',
value: function renderNumberElement() {
var _this3 = this;
var state = this.state;
if (!state.count || isNaN(state.count)) {
return state.count;
}
return getNumberArray(state.count).map(function (num, i) {
return _this3.renderCurrentNumber(num, i);
}).reverse();
}
}, {
key: 'render',
value: function render() {
var props = _extends({}, this.props, {
className: this.props.className
});
if (props.count) {
props.style = Object.assign(this.props.styles.container, this.props.styles.count);
} else {
props.style = Object.assign(this.props.styles.container, this.props.styles.dot);
}
if (!props.isWrapper) {
props.style = Object.assign(props.style, this.props.styles.isNotWrapper);
}
var isBrowser = typeof document !== 'undefined' && typeof window !== 'undefined';
if (isBrowser && _cssAnimation.isCssAnimationSupported) {
return (0, _react.createElement)(this.props.component, props, this.renderNumberElement());
}
return (0, _react.createElement)(this.props.component, props, props.count);
}
}]);
return _class;
}(_react2.default.Component), _class.label = 'scroll-number', _class.propTypes = {
count: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.number]),
isWrapper: _react2.default.PropTypes.bool,
component: _react2.default.PropTypes.string,
onAnimated: _react2.default.PropTypes.func,
height: _react2.default.PropTypes.number
}, _class.defaultProps = {
count: null,
isWrapper: false,
component: 'sup',
onAnimated: function onAnimated() {},
height: 18
}, _class.styles = function (config) {
return {
container: {
overflow: 'hidden'
},
only: {
display: 'inline-block',
transition: config.transition
},
number: {
fontSize: '12px',
fontFamily: 'tahoma'
},
count: {
position: 'absolute',
transform: 'translateX(-50%)',
top: '-10px',
height: '20px',
borderRadius: '10px',
minWidth: '20px',
background: config.colors.error,
border: '1px solid transparent',
color: '#fff',
lineHeight: '18px',
textAlign: 'center',
padding: '0 6px',
whiteSpace: 'nowrap',
transformOrigin: '-10% center',
zIndex: 10,
boxShadow: '0 0 0 1px #fff',
cursor: 'pointer'
},
dot: {
position: 'absolute',
transform: 'translateX(-50%)',
transformOrigin: '0px center',
top: '-4px',
height: '8px',
width: '8px',
borderRadius: '100%',
background: config.colors.error,
zIndex: 10,
boxShadow: '0 0 0 1px #fff',
cursor: 'pointer'
},
isNotWrapper: {
top: 'auto',
display: 'block',
position: 'relative',
transform: 'translateX(0)'
}
};
}, _temp)); |