UNPKG

react-native-mask-text

Version:
113 lines (102 loc) 3.83 kB
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); } import React, { useEffect, useState, forwardRef } from 'react'; import { TextInput } from 'react-native'; import { mask, unMask } from '../utils/mask'; export const MaskedTextInputComponent = (_ref, ref) => { let { mask: pattern = '', type = 'custom', options = {}, defaultValue, onChangeText, value, inputAccessoryView, autoCapitalize = 'sentences', textBold, textItalic, textDecoration, style, ...rest } = _ref; const styleSheet = [{ fontWeight: textBold && 'bold', fontStyle: textItalic && 'italic', textDecorationLine: textDecoration }, style]; const isMasked = () => pattern || type === 'currency'; const getMaskedValue = value => mask(value, pattern, type, options, autoCapitalize); const getUnMaskedValue = value => unMask(value, type); const updateStatesWithMasking = inputValue => { const newUnMaskedValue = getUnMaskedValue(inputValue); const newMaskedValue = getMaskedValue(newUnMaskedValue); setMaskedValue(newMaskedValue); setUnmaskedValue(newUnMaskedValue); setRawValue(inputValue); }; const updateStatesWithoutMasking = inputValue => { setMaskedValue(inputValue); setUnmaskedValue(inputValue); setRawValue(inputValue); }; const clearAllStates = () => { setMaskedValue(''); setUnmaskedValue(''); setRawValue(''); }; const defaultValueCustom = defaultValue || ''; const defaultValueCurrency = defaultValue || '0'; const initialRawValue = value; const initialMaskedValue = isMasked() ? getMaskedValue(type === 'currency' ? defaultValueCurrency : defaultValueCustom) : value || defaultValueCustom; const initialUnMaskedValue = isMasked() ? getUnMaskedValue(type === 'currency' ? defaultValueCurrency : defaultValueCustom) : value || defaultValueCustom; const [maskedValue, setMaskedValue] = useState(initialMaskedValue); const [unMaskedValue, setUnmaskedValue] = useState(initialUnMaskedValue); const [rawValue, setRawValue] = useState(initialRawValue); const [isInitialRender, setIsInitialRender] = useState(true); const actualValue = isMasked() ? maskedValue : rawValue; const handleChange = inputValue => { if (isMasked()) { updateStatesWithMasking(inputValue); } else { updateStatesWithoutMasking(inputValue); } }; useEffect(() => { if (isInitialRender) { setIsInitialRender(false); return; } if (isMasked()) { onChangeText(maskedValue, unMaskedValue); } else { onChangeText(rawValue || '', rawValue || ''); } }, [maskedValue, unMaskedValue, rawValue]); useEffect(() => { if (value) { if (isMasked()) { setMaskedValue(getMaskedValue(value)); setUnmaskedValue(getUnMaskedValue(value)); } else { updateStatesWithoutMasking(value); } } else { if (isMasked()) { setMaskedValue(initialMaskedValue); setUnmaskedValue(initialUnMaskedValue); } else { clearAllStates(); } } }, [value]); return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(TextInput, _extends({ onChangeText: handleChange, ref: ref, maxLength: pattern.length || undefined, autoCapitalize: autoCapitalize }, rest, { value: actualValue, style: styleSheet })), inputAccessoryView); }; export const MaskedTextInput = /*#__PURE__*/forwardRef(MaskedTextInputComponent); //# sourceMappingURL=MaskedTextInput.js.map