-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPins.js
More file actions
347 lines (312 loc) · 8.77 KB
/
Pins.js
File metadata and controls
347 lines (312 loc) · 8.77 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* @author Luke Brandon Farrell
* @description Pins component with shake animation
*/
import React, { Component } from "react";
import { Animated, StyleSheet, Vibration } from "react-native";
import PropTypes from "prop-types";
class Pins extends Component {
/**
* [ Built-in React method. ]
*
* Allows us to render JSX to the screen
*/
constructor(props) {
super(props);
this.state = {
shake: new Animated.Value(0),
animatedPinValue: new Animated.Value(0),
positionOfActivePin: 1,
prevPinSizes: [],
currentPinSizes: [],
};
this.setPositionOfPins = this.setPositionOfPins.bind(this);
this.getPinSize = this.getPinSize.bind(this);
this.updatePinSizes = this.updatePinSizes.bind(this);
}
/**
* [ Built-in React method. ]
*
* Executed when the component is mounted to the screen.
*/
componentDidMount() {
if (this.props.onRef != undefined) {
this.props.onRef(this);
}
// Get and set the initial pin sizes
const { numberOfPins } = this.props;
let initialPinSizes = [];
for (let p = 0; p < numberOfPins; p++) {
let size = this.getPinSize(p);
initialPinSizes[p] = size;
}
this.setState({
prevPinSizes: initialPinSizes,
currentPinSizes: initialPinSizes,
});
}
/**
* [ Built-in React method. ]
*
* Executed when the component is unmounted from the screen
*/
componentWillUnmount() {
if (this.props.onRef != undefined) {
this.props.onRef(undefined);
}
}
/**
* [ Built-in React method. ]
*
* Executed when there is any changes to the props or state
*/
componentDidUpdate(prevProps) {
const prevNumberOfPinsActive = prevProps.numberOfPinsActive;
const { numberOfPinsActive } = this.props;
if (prevNumberOfPinsActive != numberOfPinsActive) {
this.setPositionOfPins(prevNumberOfPinsActive).then(() => {
this.updatePinSizes();
// Reset animation to so we can reanimate the pins
this.state.animatedPinValue.setValue(0);
Animated.timing(this.state.animatedPinValue, {
duration: 300,
toValue: 1,
useNativeDriver: false,
}).start();
});
}
}
shouldComponentUpdate(nextProps, nextState) {
return (
this.state.currentPinSizes != nextState.currentPinSizes ||
this.props.numberOfPinsActive != nextProps.numberOfPinsActive
);
}
/**
* [ Built-in React method. ]
*
* Allows us to render JSX to the screen
*/
render() {
/** Styles */
const {
containerDefaultStyle,
pinDefaultStyle,
pinActiveDefaultStyle,
} = styles;
/** Props */
const {
numberOfPinsActive,
numberOfPins,
numberOfPinsMaximum,
// Style Props
containerStyle,
pinStyle,
pinActiveStyle,
activeOnly,
pinSize,
spacing,
} = this.props;
/** State */
const {
shake,
animatedPinValue,
prevPinSizes,
currentPinSizes,
} = this.state;
// Create the pins from set props
const pins = [];
const hasMaxNumberOfPins =
numberOfPinsMaximum < numberOfPins &&
numberOfPinsMaximum > 0 &&
numberOfPins;
for (let p = 0; p < numberOfPins; p++) {
const prevSize = prevPinSizes[p];
const currentSize = currentPinSizes[p];
const currentSizeIsMoreThanZero = hasMaxNumberOfPins ? currentSize > 0 : true;
const size = hasMaxNumberOfPins
? animatedPinValue.interpolate({
inputRange: [0, 1],
outputRange: [prevSize, currentSize],
})
: pinSize;
pins.push(
<Animated.View
key={p}
style={[
pinDefaultStyle,
pinStyle,
activeOnly
? p == numberOfPinsActive - 1
? { ...pinActiveDefaultStyle, ...pinActiveStyle }
: {}
: p < numberOfPinsActive
? { ...pinActiveDefaultStyle, ...pinActiveStyle }
: {},
pinSize && {
width: size,
height: size,
borderRadius: size,
},
spacing &&
currentSizeIsMoreThanZero && {
marginRight: spacing,
marginLeft: spacing,
},
]}
/>
);
}
// Create the shake animation via interpolation
const shakeAnimation = shake.interpolate({
inputRange: [0, 0.2, 0.4, 0.6, 0.8, 1],
outputRange: [0, -20, 20, -20, 20, 0],
});
return (
<Animated.View
style={[
containerDefaultStyle,
containerStyle,
{
transform: [{ translateX: shakeAnimation }],
},
]}
>
{pins}
</Animated.View>
);
}
/**
* Shakes the pins
*/
shake() {
// If vibration is enabled then we vibrate on error
if (this.props.vibration) {
Vibration.vibrate(500);
}
// Reset animation to so we can reanimate
this.state.shake.setValue(0);
// Animate the pins to shake
Animated.spring(this.state.shake, {
toValue: 1,
useNativeDriver: true,
}).start(() => {
if (this.props.animationShakeCallback) {
this.props.animationShakeCallback();
}
});
}
/**
* Sets the position of the active pin among the large pins.
* @param {Number} prevNumberOfPinsActive The index of the previous active pin
*/
async setPositionOfPins(prevNumberOfPinsActive) {
const { numberOfPinsMaximum, numberOfPinsActive } = this.props;
const { positionOfActivePin } = this.state;
// If index of pin increases
if (numberOfPinsActive > prevNumberOfPinsActive) {
// If the position of the pin is not at the right-end, we increase the position of the active pin among the large pins
if (positionOfActivePin != numberOfPinsMaximum) {
await this.setState({ positionOfActivePin: positionOfActivePin + 1 });
}
// If index of pin decreases
} else {
// If the position of the pin is not at the left-end, we decrease the position of the active pin among the large pins
if (positionOfActivePin != 1) {
await this.setState({ positionOfActivePin: positionOfActivePin - 1 });
}
}
}
/**
* Updates the sizes of all the pins depending on the number of pins active.
*/
updatePinSizes() {
const { currentPinSizes } = this.state;
let updatedPinSizes = [];
this.setState({ prevPinSizes: currentPinSizes }, () => {
currentPinSizes.map((prevSize, idx) => {
let size = this.getPinSize(idx);
updatedPinSizes[idx] = size;
});
});
this.setState({ currentPinSizes: updatedPinSizes });
}
/**
* Returns size of an individual pin.
* @param {number} idx Index of the pin
*/
getPinSize(idx) {
const { numberOfPinsMaximum, numberOfPinsActive, pinSize } = this.props;
const { positionOfActivePin } = this.state;
if (
idx > numberOfPinsActive - positionOfActivePin - 1 &&
idx < numberOfPinsActive - positionOfActivePin + numberOfPinsMaximum
) {
return pinSize;
}
if (
idx == numberOfPinsActive - positionOfActivePin - 1 ||
idx == numberOfPinsActive - positionOfActivePin + numberOfPinsMaximum
) {
return pinSize / 2;
}
if (
idx == numberOfPinsActive - positionOfActivePin - 2 ||
idx == numberOfPinsActive - positionOfActivePin + numberOfPinsMaximum + 1
) {
return pinSize / 4;
}
return 0;
}
}
Pins.propTypes = {
onRef: PropTypes.any,
numberOfPins: PropTypes.number,
numberOfPinsActive: PropTypes.number,
vibration: PropTypes.bool,
animationShakeCallback: PropTypes.func,
numberOfPinsMaximum: PropTypes.number,
activeOnly: PropTypes.bool,
// Style props
containerStyle: PropTypes.object,
pinStyle: PropTypes.object,
pinActiveStyle: PropTypes.object,
pinSize: PropTypes.number,
spacing: PropTypes.number,
};
Pins.defaultProps = {
// Number of pins to create
numberOfPins: 5,
// Active number of pins
numberOfPinsActive: 0,
// Is vibration enabled or disabled
vibration: true,
// Default pin size
pinSize: 18,
// Default spacing between pins
spacing: 15,
};
export default Pins;
/** -------------------------------------------- */
/** Component Styling */
/** -------------------------------------------- */
const styles = StyleSheet.create({
// Style for pin container. You can use the flex
// property to expand the pins to take up more space
// on the screen. The default is 0.8.
containerDefaultStyle: {
flex: 1,
flexDirection: "row",
alignItems: "center",
paddingTop: 25,
paddingBottom: 25,
},
pinDefaultStyle: {
borderRadius: 9,
opacity: 0.45,
backgroundColor: "#FFF",
},
pinActiveDefaultStyle: {
opacity: 1.0,
},
});