UNPKG

@fruits-chain/react-native-xiaoshu

Version:
154 lines (147 loc) 4.54 kB
function _extends() { _extends = Object.assign || 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 isNil from 'lodash/isNil'; import React, { useEffect, useMemo, useRef, memo } from 'react'; import { View, Keyboard } from 'react-native'; import { callInterceptor, getDefaultValue } from '../helpers'; import { usePersistFn } from '../hooks'; import useState from '../hooks/useStateUpdate'; import NumberInput from '../number-input'; import TextInput from '../text-input'; import Theme from '../theme'; import Dialog from './dialog'; import { varCreator } from './style'; /** * Dialog 弹出框 * @description 配合函数的使用 */ const DialogInput = _ref => { let { showCancelButton = true, duration, beforeClose, onPressCancel, onPressConfirm, defaultValue, placeholder, type = 'text', autoFocus = true, textInput: { value: textInputValue, onChangeText, ...resetTextInputProps } = {}, numberInput: { value: numberInputValue, onChange, ...resetNumberInputProps } = {}, ...restProps } = _ref; const isInputText = type === 'textarea' || type === 'text'; const realValue = isInputText ? textInputValue : numberInputValue; const TextInputRef = useRef(null); const TOKENS = Theme.useThemeTokens(); const CV = Theme.createVar(TOKENS, varCreator); const [state, setState] = useState({ visible: false, value: defaultValue, cancel: false, confirm: false, overlay: false }); const boxStyle = useMemo(() => ({ marginHorizontal: CV.dialog_input_gap, marginTop: CV.dialog_input_gap, overflow: 'hidden' // borderStartColor: '#f30', // height: 100, }), [CV.dialog_input_gap]); duration = getDefaultValue(duration, CV.dialog_transition); const onChangeTextPersistFn = usePersistFn(t => { setState({ value: t }); onChangeText === null || onChangeText === void 0 ? void 0 : onChangeText(t); }); const onChangePersistFn = usePersistFn(t => { setState({ value: t }); onChange === null || onChange === void 0 ? void 0 : onChange(t); }); const genOnPressBtn = action => () => { Keyboard.dismiss(); setState({ [action]: true }); const actionCallback = action === 'confirm' ? onPressConfirm : onPressCancel; const finalValue = !isNil(state.value) ? `${state.value}` : ''; callInterceptor(beforeClose, { args: [action, finalValue], done: () => { callInterceptor(actionCallback, { args: [finalValue], done: () => { setState({ [action]: false, visible: false }); }, canceled: () => { setState({ [action]: false }); } }); }, canceled: () => { setState({ [action]: false }); } }); }; useEffect(() => { setState({ visible: true }); // 当对话框完全显示的时候再去聚焦 if (autoFocus) { setTimeout(() => { TextInputRef.current.focus(); }, duration); } }, [duration, autoFocus]); useEffect(() => { if (!isNil(realValue)) { setState({ value: realValue }); } }, [realValue]); return /*#__PURE__*/React.createElement(Dialog, _extends({}, restProps, { showCancelButton: showCancelButton, closeOnPressOverlay: false, visible: state.visible, onPressConfirm: genOnPressBtn('confirm'), onPressCancel: genOnPressBtn('cancel'), cancelButtonLoading: state.cancel, confirmButtonLoading: state.confirm }), /*#__PURE__*/React.createElement(View, { style: boxStyle }, type === 'textarea' || type === 'text' ? /*#__PURE__*/React.createElement(TextInput, _extends({}, resetTextInputProps, { ref: TextInputRef, type: type, placeholder: placeholder, value: state.value, onChangeText: onChangeTextPersistFn, bordered: true })) : /*#__PURE__*/React.createElement(NumberInput, _extends({}, resetNumberInputProps, { ref: TextInputRef, type: type, placeholder: placeholder, value: state.value, onChange: onChangePersistFn, bordered: true })))); }; export default /*#__PURE__*/memo(DialogInput); //# sourceMappingURL=dialog-input.js.map