react-sidenav
Version:
A Simple SideBar Navigation written in React
61 lines (60 loc) • 2.7 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var types_1 = require("./types");
exports.SideNavActionContext = React.createContext(null);
exports.SideNavContext = React.createContext(null);
exports.SideNav = function (props) {
var _a = React.useState({
selectedPath: props.defaultSelectedPath || '',
mode: props.mode || types_1.ViewMode.normal,
childrenToggleMode: props.childrenToggleMode || types_1.ChildrenToggleMode.hover,
childrenToggleIndicator: props.childrenToggleIndicator
}), state = _a[0], setState = _a[1];
var onSelectionPathSelected = function (path, selectionData) {
if (props.onSelection) {
props.onSelection(path, selectionData);
}
setState(function (currentState) {
return __assign({}, currentState, { selectedPath: path });
});
};
var onMouseOver = function (e) {
var mouseOverPathId;
var current = e.target;
while (current && current.getAttribute) {
var pathId = current.getAttribute('data-pathid');
if (pathId) {
mouseOverPathId = pathId;
break;
}
current = current.parentNode;
}
if (mouseOverPathId && state.mouseOverPathId !== mouseOverPathId) {
setState(__assign({}, state, { mouseOverPathId: mouseOverPathId }));
}
};
React.useEffect(function () {
setState(__assign({}, state, { mode: props.mode || types_1.ViewMode.normal }));
}, [props.mode]);
React.useEffect(function () {
setState(__assign({}, state, { selectedPath: props.defaultSelectedPath || '' }));
}, [props.defaultSelectedPath]);
React.useEffect(function () {
setState(__assign({}, state, { childrenToggleIndicator: props.childrenToggleIndicator }));
}, [props.childrenToggleIndicator]);
return (React.createElement(exports.SideNavContext.Provider, { value: state },
React.createElement(exports.SideNavActionContext.Provider, { value: { onSelectionPathSelected: onSelectionPathSelected } },
React.createElement("aside", { onMouseOver: onMouseOver, "data-selected-path": state.selectedPath, "data-testid": 'sidenav-root' }, props.children))));
};