UNPKG

@pakenfit/react-native-pin-input

Version:
87 lines (86 loc) 4.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Input = void 0; var _react = _interopRequireWildcard(require("react")); var _colors = require("../theme/colors"); var _reactNative = require("react-native"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); } const Input = exports.Input = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => { let { placeholder = '0', style, onChangeText, onKeyPress, ...rest } = _ref; const innerRef = (0, _react.useRef)(null); const [value, setValue] = (0, _react.useState)(''); const [innerPlaceholder, setInnerPlaceholder] = (0, _react.useState)(placeholder); (0, _react.useImperativeHandle)(ref, () => innerRef.current); const onFocus = (0, _react.useCallback)(() => setInnerPlaceholder(''), []); const onBlur = (0, _react.useCallback)(() => { if (!(value !== null && value !== void 0 && value.length)) { setInnerPlaceholder(placeholder); } }, [placeholder, value === null || value === void 0 ? void 0 : value.length]); const handleChangeText = (0, _react.useCallback)(text => { const regex = /^\d+$/; if (regex.test(text)) { setValue(text); onChangeText === null || onChangeText === void 0 || onChangeText(text); } else { var _innerRef$current; (_innerRef$current = innerRef.current) === null || _innerRef$current === void 0 || _innerRef$current.setNativeProps({ text: '' }); } }, [onChangeText]); const handleKeyPress = (0, _react.useCallback)(event => { if (event.nativeEvent.key === 'Backspace') { var _innerRef$current2, _innerRef$current3; setValue(''); (_innerRef$current2 = innerRef.current) === null || _innerRef$current2 === void 0 || _innerRef$current2.setNativeProps({ text: '' }); if (!(innerRef !== null && innerRef !== void 0 && (_innerRef$current3 = innerRef.current) !== null && _innerRef$current3 !== void 0 && _innerRef$current3.isFocused())) { var _innerRef$current4; (_innerRef$current4 = innerRef.current) === null || _innerRef$current4 === void 0 || _innerRef$current4.setNativeProps({ placeholder: innerPlaceholder }); } } onKeyPress === null || onKeyPress === void 0 || onKeyPress(event); }, [innerPlaceholder, onKeyPress]); return /*#__PURE__*/_react.default.createElement(_reactNative.TextInput, _extends({ ref: innerRef }, rest, { placeholderTextColor: _colors.Colors.BLACKMATTE, style: [styles.container, style], placeholder: innerPlaceholder, onFocus: onFocus, onBlur: onBlur, onChangeText: handleChangeText, maxLength: 1, onKeyPress: handleKeyPress })); }); const styles = _reactNative.StyleSheet.create({ container: { backgroundColor: _colors.Colors.WHITE, alignItems: 'center', justifyContent: 'center', fontSize: 30, fontWeight: 'normal', borderWidth: 1, borderColor: _colors.Colors.SPANISHGRAY, borderRadius: 8, height: 71, width: 80, textAlign: 'center' } }); //# sourceMappingURL=Input.js.map