UNPKG

input-otp-native

Version:

One time passcode Input For React Native/Expo. Unstyled and fully customizable.

94 lines (93 loc) 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OTPInput = void 0; var React = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _useInput = require("./use-input.js"); var _jsxRuntime = require("react/jsx-runtime"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } const OTPInput = exports.OTPInput = /*#__PURE__*/React.forwardRef(({ onChange, maxLength, pattern, placeholder, inputMode = 'numeric', containerStyle, onComplete, render, ...props }, ref) => { const { inputRef, contextValue, value, handlers, actions } = (0, _useInput.useInput)({ onChange, maxLength, pattern, placeholder, defaultValue: props.defaultValue, onComplete }); React.useImperativeHandle(ref, () => ({ setValue: newValue => { handlers.onChangeText(newValue); }, focus: () => { actions.focus(); // for test only we need to call onFocus handlers.onFocus(); }, blur: () => inputRef.current?.blur(), clear: actions.clear })); const renderedChildren = React.useMemo(() => { if (render) { return render(contextValue); } return null; }, [contextValue, render]); const onPress = React.useCallback(() => { actions.focus(); actions.clear(); }, [actions]); return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.Pressable, { testID: "otp-input-container", style: [styles.container, containerStyle], onPress: onPress, children: [renderedChildren, /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TextInput, { ref: inputRef, style: styles.input, maxLength: maxLength, value: value, onChangeText: handlers.onChangeText, onFocus: handlers.onFocus, onBlur: handlers.onBlur, placeholder: placeholder, inputMode: inputMode, autoComplete: _reactNative.Platform.OS === 'android' ? 'sms-otp' : 'one-time-code', clearTextOnFocus: true, accessible: true, accessibilityRole: "text", testID: "otp-input", ...props })] }); }); OTPInput.displayName = 'OTPInput'; const styles = _reactNative.StyleSheet.create({ container: { position: 'relative' }, input: { ..._reactNative.StyleSheet.absoluteFillObject, opacity: 0, backgroundColor: 'red' } }); //# sourceMappingURL=input.js.map