@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
128 lines (124 loc) • 4.69 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 isNil from 'lodash/isNil';
import React, { useRef, useCallback, memo, isValidElement } from 'react';
import { View, Text, Animated } from 'react-native';
import Button from '../button';
import { getDefaultValue, easing, renderTextLikeJSX } from '../helpers';
import { usePersistFn } from '../hooks';
import Locale from '../locale';
import Popup from '../popup/popup';
import Theme from '../theme';
import { varCreator, styleCreator } from './style';
/**
* Dialog 弹出框
* @description 弹出模态框,常用于消息提示、消息确认,或在当前页面内完成特定的交互操作。
* @description 弹出框组件支持函数调用和组件调用两种方式。
*/
const Dialog = _ref => {
let {
children,
title,
message,
width,
messageAlign = 'center',
showConfirmButton = true,
showCancelButton = false,
confirmButtonText,
cancelButtonText,
confirmButtonColor,
cancelButtonColor,
confirmButtonLoading = false,
cancelButtonLoading = false,
onPressCancel,
onPressConfirm,
duration,
onOpen: onOpenFn,
onClose: onCloseFn,
...resetProps
} = _ref;
const fadeAnim = useRef(new Animated.Value(0)).current;
const fadeInstance = useRef(null);
const locale = Locale.useLocale().Dialog;
const TOKENS = Theme.useThemeTokens();
const CV = Theme.createVar(TOKENS, varCreator);
const STYLES = Theme.createStyle(CV, styleCreator);
width = getDefaultValue(width, CV.dialog_width);
duration = getDefaultValue(duration, CV.dialog_transition);
const showDialog = useCallback(show => {
if (fadeInstance.current) {
fadeInstance.current.stop();
fadeInstance.current = null;
}
fadeInstance.current = Animated.timing(fadeAnim, // 动画中的变量值
{
toValue: show ? 1 : 0,
duration: duration,
useNativeDriver: true,
easing: show ? easing.easeOutCirc : easing.easeInCubic
});
fadeInstance.current.start();
}, [duration, fadeAnim]);
const onOpenPersistFn = usePersistFn(() => {
showDialog(true);
onOpenFn === null || onOpenFn === void 0 ? void 0 : onOpenFn();
});
const onClosePersistFn = usePersistFn(() => {
showDialog(false);
onCloseFn === null || onCloseFn === void 0 ? void 0 : onCloseFn();
});
const titleJSX = renderTextLikeJSX(title, STYLES.title_text);
const messageJSX = !isNil(message) ? /*#__PURE__*/isValidElement(message) ? message : /*#__PURE__*/React.createElement(Text, {
style: [STYLES.message_text, {
textAlign: messageAlign
}]
}, message) : null;
const cancelButtonProps = {
color: cancelButtonColor || CV.dialog_cancel_button_text_color,
text: cancelButtonText || locale.cancelButtonText,
loading: cancelButtonLoading,
onPress: onPressCancel
};
const confirmButtonProps = {
color: confirmButtonColor || CV.dialog_confirm_button_text_color,
text: confirmButtonText || locale.confirmButtonText,
loading: confirmButtonLoading,
onPress: onPressConfirm
};
return /*#__PURE__*/React.createElement(Popup, _extends({}, resetProps, {
duration: duration,
onOpen: onOpenPersistFn,
onClose: onClosePersistFn
}), /*#__PURE__*/React.createElement(Animated.View, {
style: [STYLES.dialog, {
width,
transform: [{
scale: fadeAnim.interpolate({
inputRange: [0, 0.01, 0.98, 1],
outputRange: [0, 0.9, 1.02, 1]
})
}]
}]
}, titleJSX, titleJSX ? messageJSX : /*#__PURE__*/React.createElement(View, {
style: STYLES.content_isolated
}, messageJSX), children, /*#__PURE__*/React.createElement(View, {
style: STYLES.footer
}, showCancelButton ? /*#__PURE__*/React.createElement(Button, _extends({}, cancelButtonProps, {
type: "link",
size: "xl",
square: true,
style: STYLES.btn
})) : null, showConfirmButton ? /*#__PURE__*/React.createElement(Button, _extends({}, confirmButtonProps, {
type: "link",
size: "xl",
square: true,
style: [STYLES.btn, showCancelButton ? // eslint-disable-next-line react-native/no-inline-styles
{
// react-native-web 覆盖原 button 样式
borderLeftWidth: 1,
borderColor: CV.dialog_footer_divider_color
} : null],
textStyle: STYLES.btn_confirm
})) : null)));
};
export default /*#__PURE__*/memo(Dialog);
//# sourceMappingURL=dialog.js.map