@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
148 lines (147 loc) • 6.39 kB
JavaScript
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpotlightTour = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const Spotlight_1 = require("./Spotlight");
const SpotlightProvider_1 = require("./SpotlightProvider");
const SpotlightTour = (props) => {
const { setActiveSpotlight, setIsOpen } = (0, SpotlightProvider_1.useSpotlightContext)();
const { spotlights, dismissButtonText = 'Dismiss', backButtonText = 'Back', nextButtonText = 'Next', closeButtonText = 'Close', onAction, } = props;
const next = (0, react_1.useCallback)((index) => {
onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'next');
setActiveSpotlight(spotlights[index + 1].target);
}, [onAction, spotlights, setActiveSpotlight]);
const back = (0, react_1.useCallback)((index) => {
onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'back');
setActiveSpotlight(spotlights[index - 1].target);
}, [onAction, spotlights, setActiveSpotlight]);
const dismiss = (0, react_1.useCallback)((index) => {
onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'dismiss');
setActiveSpotlight(null);
setIsOpen(false);
}, [onAction, spotlights, setActiveSpotlight, setIsOpen]);
const close = (0, react_1.useCallback)((index) => {
onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'close');
setActiveSpotlight(null);
setIsOpen(false);
}, [onAction, spotlights, setActiveSpotlight, setIsOpen]);
const getActions = (0, react_1.useCallback)((actions, index) => {
if (actions === null || actions === void 0 ? void 0 : actions.length) {
return actions;
}
const isFirst = index === 0;
const isLast = index === spotlights.length - 1;
const hasMultiple = spotlights.length > 1;
const newActions = [];
if (!isLast && hasMultiple) {
newActions.push({
label: dismissButtonText,
fill: 'text',
onClick: () => dismiss(index),
});
}
if (!isFirst && hasMultiple) {
newActions.push({
label: backButtonText,
fill: 'text',
onClick: () => back(index),
});
}
if (isLast) {
newActions.push({
label: closeButtonText,
fill: 'outlined',
onClick: () => close(index),
});
}
else {
newActions.push({
label: nextButtonText,
fill: 'outlined',
onClick: () => next(index),
});
}
return newActions;
}, [
spotlights,
dismissButtonText,
dismiss,
backButtonText,
back,
closeButtonText,
close,
nextButtonText,
next,
]);
const getBeforeActions = (0, react_1.useCallback)((beforeActions, index) => {
if (beforeActions) {
return beforeActions;
}
return spotlights.length > 1 ? index + 1 + '/' + spotlights.length : null;
}, [spotlights]);
const getInitialFocus = (0, react_1.useCallback)((initialFocus, actions) => {
if (initialFocus !== undefined) {
return initialFocus;
}
return actions.length - 1;
}, []);
const getOnClose = (0, react_1.useCallback)((onClose, index) => {
if (onClose) {
return onClose;
}
return (reason) => {
onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, reason);
};
}, [onAction, spotlights]);
const getOnOpen = (0, react_1.useCallback)((onOpen, index) => {
if (onOpen) {
return onOpen;
}
return () => {
onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'open');
};
}, [onAction, spotlights]);
return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: spotlights.map((spotlight, index) => {
const { actions, beforeActions, initialFocus, onClose, onOpen } = spotlight, restSpotlight = __rest(spotlight, ["actions", "beforeActions", "initialFocus", "onClose", "onOpen"]);
const newActions = getActions(actions, index);
const newBeforeActions = getBeforeActions(beforeActions, index);
const newInitialFocus = getInitialFocus(initialFocus, newActions);
const newOnClose = getOnClose(onClose, index);
const newOnOpen = getOnOpen(onOpen, index);
return ((0, jsx_runtime_1.jsx)(Spotlight_1.Spotlight, Object.assign({}, restSpotlight, { actions: newActions, beforeActions: newBeforeActions, initialFocus: newInitialFocus, onClose: newOnClose, onOpen: newOnOpen }), index));
}) }));
};
exports.SpotlightTour = SpotlightTour;
//# sourceMappingURL=SpotlightTour.js.map