UNPKG

@gravity-ui/uikit

Version:

Gravity UI base styling and components

45 lines (44 loc) 2.42 kB
'use client'; import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime"; import * as React from 'react'; import { isOfType } from "../utils/isOfType.js"; import { DisclosureProvider } from "./DisclosureContext.js"; import { DisclosureDetails } from "./DisclosureDetails/DisclosureDetails.js"; import { DefaultDisclosureSummary, DisclosureSummary } from "./DisclosureSummary/DisclosureSummary.js"; import { b } from "./constants.js"; import "./Disclosure.css"; const isDisclosureSummaryComponent = isOfType(DisclosureSummary); // @ts-expect-error this ts-error is appears when forwarding ref. It complains that DisclosureComposition props is not provided initially export const Disclosure = React.forwardRef(function Disclosure(props, ref) { const { size = 'm', disabled = false, defaultExpanded = false, arrowPosition = 'start', summary = '', className, keepMounted = true, children, onUpdate = () => { }, expanded, qa, } = props; const [summaryContent, detailsContent] = prepareChildren(children, { disclosureQa: qa, }); return (_jsx(DisclosureProvider, { disabled: disabled, defaultExpanded: defaultExpanded, expanded: expanded, keepMounted: keepMounted, size: size, summary: summary, arrowPosition: arrowPosition, onUpdate: onUpdate, children: _jsxs("section", { ref: ref, className: b({ size }, className), "data-qa": qa, children: [summaryContent, detailsContent] }) })); }); function prepareChildren(children, { disclosureQa }) { const items = React.Children.toArray(children); let summary, details; const content = []; for (const item of items) { const isDisclosureSummary = isDisclosureSummaryComponent(item); if (isDisclosureSummary) { if (summary) { throw new Error('Only one <Disclosure.Summary> component is allowed'); } summary = item; continue; } content.push(item); } if (content.length > 0) { details = (_jsx(DisclosureDetails, { qa: disclosureQa && `${disclosureQa}-details`, children: content })); } if (!summary) { summary = (_jsx(DisclosureSummary, { qa: disclosureQa && `${disclosureQa}-summary`, children: (props) => _jsx(DefaultDisclosureSummary, { ...props }) })); } return [summary, details]; } Disclosure.Summary = DisclosureSummary; Disclosure.displayName = 'Disclosure'; //# sourceMappingURL=Disclosure.js.map