react-native-timer-picker
Version:
A simple, flexible, performant duration picker for React Native apps 🔥 Great for timers, alarms and duration inputs ⏰🕰️⏳ Includes iOS-style haptic and audio feedback 🍏
153 lines (150 loc) • 9.23 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _getSafeInitialValue = require("../../utils/getSafeInitialValue");
var _Modal = _interopRequireDefault(require("../Modal"));
var _TimerPicker = _interopRequireDefault(require("../TimerPicker"));
var _styles = require("./styles");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const TimerPickerModal = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
const {
buttonContainerProps,
buttonTouchableOpacityProps,
cancelButtonText = "Cancel",
closeOnOverlayPress,
confirmButtonText = "Confirm",
containerProps,
contentContainerProps,
hideCancelButton = false,
initialValue,
modalProps,
modalTitle,
modalTitleProps,
onCancel,
onConfirm,
onDurationChange,
setIsVisible,
styles: customStyles,
visible,
...otherProps
} = props;
const styles = (0, _styles.generateStyles)(customStyles, {
hasModalTitle: Boolean(modalTitle)
});
const timerPickerRef = (0, _react.useRef)(null);
const safeInitialValue = (0, _getSafeInitialValue.getSafeInitialValue)({
days: initialValue === null || initialValue === void 0 ? void 0 : initialValue.days,
hours: initialValue === null || initialValue === void 0 ? void 0 : initialValue.hours,
minutes: initialValue === null || initialValue === void 0 ? void 0 : initialValue.minutes,
seconds: initialValue === null || initialValue === void 0 ? void 0 : initialValue.seconds
});
const [selectedDuration, setSelectedDuration] = (0, _react.useState)(safeInitialValue);
const [confirmedDuration, setConfirmedDuration] = (0, _react.useState)(safeInitialValue);
const reset = options => {
var _timerPickerRef$curre;
setSelectedDuration(safeInitialValue);
setConfirmedDuration(safeInitialValue);
(_timerPickerRef$curre = timerPickerRef.current) === null || _timerPickerRef$curre === void 0 || _timerPickerRef$curre.reset(options);
};
// reset state if the initial value changes
(0, _react.useEffect)(() => {
reset();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [safeInitialValue.days, safeInitialValue.hours, safeInitialValue.minutes, safeInitialValue.seconds]);
const hideModalHandler = () => {
setSelectedDuration({
days: confirmedDuration.days,
hours: confirmedDuration.hours,
minutes: confirmedDuration.minutes,
seconds: confirmedDuration.seconds
});
setIsVisible(false);
};
const confirmHandler = () => {
var _timerPickerRef$curre2, _latestDuration$days, _latestDuration$hours, _latestDuration$minut, _latestDuration$secon;
const latestDuration = (_timerPickerRef$curre2 = timerPickerRef.current) === null || _timerPickerRef$curre2 === void 0 ? void 0 : _timerPickerRef$curre2.latestDuration;
const newDuration = {
days: (latestDuration === null || latestDuration === void 0 || (_latestDuration$days = latestDuration.days) === null || _latestDuration$days === void 0 ? void 0 : _latestDuration$days.current) ?? selectedDuration.days,
hours: (latestDuration === null || latestDuration === void 0 || (_latestDuration$hours = latestDuration.hours) === null || _latestDuration$hours === void 0 ? void 0 : _latestDuration$hours.current) ?? selectedDuration.hours,
minutes: (latestDuration === null || latestDuration === void 0 || (_latestDuration$minut = latestDuration.minutes) === null || _latestDuration$minut === void 0 ? void 0 : _latestDuration$minut.current) ?? selectedDuration.minutes,
seconds: (latestDuration === null || latestDuration === void 0 || (_latestDuration$secon = latestDuration.seconds) === null || _latestDuration$secon === void 0 ? void 0 : _latestDuration$secon.current) ?? selectedDuration.seconds
};
setConfirmedDuration(newDuration);
onConfirm(newDuration);
};
const cancelHandler = () => {
setIsVisible(false);
setSelectedDuration(confirmedDuration);
onCancel === null || onCancel === void 0 || onCancel();
};
// wrapped in useCallback to avoid unnecessary re-renders of TimerPicker
const durationChangeHandler = (0, _react.useCallback)(duration => {
setSelectedDuration(duration);
onDurationChange === null || onDurationChange === void 0 || onDurationChange(duration);
}, [onDurationChange]);
(0, _react.useImperativeHandle)(ref, () => {
var _timerPickerRef$curre4, _timerPickerRef$curre5, _timerPickerRef$curre6, _timerPickerRef$curre7;
return {
reset,
setValue: (value, options) => {
var _timerPickerRef$curre3;
setSelectedDuration(prev => ({
...prev,
...value
}));
setConfirmedDuration(prev => ({
...prev,
...value
}));
(_timerPickerRef$curre3 = timerPickerRef.current) === null || _timerPickerRef$curre3 === void 0 || _timerPickerRef$curre3.setValue(value, options);
},
latestDuration: {
days: (_timerPickerRef$curre4 = timerPickerRef.current) === null || _timerPickerRef$curre4 === void 0 || (_timerPickerRef$curre4 = _timerPickerRef$curre4.latestDuration) === null || _timerPickerRef$curre4 === void 0 ? void 0 : _timerPickerRef$curre4.days,
hours: (_timerPickerRef$curre5 = timerPickerRef.current) === null || _timerPickerRef$curre5 === void 0 || (_timerPickerRef$curre5 = _timerPickerRef$curre5.latestDuration) === null || _timerPickerRef$curre5 === void 0 ? void 0 : _timerPickerRef$curre5.hours,
minutes: (_timerPickerRef$curre6 = timerPickerRef.current) === null || _timerPickerRef$curre6 === void 0 || (_timerPickerRef$curre6 = _timerPickerRef$curre6.latestDuration) === null || _timerPickerRef$curre6 === void 0 ? void 0 : _timerPickerRef$curre6.minutes,
seconds: (_timerPickerRef$curre7 = timerPickerRef.current) === null || _timerPickerRef$curre7 === void 0 || (_timerPickerRef$curre7 = _timerPickerRef$curre7.latestDuration) === null || _timerPickerRef$curre7 === void 0 ? void 0 : _timerPickerRef$curre7.seconds
}
};
});
return /*#__PURE__*/_react.default.createElement(_Modal.default, _extends({
isVisible: visible,
onOverlayPress: closeOnOverlayPress ? hideModalHandler : undefined
}, modalProps, {
testID: "timer-picker-modal"
}), /*#__PURE__*/_react.default.createElement(_reactNative.View, _extends({}, containerProps, {
style: styles.container
}), /*#__PURE__*/_react.default.createElement(_reactNative.View, _extends({}, contentContainerProps, {
style: styles.contentContainer
}), modalTitle ? /*#__PURE__*/_react.default.createElement(_reactNative.Text, _extends({}, modalTitleProps, {
style: styles.modalTitle
}), modalTitle) : null, /*#__PURE__*/_react.default.createElement(_TimerPicker.default, _extends({
ref: timerPickerRef,
initialValue: confirmedDuration
}, otherProps, {
aggressivelyGetLatestDuration: true,
onDurationChange: durationChangeHandler,
styles: styles.timerPickerStyles
})), /*#__PURE__*/_react.default.createElement(_reactNative.View, _extends({}, buttonContainerProps, {
style: styles.buttonContainer
}), !hideCancelButton ? /*#__PURE__*/_react.default.isValidElement(props.cancelButton) ? /*#__PURE__*/_react.default.cloneElement(props.cancelButton, {
onPress: cancelHandler
}) : /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, _extends({}, buttonTouchableOpacityProps, {
onPress: cancelHandler
}), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
style: [styles.button, styles.cancelButton]
}, cancelButtonText)) : null, /*#__PURE__*/_react.default.isValidElement(props.confirmButton) ? /*#__PURE__*/_react.default.cloneElement(props.confirmButton, {
onPress: confirmHandler
}) : /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, _extends({}, buttonTouchableOpacityProps, {
onPress: confirmHandler
}), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
style: [styles.button, styles.confirmButton]
}, confirmButtonText))))));
});
var _default = exports.default = /*#__PURE__*/_react.default.memo(TimerPickerModal);
//# sourceMappingURL=TimerPickerModal.js.map