@brainsbeards/react-native-animated-code-input
Version:
Animated code input component for React Native, with support for iOS, Android, and React Native Web. It works with one-time code autofill on iOS and Android.
61 lines (60 loc) • 2.58 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_native_1 = require("react-native");
const NON_NUMBER_REGEX = /[^0-9]/g;
const InputField = (props) => {
const { autoFocus, codeMaxLength, value, onBlur, onChangeText, onSubmit, testID, textContentType, textInputRef, } = props;
const onChangeTextCallback = react_1.useCallback((text) => {
const numbersFromText = text.replace(NON_NUMBER_REGEX, "");
const codeChanged = numbersFromText !== value;
if (onChangeText) {
onChangeText(numbersFromText);
}
if (codeChanged) {
if (numbersFromText.length === codeMaxLength) {
react_native_1.Keyboard.dismiss();
}
}
}, [codeMaxLength, value, onChangeText]);
const onBlurCallback = react_1.useCallback((e) => {
react_native_1.InteractionManager.runAfterInteractions(() => {
if (onSubmit && value.length === codeMaxLength) {
onSubmit();
}
});
if (onBlur) {
onBlur(e);
}
}, [onSubmit, onBlur]);
return (react_1.default.createElement(react_native_1.TextInput, { autoFocus: autoFocus || true, caretHidden: true, keyboardType: "number-pad", onBlur: onBlurCallback, onChangeText: onChangeTextCallback, maxLength: codeMaxLength, ref: textInputRef, style: styles.input, testID: testID, textContentType: textContentType ? textContentType : "oneTimeCode", value: value }));
};
const styles = react_native_1.StyleSheet.create({
input: {
fontSize: 1,
height: 1,
margin: 0,
opacity: 0,
padding: 0,
},
});
exports.default = InputField;
;