@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
80 lines (79 loc) • 3.07 kB
JavaScript
"use client";
import React, { useContext, useMemo } from 'react';
import clsx from 'clsx';
import { ListContext } from "./ListContext.js";
import FlexContainer from "../flex/Stack.js";
import HeightAnimation from "../height-animation/HeightAnimation.js";
import SharedContext from "../../shared/Context.js";
import { useSharedState } from "../../shared/helpers/useSharedState.js";
import { jsx as _jsx } from "react/jsx-runtime";
function ListContainer(props) {
var _ref, _toggleData$expanded;
const {
className,
children,
visibleCount,
variant = 'basic',
separated = false,
skeleton,
disabled,
wrapChildrenInSpace = false,
...rest
} = props;
const parentContext = useContext(ListContext);
const globalContext = useContext(SharedContext);
const appliedSkeleton = (_ref = skeleton !== null && skeleton !== void 0 ? skeleton : parentContext === null || parentContext === void 0 ? void 0 : parentContext.skeleton) !== null && _ref !== void 0 ? _ref : globalContext === null || globalContext === void 0 ? void 0 : globalContext.skeleton;
const appliedDisabled = disabled !== null && disabled !== void 0 ? disabled : parentContext === null || parentContext === void 0 ? void 0 : parentContext.disabled;
const hasVisibleCount = typeof visibleCount === 'number' && Number.isFinite(visibleCount) && visibleCount > 0;
const hasToggle = hasVisibleCount && props.id !== undefined;
const {
data: toggleData
} = useSharedState(hasToggle ? props.id : undefined, {
expanded: false
});
const expanded = hasToggle ? (_toggleData$expanded = toggleData === null || toggleData === void 0 ? void 0 : toggleData.expanded) !== null && _toggleData$expanded !== void 0 ? _toggleData$expanded : false : false;
const shouldLimit = hasVisibleCount && !expanded;
const renderedChildren = useMemo(() => {
if (!hasVisibleCount) {
return children;
}
const childArray = React.Children.toArray(children);
if (!shouldLimit) {
return childArray;
}
return childArray.map((child, index) => {
if (index < visibleCount) {
return child;
}
if (React.isValidElement(child)) {
return React.cloneElement(child, {
hidden: true
});
}
return null;
});
}, [children, hasVisibleCount, shouldLimit, visibleCount]);
const listContent = _jsx(FlexContainer, {
element: "ul",
rowGap: separated ? 'small' : false,
wrap: false,
wrapChildrenInSpace: wrapChildrenInSpace,
className: clsx("dnb-list dnb-list__container", className, variant && `dnb-list--variant-${variant}`, separated && 'dnb-list--separated'),
...rest,
children: renderedChildren
});
return _jsx(ListContext, {
value: {
variant,
separated,
skeleton: appliedSkeleton,
disabled: appliedDisabled
},
children: hasToggle ? _jsx(HeightAnimation, {
children: listContent
}) : listContent
});
}
ListContainer._supportsSpacingProps = true;
export default ListContainer;
//# sourceMappingURL=Container.js.map