UNPKG

robust-react-ui

Version:

A React component library, built with a focus on accessibility, extensibility and reusability.

70 lines (65 loc) 3.26 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var React = require('react'); var React__default = _interopDefault(React); var index = require('../../../utils/getClassNames/index.js'); var index$1 = require('../Panel/index.js'); // Generated with util/create-component.js var Accordion = function (_a) { var children = _a.children, fullWidth = _a.fullWidth, activePanel = _a.activePanel, onSelectPanelFunction = _a.onSelectPanelFunction; var _b = React.useState([]), refs = _b[0], setRefs = _b[1]; var handleFocusPrevious = function (_index) { var _a, _b, _c; var max = refs.length - 1; var newIndex = _index === 0 ? max : _index - 1; (_c = (_b = (_a = refs[newIndex]) === null || _a === void 0 ? void 0 : _a.ref) === null || _b === void 0 ? void 0 : _b.current) === null || _c === void 0 ? void 0 : _c.focus(); }; var handleFocusNext = function (_index) { var _a, _b, _c; var max = refs.length - 1; var newIndex = _index === max ? 0 : _index + 1; (_c = (_b = (_a = refs[newIndex]) === null || _a === void 0 ? void 0 : _a.ref) === null || _b === void 0 ? void 0 : _b.current) === null || _c === void 0 ? void 0 : _c.focus(); }; var handleFocusFirst = function () { var _a, _b, _c; if (refs.length) (_c = (_b = (_a = refs[0]) === null || _a === void 0 ? void 0 : _a.ref) === null || _b === void 0 ? void 0 : _b.current) === null || _c === void 0 ? void 0 : _c.focus(); }; var handleFocusLast = function () { var _a, _b, _c; if (refs.length) (_c = (_b = (_a = refs[refs.length - 1]) === null || _a === void 0 ? void 0 : _a.ref) === null || _b === void 0 ? void 0 : _b.current) === null || _c === void 0 ? void 0 : _c.focus(); }; React.useEffect(function () { if (React__default.Children.count(children)) { setRefs(React__default.Children.map(children, function (child, index) { if (React__default.isValidElement(child)) { return { panelId: child.props.panelId, idx: index, ref: React.createRef(), }; } return null; })); } }, []); return (React__default.createElement("div", { "data-testid": "Accordion", id: "accordionGroup", className: index({ 'rrui-accordion': true, 'rrui-accordion--full-width': fullWidth, }) }, React__default.Children.map(children, function (child, index) { return React__default.cloneElement(child, { activePanel: activePanel, onSelectPanelFunction: onSelectPanelFunction, handleFocusPrevious: handleFocusPrevious, handleFocusNext: handleFocusNext, handleFocusFirst: handleFocusFirst, handleFocusLast: handleFocusLast, index: index, ref: refs.length ? refs[index].ref : null, }); }))); }; Accordion.Panel = index$1; module.exports = Accordion; //# sourceMappingURL=Accordion.js.map