react-native-advanced-input-mask
Version:
Text input mask for React Native on iOS, Android and web. Synchronous and easy formatting without hustle
115 lines • 4.42 kB
JavaScript
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import React, { forwardRef, memo, useCallback, useImperativeHandle, useRef } from "react";
import { StyleSheet, TextInput } from "react-native";
import { IS_FABRIC } from "../../architecture";
import AdvancedTextInputMaskDecoratorViewNativeComponent from "../../specs/AdvancedTextInputMaskDecoratorViewNativeComponent";
import { Commands } from "../../specs/AdvancedTextInputMaskDecoratorViewNativeComponent";
const styles = StyleSheet.create({
displayNone: {
display: "none"
},
farAway: {
position: "absolute",
top: 1e8,
left: 1e8
}
});
const MaskedTextInput = /*#__PURE__*/forwardRef(({
affinityCalculationStrategy,
affinityFormat,
allowSuggestions,
allowedKeys,
autocomplete,
autocompleteOnFocus,
autoSkip,
customNotations,
customTransformation,
defaultValue,
isRTL,
mask,
autoCapitalize = "words",
value,
onChangeText,
onTailPlaceholderChange,
renderTextInputComponent,
validationRegex,
...rest
}, ref) => {
const inputRef = useRef(null);
const maskedViewDecoratorRef = useRef(null);
const InputComponent = renderTextInputComponent ?? TextInput;
useImperativeHandle(ref, () => {
return {
isFocused: () => {
var _inputRef$current;
return !!((_inputRef$current = inputRef.current) !== null && _inputRef$current !== void 0 && _inputRef$current.isFocused());
},
blur: () => {
var _inputRef$current2;
(_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 || _inputRef$current2.blur();
},
focus: () => {
var _inputRef$current3;
(_inputRef$current3 = inputRef.current) === null || _inputRef$current3 === void 0 || _inputRef$current3.focus();
},
setNativeProps: props => {
var _inputRef$current4;
(_inputRef$current4 = inputRef.current) === null || _inputRef$current4 === void 0 || _inputRef$current4.setNativeProps(props);
},
clear: () => {
if (maskedViewDecoratorRef.current) {
// @ts-expect-error the type is correct
Commands.setText(maskedViewDecoratorRef.current, "", false);
}
},
setSelection: (start, end) => {
var _inputRef$current5;
(_inputRef$current5 = inputRef.current) === null || _inputRef$current5 === void 0 || _inputRef$current5.setSelection(start, end);
},
setText: (text, autoComplete) => {
if (maskedViewDecoratorRef.current) {
Commands.setText(
// @ts-expect-error the type is correct
maskedViewDecoratorRef.current, text, !!autoComplete);
}
}
};
});
const onAdvancedMaskTextChangeCallback = useCallback(({
nativeEvent: {
extracted,
formatted,
tailPlaceholder,
complete
}
}) => {
onChangeText === null || onChangeText === void 0 || onChangeText(formatted, extracted, tailPlaceholder, complete);
onTailPlaceholderChange === null || onTailPlaceholderChange === void 0 || onTailPlaceholderChange(tailPlaceholder);
}, [onChangeText, onTailPlaceholderChange]);
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(InputComponent, _extends({}, rest, {
ref: inputRef,
autoCapitalize: autoCapitalize
})), /*#__PURE__*/React.createElement(AdvancedTextInputMaskDecoratorViewNativeComponent
// @ts-expect-error the type is correct
, {
ref: maskedViewDecoratorRef,
affinityCalculationStrategy: affinityCalculationStrategy,
affinityFormat: affinityFormat,
allowedKeys: allowedKeys,
allowSuggestions: allowSuggestions,
autocomplete: autocomplete,
autocompleteOnFocus: autocompleteOnFocus,
autoSkip: autoSkip,
customNotations: customNotations,
customTransformation: customTransformation,
defaultValue: defaultValue,
isRTL: isRTL,
primaryMaskFormat: mask,
style: IS_FABRIC ? styles.farAway : styles.displayNone,
validationRegex: validationRegex,
value: value,
onAdvancedMaskTextChange: onAdvancedMaskTextChangeCallback
}));
});
export default /*#__PURE__*/memo(MaskedTextInput);
//# sourceMappingURL=index.js.map