@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
116 lines (115 loc) • 3.85 kB
JavaScript
"use strict";
import omit from 'lodash/omit';
import pick from 'lodash/pick';
import React, { useState, useEffect, useRef, useCallback, memo } from 'react';
import { Text, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import DatePickerView from "../date-picker-view/index.js";
import { callInterceptor } from "../helpers/index.js";
import { usePersistFn } from "../hooks/index.js";
import Locale from "../locale/index.js";
import { varCreator as varCreatorPicker, styleCreator as styleCreatorPicker } from "../picker/style.js";
import Popup from "../popup/index.js";
import Theme from "../theme/index.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const DATE_PICKER_VIEW_PROPS_KEYS = ['defaultValue', 'mode', 'min', 'max', 'renderLabel'];
const DatePickerSingleMethod = ({
title,
confirmButtonText,
cancelButtonText,
onCancel,
onConfirm,
onPressOverlay,
beforeClose,
...restProps
}) => {
const locale = Locale.useLocale().DatePickerSingleMethod;
const TOKENS = Theme.useThemeTokens();
const CV_PICKER = Theme.createVar(TOKENS, varCreatorPicker);
const STYLES_PICKER = Theme.createStyle(CV_PICKER, styleCreatorPicker);
const insets = useSafeAreaInsets();
const [visible, setVisible] = useState(false);
const [loading, setLoading] = useState(false);
const Value = useRef(restProps.defaultValue || new Date());
useEffect(() => {
setVisible(true);
}, []);
const onChange = useCallback(v => {
Value.current = v;
}, []);
const doAction = usePersistFn(action => {
setLoading(true);
callInterceptor(beforeClose, {
args: [action, Value.current],
done: () => {
switch (action) {
case 'cancel':
onCancel?.(Value.current);
break;
case 'confirm':
onConfirm?.(Value.current);
break;
case 'overlay':
onPressOverlay?.(Value.current);
break;
default:
break;
}
setLoading(false);
setVisible(false);
},
canceled: () => {
setLoading(false);
}
});
});
const onPressCancel = useCallback(() => {
doAction('cancel');
}, [doAction]);
const onPressConfirm = useCallback(() => {
doAction('confirm');
}, [doAction]);
const onPressPopupOverlay = useCallback(() => {
doAction('overlay');
}, [doAction]);
const onRequestClose = useCallback(() => {
onPressPopupOverlay();
return true;
}, [onPressPopupOverlay]);
const dataPickerViewProps = pick(restProps, DATE_PICKER_VIEW_PROPS_KEYS);
const popupProps = omit(restProps, DATE_PICKER_VIEW_PROPS_KEYS);
return /*#__PURE__*/_jsxs(Popup, {
...popupProps,
onRequestClose: onRequestClose,
visible: visible,
onPressOverlay: onPressPopupOverlay,
position: "bottom",
round: true,
children: [/*#__PURE__*/_jsx(Popup.Header, {
showClose: false,
title: title,
leftExtra: /*#__PURE__*/_jsx(Text, {
suppressHighlighting: true,
style: STYLES_PICKER.cancel_text,
onPress: loading ? undefined : onPressCancel,
children: cancelButtonText ?? locale.cancelButtonText
}),
rightExtra: /*#__PURE__*/_jsx(Text, {
suppressHighlighting: true,
style: STYLES_PICKER.confirm_text,
onPress: loading ? undefined : onPressConfirm,
children: confirmButtonText ?? locale.confirmButtonText
})
}), /*#__PURE__*/_jsx(DatePickerView, {
...dataPickerViewProps,
loading: loading,
onChange: onChange
}), /*#__PURE__*/_jsx(View, {
style: {
height: insets.bottom + CV_PICKER.picker_bottom_gap
}
})]
});
};
export default /*#__PURE__*/memo(DatePickerSingleMethod);
//# sourceMappingURL=date-picker-single-method.js.map