UNPKG

@fruits-chain/react-native-xiaoshu

Version:
167 lines (164 loc) 4.41 kB
"use strict"; import isNil from 'lodash/isNil'; import React, { useEffect, useMemo, useRef, memo, useCallback } from 'react'; import { View, Keyboard } from 'react-native'; import { callInterceptor, getDefaultValue } from "../helpers/index.js"; import { usePersistFn } from "../hooks/index.js"; import useState from "../hooks/useStateUpdate.js"; import NumberInput from "../number-input/index.js"; import TextInput from "../text-input/index.js"; import Theme from "../theme/index.js"; import DialogKeyboard from "./dialog-keyboard.js"; import { varCreator } from "./style.js"; /** * Dialog 弹出框 * @description 配合函数的使用 */ import { jsx as _jsx } from "react/jsx-runtime"; 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 = useRef(null); const [CV] = Theme.useStyle({ varCreator, theme: restProps.theme }); 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, paddingBottom: CV.dialog_input_gap / 2, overflow: 'hidden', maxHeight: 200 }), [CV.dialog_input_gap]); duration = getDefaultValue(duration, CV.dialog_transition); const onChangeTextPersistFn = usePersistFn(t => { setState({ value: t }); onChangeText?.(t); }); const onChangePersistFn = usePersistFn(t => { setState({ value: t }); 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]); const onPressClose = useCallback(() => { setState({ visible: false }); }, []); return /*#__PURE__*/_jsx(DialogKeyboard, { ...restProps, showCancelButton: showCancelButton, closeOnPressOverlay: false, visible: state.visible, onPressConfirm: genOnPressBtn('confirm'), onPressCancel: genOnPressBtn('cancel'), cancelButtonLoading: state.cancel, confirmButtonLoading: state.confirm, onPressClose: onPressClose, children: /*#__PURE__*/_jsx(View, { style: boxStyle, children: type === 'textarea' || type === 'text' ? /*#__PURE__*/_jsx(TextInput, { size: "xl", ...resetTextInputProps, ref: TextInputRef, type: type, placeholder: placeholder, value: state.value, onChangeText: onChangeTextPersistFn, bordered: true }) : /*#__PURE__*/_jsx(NumberInput, { size: "xl", ...resetNumberInputProps, ref: TextInputRef, type: type, placeholder: placeholder, value: state.value, onChange: onChangePersistFn, bordered: true }) }) }); }; export default /*#__PURE__*/memo(DialogInput); //# sourceMappingURL=dialog-input.js.map