|
| 1 | +/** |
| 2 | + * @author Luke Brandon Farrell |
| 3 | + * @description Keyboard component with message and interactive keys |
| 4 | + */ |
| 5 | + |
| 6 | +import React, { Component } from "react"; |
| 7 | +import { View, Image, Text, StyleSheet, Platform, Vibration } from "react-native"; |
| 8 | +import Ripple from "react-native-material-ripple"; |
| 9 | +import PropTypes from "prop-types"; |
| 10 | + |
| 11 | +const backAsset = require("./back.png"); |
| 12 | + |
| 13 | +class PinKeyboard extends Component { |
| 14 | + /** |
| 15 | + * [ Built-in React method. ] |
| 16 | + * |
| 17 | + * Setup the component. Executes when the component is created |
| 18 | + * |
| 19 | + * @param {object} props |
| 20 | + * |
| 21 | + */ |
| 22 | + constructor(props) { |
| 23 | + super(props); |
| 24 | + |
| 25 | + this.state = { |
| 26 | + disabled: false, |
| 27 | + message: null |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * [ Built-in React method. ] |
| 33 | + * |
| 34 | + * Executed when the component is mounted to the screen. |
| 35 | + */ |
| 36 | + componentDidMount() { |
| 37 | + this.props.onRef(this); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * [ Built-in React method. ] |
| 42 | + * |
| 43 | + * Executed when the component is unmounted from the screen |
| 44 | + */ |
| 45 | + componentWillUnmount() { |
| 46 | + this.props.onRef(undefined); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * [ Built-in React method. ] |
| 51 | + * |
| 52 | + * Allows us to render JSX to the screen |
| 53 | + */ |
| 54 | + render() { |
| 55 | + /** Styles */ |
| 56 | + const { containerStyle, keyboardDefaultStyle, keyboardRowStyle } = styles; |
| 57 | + /** Props */ |
| 58 | + const { |
| 59 | + keyboard, |
| 60 | + keyboardCustomKeyImage, |
| 61 | + // Style Props |
| 62 | + keyboardStyle |
| 63 | + } = this.props; |
| 64 | + /** Variables */ |
| 65 | + // Keyboard configuration. The default contains a key |
| 66 | + // for each number 0 - 9 and a back button. |
| 67 | + const defaultKeyboard = keyboard ? |
| 68 | + keyboard : [[1, 2, 3], [4, 5, 6], [7, 8, 9], [keyboardCustomKeyImage, 0, backAsset]]; |
| 69 | + |
| 70 | + return ( |
| 71 | + <View style={containerStyle}> |
| 72 | + {this.renderMessage()} |
| 73 | + <View style={[keyboardDefaultStyle, keyboardStyle]}> |
| 74 | + {// Maps each array of numbers in the keyboardValues array |
| 75 | + defaultKeyboard.map((row, r) => { |
| 76 | + return ( |
| 77 | + <View key={r} style={keyboardRowStyle}> |
| 78 | + {// Maps each number in row and creates key for that number |
| 79 | + row.map((element, k) => { |
| 80 | + return this.renderKey(element, r, k); |
| 81 | + })} |
| 82 | + </View> |
| 83 | + ); |
| 84 | + })} |
| 85 | + </View> |
| 86 | + </View> |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Renders the message |
| 92 | + * |
| 93 | + * @return {*} |
| 94 | + */ |
| 95 | + renderMessage() { |
| 96 | + // Styles |
| 97 | + const { messageDefaultStyle, messageTextDefaultStyle } = styles; |
| 98 | + |
| 99 | + // Props |
| 100 | + const { |
| 101 | + // Style Props |
| 102 | + messageStyle, |
| 103 | + messageTextStyle |
| 104 | + } = this.props; |
| 105 | + |
| 106 | + // State |
| 107 | + const { message } = this.state; |
| 108 | + |
| 109 | + if (message) { |
| 110 | + return ( |
| 111 | + <View style={[messageDefaultStyle, messageStyle]}> |
| 112 | + <Text style={[messageTextDefaultStyle, messageTextStyle]}>{message}</Text> |
| 113 | + </View> |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Renders a key on the keyboard |
| 122 | + * |
| 123 | + * @param entity |
| 124 | + * @param row |
| 125 | + * @param column |
| 126 | + * |
| 127 | + * @return {jsx} |
| 128 | + */ |
| 129 | + renderKey(entity, row, column) { |
| 130 | + /** Styles */ |
| 131 | + const { |
| 132 | + keyContainerStyle, |
| 133 | + keyboardDisabledDefaultStyle, |
| 134 | + keyDefaultStyle, |
| 135 | + keyTextDefaultStyle, |
| 136 | + keyImageDefaultStyle |
| 137 | + } = styles; |
| 138 | + /** Props */ |
| 139 | + const { |
| 140 | + keyDown, |
| 141 | + keyboardFunc, |
| 142 | + keyboardDisabledStyle, |
| 143 | + vibration, |
| 144 | + // Style Props |
| 145 | + keyStyle, |
| 146 | + keyTextStyle, |
| 147 | + keyImageStyle |
| 148 | + } = this.props; |
| 149 | + /** State */ |
| 150 | + const { disabled } = this.state; |
| 151 | + |
| 152 | + // Custom functions for the keyboard key |
| 153 | + const keyboardFuncSet = keyboardFunc |
| 154 | + ? keyboardFunc |
| 155 | + : [ |
| 156 | + [null, null, null], |
| 157 | + [null, null, null], |
| 158 | + [null, null, null], |
| 159 | + [() => this.props.keyDown("custom"), null, () => this.props.keyDown("back")] |
| 160 | + ]; |
| 161 | + |
| 162 | + // Decide if the element passed as the key is text |
| 163 | + const keyJsx = keyboardFuncSet[row][column] ? ( |
| 164 | + <Image style={[keyImageDefaultStyle, keyImageStyle]} source={entity} /> |
| 165 | + ) : ( |
| 166 | + <Text style={[keyTextDefaultStyle, keyTextStyle]}>{entity}</Text> |
| 167 | + ); |
| 168 | + |
| 169 | + // We want to block keyboard interactions if it has been disabled. |
| 170 | + if (!disabled) { |
| 171 | + return ( |
| 172 | + <Ripple |
| 173 | + rippleColor={"#000"} |
| 174 | + key={column} |
| 175 | + onPressIn={() => { |
| 176 | + if(vibration) Vibration.vibrate(50); |
| 177 | + |
| 178 | + keyboardFuncSet[row][column] ? keyboardFuncSet[row][column]() : keyDown(entity) |
| 179 | + }} |
| 180 | + style={[keyContainerStyle, keyDefaultStyle, keyStyle]} |
| 181 | + > |
| 182 | + {keyJsx} |
| 183 | + </Ripple> |
| 184 | + ); |
| 185 | + } else { |
| 186 | + return ( |
| 187 | + <View |
| 188 | + key={column} |
| 189 | + style={[ |
| 190 | + keyContainerStyle, |
| 191 | + keyDefaultStyle, |
| 192 | + keyStyle, |
| 193 | + keyboardDisabledDefaultStyle, |
| 194 | + keyboardDisabledStyle |
| 195 | + ]} |
| 196 | + > |
| 197 | + {keyJsx} |
| 198 | + </View> |
| 199 | + ); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Function used to display a message above the keyboard |
| 205 | + * |
| 206 | + * @param message |
| 207 | + */ |
| 208 | + displayMessage(message) { |
| 209 | + this.setState({ |
| 210 | + message |
| 211 | + }, () => { |
| 212 | + if(this.hideMessageTimeout) clearTimeout(this.hideMessageTimeout); |
| 213 | + |
| 214 | + this.hideMessageTimeout = setTimeout(() => { |
| 215 | + this.clearMessage(); |
| 216 | + }, this.props.keyboardMessageDisplayTime); |
| 217 | + }); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Function used to clear the message on the keyboard |
| 222 | + */ |
| 223 | + clearMessage() { |
| 224 | + this.setState({ message: null }); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Function used to disable the keyboard |
| 229 | + */ |
| 230 | + disable() { |
| 231 | + this.setState({ |
| 232 | + disabled: true |
| 233 | + }); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * Function used to enable the keyboard |
| 238 | + */ |
| 239 | + enable() { |
| 240 | + this.setState({ |
| 241 | + disabled: false |
| 242 | + }); |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +PinKeyboard.propTypes = { |
| 247 | + onRef: PropTypes.any.isRequired, |
| 248 | + keyDown: PropTypes.func.isRequired, |
| 249 | + keyboard: PropTypes.array, |
| 250 | + keyboardFunc: PropTypes.array, |
| 251 | + keyboardCustomKeyImage: PropTypes.number, |
| 252 | + keyboardMessageDisplayTime: PropTypes.number, |
| 253 | + vibration: PropTypes.bool, |
| 254 | + // Style props |
| 255 | + keyboardStyle: PropTypes.object, |
| 256 | + keyboardDisabledStyle: PropTypes.object, |
| 257 | + keyStyle: PropTypes.object, |
| 258 | + keyTextStyle: PropTypes.object, |
| 259 | + keyImageStyle: PropTypes.object, |
| 260 | + messageStyle: PropTypes.object, |
| 261 | + messageTextStyle: PropTypes.object |
| 262 | +}; |
| 263 | + |
| 264 | +PinKeyboard.defaultProps = { |
| 265 | + // Keyboard functions. By default the text (number) in the |
| 266 | + // keyboard array will be passed via the keyDown callback. |
| 267 | + // Use this array to set custom functions for certain keys. |
| 268 | + keyboardFunc: null, |
| 269 | + keyboardMessageDisplayTime: 3000, |
| 270 | + vibration: false, |
| 271 | +}; |
| 272 | + |
| 273 | +const styles = StyleSheet.create({ |
| 274 | + containerStyle: { |
| 275 | + flex: null, |
| 276 | + width: "100%", |
| 277 | + flexDirection: "column", |
| 278 | + justifyContent: "flex-end" |
| 279 | + }, |
| 280 | + // Style applied to the keyboard. Must contain a height or |
| 281 | + // the keyboard will not be displayed. |
| 282 | + keyboardDefaultStyle: { |
| 283 | + height: 250, |
| 284 | + backgroundColor: "#FFF" |
| 285 | + }, |
| 286 | + keyboardRowStyle: { |
| 287 | + flex: 1, |
| 288 | + flexDirection: "row" |
| 289 | + }, |
| 290 | + keyContainerStyle: { |
| 291 | + flex: 1, |
| 292 | + flexDirection: "column", |
| 293 | + justifyContent: "center", |
| 294 | + alignItems: "center" |
| 295 | + }, |
| 296 | + // Style applied to keyboard when it is disabled. |
| 297 | + keyboardDisabledDefaultStyle: { |
| 298 | + backgroundColor: "#FFF" |
| 299 | + }, |
| 300 | + // Style the individual keys |
| 301 | + keyDefaultStyle: { |
| 302 | + backgroundColor: "#FFF", |
| 303 | + borderRightColor: "#e8e8e8", |
| 304 | + borderRightWidth: 1, |
| 305 | + borderBottomColor: "#e8e8e8", |
| 306 | + borderBottomWidth: 1 |
| 307 | + }, |
| 308 | + // Style for the text inside a key |
| 309 | + keyTextDefaultStyle: { |
| 310 | + ...Platform.select({ |
| 311 | + ios: { |
| 312 | + fontFamily: "HelveticaNeue" |
| 313 | + }, |
| 314 | + android: { |
| 315 | + fontFamily: "Roboto" |
| 316 | + } |
| 317 | + }), |
| 318 | + fontWeight: "400", |
| 319 | + fontSize: 25, |
| 320 | + textAlign: "center", |
| 321 | + color: "#222222" |
| 322 | + }, |
| 323 | + // Style for an image inside a key |
| 324 | + keyImageDefaultStyle: { |
| 325 | + width: 28, |
| 326 | + height: 28 |
| 327 | + }, |
| 328 | + messageDefaultStyle: { |
| 329 | + height: 30, |
| 330 | + width: "100%", |
| 331 | + justifyContent: "center", |
| 332 | + alignItems: "center", |
| 333 | + backgroundColor: "#e8e8e8" |
| 334 | + }, |
| 335 | + messageTextDefaultStyle: { |
| 336 | + color: "#222222", |
| 337 | + fontSize: 15, |
| 338 | + fontWeight: "bold" |
| 339 | + } |
| 340 | +}); |
| 341 | + |
| 342 | +export default PinKeyboard; |
0 commit comments