UNPKG

@fruits-chain/react-native-xiaoshu

Version:
172 lines (170 loc) 5.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _isNil = _interopRequireDefault(require("lodash/isNil")); var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _index = require("../helpers/index.js"); var _index2 = require("../hooks/index.js"); var _useStateUpdate = _interopRequireDefault(require("../hooks/useStateUpdate.js")); var _index3 = _interopRequireDefault(require("../number-input/index.js")); var _index4 = _interopRequireDefault(require("../text-input/index.js")); var _index5 = _interopRequireDefault(require("../theme/index.js")); var _dialogKeyboard = _interopRequireDefault(require("./dialog-keyboard.js")); var _style = require("./style.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; } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * Dialog 弹出框 * @description 配合函数的使用 */const DialogInput = ({ showCancelButton = true, duration, beforeClose, onPressCancel, onPressConfirm, defaultValue, placeholder, type = 'text', autoFocus = true, textInput: { value: textInputValue, onChangeText, ...resetTextInputProps } = {}, numberInput: { value: numberInputValue, onChange, ...resetNumberInputProps } = {}, ...restProps }) => { const isInputText = type === 'textarea' || type === 'text'; const realValue = isInputText ? textInputValue : numberInputValue; const TextInputRef = (0, _react.useRef)(null); const [CV] = _index5.default.useStyle({ varCreator: _style.varCreator, theme: restProps.theme }); const [state, setState] = (0, _useStateUpdate.default)({ visible: false, value: defaultValue || '', cancel: false, confirm: false, overlay: false }); const boxStyle = (0, _react.useMemo)(() => ({ marginHorizontal: CV.dialog_input_gap, marginTop: CV.dialog_input_gap, paddingBottom: CV.dialog_input_gap / 2, overflow: 'hidden', maxHeight: 200 }), [CV.dialog_input_gap]); duration = (0, _index.getDefaultValue)(duration, CV.dialog_transition); const onChangeTextPersistFn = (0, _index2.usePersistFn)(t => { setState({ value: t }); onChangeText?.(t); }); const onChangePersistFn = (0, _index2.usePersistFn)(t => { setState({ value: t }); onChange?.(t); }); const genOnPressBtn = action => () => { _reactNative.Keyboard.dismiss(); setState({ [action]: true }); const actionCallback = action === 'confirm' ? onPressConfirm : onPressCancel; const finalValue = !(0, _isNil.default)(state.value) ? `${state.value}` : ''; (0, _index.callInterceptor)(beforeClose, { args: [action, finalValue], done: () => { (0, _index.callInterceptor)(actionCallback, { args: [finalValue], done: () => { setState({ [action]: false, visible: false }); }, canceled: () => { setState({ [action]: false }); } }); }, canceled: () => { setState({ [action]: false }); } }); }; (0, _react.useEffect)(() => { setState({ visible: true }); // 当对话框完全显示的时候再去聚焦 if (autoFocus) { setTimeout(() => { TextInputRef.current?.focus(); }, duration); } }, [duration, autoFocus]); (0, _react.useEffect)(() => { if (!(0, _isNil.default)(realValue)) { setState({ value: realValue }); } }, [realValue]); const onPressClose = (0, _react.useCallback)(() => { setState({ visible: false }); }, []); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dialogKeyboard.default, { ...restProps, showCancelButton: showCancelButton, closeOnPressOverlay: false, visible: state.visible, onPressConfirm: genOnPressBtn('confirm'), onPressCancel: genOnPressBtn('cancel'), cancelButtonLoading: state.cancel, confirmButtonLoading: state.confirm, onPressClose: onPressClose, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, { style: boxStyle, children: type === 'textarea' || type === 'text' ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_index4.default, { size: "xl", ...resetTextInputProps, ref: TextInputRef, type: type, placeholder: placeholder, value: state.value, onChangeText: onChangeTextPersistFn, bordered: true }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_index3.default, { size: "xl", ...resetNumberInputProps, ref: TextInputRef, type: type, placeholder: placeholder, value: state.value, onChange: onChangePersistFn, bordered: true }) }) }); }; var _default = exports.default = /*#__PURE__*/(0, _react.memo)(DialogInput); //# sourceMappingURL=dialog-input.js.map