@audira/carbon-react-native
Version:
Build React Native apps with component and shared patterns using Carbon
131 lines (130 loc) • 4.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Controller = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _index = require("../../../const/index.js");
var _index2 = require("./_modal/index.js");
var _index3 = require("./_overlay/index.js");
var _jsxRuntime = require("react/jsx-runtime");
const Controller = exports.Controller = /*#__PURE__*/(0, _react.forwardRef)(function Controller({
animationConfig,
modalProps,
overlayProps,
overlayTouchDismiss,
onEmpty
}, ref) {
const [data, setData] = (0, _react.useState)([]),
modalRefs = (0, _react.useRef)([]),
overlayRef = (0, _react.useRef)(null),
initialRender = (0, _react.useRef)(false),
setModalRef = (0, _react.useCallback)((modalRef, index) => {
if (modalRef && !modalRefs.current[index]) {
modalRefs.current[index] = modalRef;
}
}, []),
dismissAllHandler = (0, _react.useCallback)(() => {
if (modalRefs.current.length) {
return new Promise(resolve => {
Promise.all([overlayRef.current?.animateUnmount() ?? Promise.resolve(), modalRefs.current[modalRefs.current.length - 1]?.toState(2) ?? Promise.resolve()]).then(() => {
setData([]);
// modalRefs.current = []
resolve();
});
});
}
return Promise.resolve();
}, []),
dismissHandler = (0, _react.useCallback)(() => {
if (modalRefs.current.length) {
return new Promise(resolve => {
if (modalRefs.current.length == 1) {
dismissAllHandler();
} else {
modalRefs.current[modalRefs.current.length - 1]?.toState(2).then(() => {
modalRefs.current.splice(modalRefs.current.length - 1, 1);
modalRefs.current[modalRefs.current.length - 1]?.fromToState(0, 1).then(() => {
setData(curr => {
const nextData = curr.slice();
nextData.splice(nextData.length - 1, 1);
return nextData;
});
resolve();
});
});
}
});
}
return Promise.resolve();
}, [dismissAllHandler]);
(0, _react.useEffect)(() => {
if (!initialRender.current) {
initialRender.current = true;
} else {
if (!data.length) {
onEmpty();
}
}
}, [onEmpty, data.length]);
(0, _react.useImperativeHandle)(ref, () => {
return {
show({
stack,
...restData
}) {
if (!modalRefs.current.length) {
setData([{
...restData
}]);
} else {
modalRefs.current[modalRefs.current.length - 1]?.toState(2).then(() => {
if (stack) {
setData(curr => {
return [...curr, {
...restData
}];
});
} else {
modalRefs.current.splice(modalRefs.current.length - 1, 1);
setData([{
...restData
}]);
}
});
}
},
dismiss: dismissHandler,
dismissAll: dismissAllHandler
};
}, [dismissHandler, dismissAllHandler]);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_index3.Overlay, {
ref: overlayRef,
...overlayProps,
animationConfig: {
duration: getSingleAnimationConfigDuration(animationConfig?.duration) ?? _index.DialogAnimationConfigs.CarbonReact.duration
},
children: data.map((item, index) => {
return /*#__PURE__*/(0, _react.createElement)(_index2.Modal, {
...modalProps,
key: index,
animationConfig: animationConfig,
ref: modalRef => setModalRef(modalRef, index)
}, (item.overlayTouchDismiss ?? overlayTouchDismiss) && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, {
onPress: dismissHandler,
style: _reactNative.StyleSheet.absoluteFill
}), item.component);
})
});
});
function getSingleAnimationConfigDuration(duration) {
if (typeof duration === 'number') {
return duration;
}
if (duration) {
return duration[0];
}
return undefined;
}
//# sourceMappingURL=Controller.js.map