-
Notifications
You must be signed in to change notification settings - Fork 11
/
PinInput.js
173 lines (156 loc) · 3.69 KB
/
PinInput.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
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
/**
* @author Luke Brandon Farrell
* @description Pin Input component with shake animation
*/
import React, { Component } from "react";
import { View, Animated, StyleSheet, Vibration } from "react-native";
import PropTypes from "prop-types";
class PinInput 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)
};
}
/**
* [ Built-in React method. ]
*
* Executed when the component is mounted to the screen.
*/
componentDidMount() {
this.props.onRef(this);
}
/**
* [ Built-in React method. ]
*
* Executed when the component is unmounted from the screen
*/
componentWillUnmount() {
this.props.onRef(undefined);
}
/**
* [ Built-in React method. ]
*
* Allows us to render JSX to the screen
*/
render() {
/** Styles */
const {
containerDefaultStyle,
pinDefaultStyle,
pinActiveDefaultStyle
} = styles;
/** Props */
const {
numberOfPinsActive,
numberOfPins,
// Style Props
containerStyle,
pinStyle,
pinActiveStyle
} = this.props;
/** State */
const { shake } = this.state;
// Create the pins from set props
const pins = [];
for (let p = 0; p < numberOfPins; p++) {
pins.push(
<View
key={p}
style={[
pinDefaultStyle,
pinStyle,
p < numberOfPinsActive
? { ...pinActiveDefaultStyle, ...pinActiveStyle }
: {}
]}
/>
);
}
// 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,
{ left: 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 }).start(() => {
if (this.props.animationShakeCallback) {
this.props.animationShakeCallback();
}
});
}
}
PinInput.propTypes = {
onRef: PropTypes.any.isRequired,
numberOfPins: PropTypes.number,
numberOfPinsActive: PropTypes.number,
vibration: PropTypes.bool,
animationShakeCallback: PropTypes.func,
// Style props
containerStyle: PropTypes.object,
pinStyle: PropTypes.object,
pinActiveStyle: PropTypes.object
};
PinInput.defaultProps = {
// Number of pins to create
numberOfPins: 5,
// Active number of pins
numberOfPinsActive: 0,
// Is vibration enabled or disabled
vibration: true
};
export default PinInput;
/** -------------------------------------------- */
/** 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: null,
flexDirection: "row",
alignItems: "center",
paddingTop: 25,
paddingBottom: 25
},
pinDefaultStyle: {
width: 18,
height: 18,
marginRight: 15,
marginLeft: 15,
borderRadius: 9,
opacity: 0.45,
backgroundColor: "#FFF"
},
pinActiveDefaultStyle: {
opacity: 1.0
}
});