UNPKG

@ducor/react

Version:

admin template ui interface

107 lines (106 loc) 4.61 kB
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import React, { createContext, useContext, useState, } from "react"; var DisclosureContext = createContext(undefined); var useDisclosureContext = function () { var context = useContext(DisclosureContext); if (!context) { throw new Error("Disclosure components must be used within a Disclosure."); } return context; }; var useDisclosureItem = function (index) { var context = useDisclosureContext(); var isOpen = __spreadArray([], context.isOpen, true).includes(index); return { isOpen: isOpen, toggle: function () { context.toggle(index); }, }; }; // eventKey; eta use kora hoyeche muloto rerander jeno na hoy var Disclosure = function (_a) { var children = _a.children, _b = _a.defaultActiveKey, defaultActiveKey = _b === void 0 ? [] : _b, eventKey = _a.eventKey, _c = _a.alwaysOpen, alwaysOpen = _c === void 0 ? false : _c; // je datai dea hok kono errror dibe na . eta resove var _resolve = function () { var key = eventKey ? eventKey : defaultActiveKey; if (typeof key === "number" || typeof key === "string") { return [key]; } else if (typeof key === "object" && Array.isArray(key)) { return __spreadArray([], key.filter(function (k) { return typeof k === "number" || typeof k === "string"; }), true); } return []; }; var _d = useState(_resolve), isOpen = _d[0], setIsOpen = _d[1]; var toggle = function (index) { if (alwaysOpen) { //many if (__spreadArray([], isOpen, true).includes(index)) { setIsOpen(__spreadArray([], isOpen.filter(function (key) { return key !== index; }), true)); } else { setIsOpen(__spreadArray(__spreadArray([], isOpen, true), [index], false)); } } else if (typeof index === "number" || typeof index === "string") { // single item setIsOpen(__spreadArray([], isOpen, true).includes(index) ? [] : [index]); } }; var isValid = children !== null && children !== undefined; // children to array var childrenArray = isValid ? React.Children.toArray(children) : []; return (_jsx(DisclosureContext.Provider, { value: { isOpen: isOpen, alwaysOpen: alwaysOpen, toggle: toggle }, children: childrenArray.map(function (child, index) { if (child !== null && child !== undefined) { var newChild = React.cloneElement(child, { key: index, index: index, }); return newChild; } return null; }) })); }; var DisclosureItem = function (_a) { var children = _a.children, index = _a.index; var isValid = children !== null && children !== undefined; // children to array var childrenArray = isValid ? React.Children.toArray(children) : []; return (_jsx("div", { className: 'border rounded-lg overflow-hidden', children: childrenArray.map(function (child, key) { if (child !== null && child !== undefined) { var newChild = React.cloneElement(child, { key: key, index: index, }); return newChild; } return null; }) })); }; var Header = function (_a) { var children = _a.children, index = _a.index; var _b = useDisclosureItem(index), isOpen = _b.isOpen, toggle = _b.toggle; return (_jsxs("div", { onClick: toggle, role: 'button', className: 'flex justify-between items-center bg-gray-900 text-white w-full px-4 py-2', children: [_jsx("div", { children: children }), _jsx("i", { className: isOpen ? "feather-arrow-down" : "feather-arrow-up" })] })); }; var Body = function (_a) { var children = _a.children, index = _a.index; var isOpen = useDisclosureItem(index).isOpen; return isOpen ? (_jsx("div", { className: 'px-4 py-2 bg-gray-100', children: children })) : null; }; export default Object.assign(Disclosure, { Item: DisclosureItem, Header: Header, Body: Body, });