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 🍏
140 lines (137 loc) • 8.27 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 _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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)({
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.hours, safeInitialValue.minutes, safeInitialValue.seconds]);
const hideModalHandler = () => {
setSelectedDuration({
hours: confirmedDuration.hours,
minutes: confirmedDuration.minutes,
seconds: confirmedDuration.seconds
});
setIsVisible(false);
};
const confirmHandler = () => {
var _timerPickerRef$curre2, _latestDuration$hours, _latestDuration$minut, _latestDuration$secon;
const latestDuration = (_timerPickerRef$curre2 = timerPickerRef.current) === null || _timerPickerRef$curre2 === void 0 ? void 0 : _timerPickerRef$curre2.latestDuration;
const newDuration = {
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;
return {
reset,
setValue: (value, options) => {
var _timerPickerRef$curre3;
setSelectedDuration(value);
setConfirmedDuration(value);
(_timerPickerRef$curre3 = timerPickerRef.current) === null || _timerPickerRef$curre3 === void 0 || _timerPickerRef$curre3.setValue(value, options);
},
latestDuration: {
hours: (_timerPickerRef$curre4 = timerPickerRef.current) === null || _timerPickerRef$curre4 === void 0 || (_timerPickerRef$curre4 = _timerPickerRef$curre4.latestDuration) === null || _timerPickerRef$curre4 === void 0 ? void 0 : _timerPickerRef$curre4.hours,
minutes: (_timerPickerRef$curre5 = timerPickerRef.current) === null || _timerPickerRef$curre5 === void 0 || (_timerPickerRef$curre5 = _timerPickerRef$curre5.latestDuration) === null || _timerPickerRef$curre5 === void 0 ? void 0 : _timerPickerRef$curre5.minutes,
seconds: (_timerPickerRef$curre6 = timerPickerRef.current) === null || _timerPickerRef$curre6 === void 0 || (_timerPickerRef$curre6 = _timerPickerRef$curre6.latestDuration) === null || _timerPickerRef$curre6 === void 0 ? void 0 : _timerPickerRef$curre6.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.createElement(_reactNative.TouchableOpacity, _extends({}, buttonTouchableOpacityProps, {
onPress: cancelHandler
}), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
style: [styles.button, styles.cancelButton]
}, cancelButtonText)) : null, /*#__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=index.js.map