@react-form-fields/native-base
Version:
Native Base Form Fields
57 lines • 4.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var useConfigContext_1 = require("@react-form-fields/core/hooks/useConfigContext");
var useMask_1 = require("@react-form-fields/core/hooks/useMask");
var useMemoOtherProps_1 = require("@react-form-fields/core/hooks/useMemoOtherProps");
var useValidation_1 = require("@react-form-fields/core/hooks/useValidation");
var native_base_1 = require("native-base");
var React = require("react");
var useFieldFlow_1 = require("./hooks/useFieldFlow");
var Wrapper_1 = require("./shared/Wrapper");
var FieldText = React.memo(React.forwardRef(function (props, ref) {
var onChange = props.onChange, onLabelPress = props._onLabelPress, onPress = props._onPress, marginBottom = props.marginBottom, helperText = props.helperText, onSubmitEditing = props.onSubmitEditing, returnKeyType = props.returnKeyType, loading = props.loading, displayValue = props.displayValue, hideErrorMessage = props.hideErrorMessage, leftIcon = props.leftIcon;
var config = useConfigContext_1.default();
var _a = useValidation_1.default(props), setDirty = _a.setDirty, showError = _a.showError, errorMessage = _a.errorMessage, isValid = _a.isValid;
var _b = useMask_1.default(props), maskedValue = _b.maskedValue, maskClean = _b.maskClean;
var inputRef = React.useRef();
var _c = useFieldFlow_1.default(props, React.useCallback(function () { return inputRef.current && inputRef.current._root.focus(); }, [inputRef])), goNext = _c[0], hasValidIndex = _c[1], flowIndex = _c[2];
var _d = React.useState(false), showPassword = _d[0], setShowPassword = _d[1];
var otherProps = useMemoOtherProps_1.default(props, 'value', 'onChange', 'label', 'marginBottom', 'helperText', 'LabelProps', 'ItemProps');
var onChangeHandler = React.useCallback(function (text) {
config.validationOn === 'onChange' && setDirty(true);
onChange(maskClean(text));
}, [onChange, setDirty, maskClean, config.validationOn]);
var onSubmitHandler = React.useCallback(function (e) {
if (onSubmitEditing) {
setTimeout(function () { return onSubmitEditing(e); }, config.validationDelay || 500);
return;
}
goNext(flowIndex);
}, [onSubmitEditing, goNext, flowIndex, config.validationDelay]);
React.useImperativeHandle(ref, function () { return ({
focus: function () { return inputRef.current._root.focus(); },
blur: function () { return inputRef.current._root.blur(); },
setDirty: function (dirty) { return setDirty(dirty); }
}); }, [inputRef, setDirty]);
var toggleShowPassword = React.useCallback(function () {
setShowPassword(!showPassword);
}, [showPassword]);
var _e = React.useMemo(function () {
if (props.rightIcon || !props.secureTextEntry) {
return [props.rightIcon, props.rightIconAction];
}
return [showPassword ? 'eye-off' : 'eye', toggleShowPassword];
}, [props.rightIcon, props.rightIconAction, props.secureTextEntry, showPassword, toggleShowPassword]), rightIconComponent = _e[0], rightIconAction = _e[1];
var secureTextEntry = React.useMemo(function () {
if (props.secureTextEntry !== true || showPassword) {
return false;
}
return true;
}, [props.secureTextEntry, showPassword]);
return (React.createElement(Wrapper_1.default, { label: props.label, LabelProps: props.LabelProps, ItemProps: props.ItemProps, showError: showError, errorMessage: errorMessage, helperText: helperText, marginBottom: marginBottom, isValid: isValid, leftIcon: leftIcon, rightIcon: rightIconComponent, rightIconAction: rightIconAction, loading: loading, hideErrorMessage: hideErrorMessage, _onLabelPress: onLabelPress, _onPress: onPress, _disabled: props._disabled !== undefined ? props._disabled : props.editable === false },
React.createElement(native_base_1.Input, tslib_1.__assign({}, otherProps, { ref: inputRef, value: displayValue || (maskedValue || '').toString() || null, onChangeText: onChangeHandler, returnKeyType: returnKeyType ? returnKeyType : onSubmitEditing ? 'send' : hasValidIndex ? 'next' : 'default', onSubmitEditing: onSubmitHandler, secureTextEntry: secureTextEntry }))));
}));
FieldText.displayName = 'FieldText';
exports.default = FieldText;
//# sourceMappingURL=Text.js.map