@fruits-chain/react-native-xiaoshu
Version:
React Native UI library
129 lines (122 loc) • 3.61 kB
JavaScript
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 React, { useEffect, useMemo, useRef, memo } from 'react';
import { View, Keyboard } from 'react-native';
import TextInput from '../text-input';
import { useTheme } from '../theme';
import useState from '../hooks/useStateUpdate';
import { usePersistFn } from '../hooks';
import { callInterceptor, isDef, getDefaultValue } from '../helpers';
import Dialog from './dialog';
/**
* Dialog 弹出框
* @description 配合函数的使用
*/
const DialogInput = _ref => {
let {
showCancelButton = true,
duration,
beforeClose,
onPressCancel,
onPressConfirm,
defaultValue,
placeholder,
type,
textInput: {
value,
onChangeText,
autoFocus = true,
...resetTextInputProps
} = {},
...restProps
} = _ref;
const TextInputRef = useRef(null);
const THEME_VAR = useTheme();
const [state, setState] = useState({
visible: false,
value: defaultValue,
cancel: false,
confirm: false,
overlay: false
});
const boxStyle = useMemo(() => ({
marginHorizontal: THEME_VAR.padding_md,
marginTop: THEME_VAR.padding_md,
overflow: 'hidden',
borderStartColor: '#f30' // height: 100,
}), [THEME_VAR.padding_md]);
duration = getDefaultValue(duration, THEME_VAR.dialog_transition);
const onChangeTextPersistFn = usePersistFn(t => {
setState({
value: t
});
onChangeText === null || onChangeText === void 0 ? void 0 : onChangeText(t);
});
const genOnPressBtn = action => () => {
Keyboard.dismiss();
setState({
[action]: true
});
const actionCallback = action === 'confirm' ? onPressConfirm : onPressCancel;
callInterceptor(beforeClose, {
args: [action, state.value],
done: () => {
callInterceptor(actionCallback, {
args: [state.value || ''],
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 (isDef(value)) {
setState({
value
});
}
}, [value]);
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
}, /*#__PURE__*/React.createElement(TextInput, _extends({}, resetTextInputProps, {
ref: TextInputRef,
type: type,
placeholder: placeholder,
value: state.value,
onChangeText: onChangeTextPersistFn,
bordered: true
}))));
};
export default /*#__PURE__*/memo(DialogInput);
//# sourceMappingURL=dialog-input.js.map