@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
123 lines • 4.04 kB
JavaScript
import React from 'react';
import clsx from 'clsx';
import { TableAccordionHead } from "./table-accordion/TableAccordionHead.js";
import { TableNavigationHead } from "./table-navigation/TableNavigationHead.js";
import { TableAccordionContentRow } from "./table-accordion/TableAccordionContent.js";
import { TableContext } from "./TableContext.js";
import { jsx as _jsx } from "react/jsx-runtime";
export default function Tr(componentProps) {
var _tableContext$allProp, _tableContext$allProp2;
const {
variant,
noWrap,
className: _className,
...restProps
} = componentProps;
const {
currentVariant,
isLast,
count
} = useHandleTrVariant({
variant
});
const className = clsx('dnb-table__tr', _className, currentVariant && `dnb-table__tr--${currentVariant}`, isLast && 'dnb-table__tr--last', noWrap && 'dnb-table--no-wrap');
const tableContext = React.useContext(TableContext);
if ((tableContext === null || tableContext === void 0 || (_tableContext$allProp = tableContext.allProps) === null || _tableContext$allProp === void 0 ? void 0 : _tableContext$allProp.mode) == 'accordion') {
return _jsx(TableAccordionHead, {
count: count,
className: className,
...restProps
});
}
if ((tableContext === null || tableContext === void 0 || (_tableContext$allProp2 = tableContext.allProps) === null || _tableContext$allProp2 === void 0 ? void 0 : _tableContext$allProp2.mode) === 'navigation') {
return _jsx(TableNavigationHead, {
className: className,
...restProps
});
}
const {
expanded,
disabled,
noAnimation,
keepInDOM,
onClick,
onOpen,
onClose,
...trProps
} = restProps;
return _jsx("tr", {
role: "row",
className: className,
...trProps
});
}
function useHandleTrVariant({
variant
}) {
const tableContext = React.useContext(TableContext);
const countRef = tableContext === null || tableContext === void 0 ? void 0 : tableContext.trCountRef.current;
const lastRenderAliasRef = React.useRef(undefined);
const hasIncrementedRef = React.useRef(false);
const increment = React.useCallback(() => {
if (typeof countRef === 'undefined') {
return 0;
}
if (!variant || variant === 'even' && countRef.count % 2 === 1 || variant === 'odd' && countRef.count % 2 === 0) {
countRef.count++;
}
return countRef.count;
}, [countRef, variant]);
const [count, setCount] = React.useState(() => {
if (typeof window === 'undefined') {
hasIncrementedRef.current = true;
return increment();
}
return undefined;
});
React.useEffect(() => {
if (lastRenderAliasRef.current !== (tableContext === null || tableContext === void 0 ? void 0 : tableContext.rerenderAlias)) {
lastRenderAliasRef.current = tableContext === null || tableContext === void 0 ? void 0 : tableContext.rerenderAlias;
hasIncrementedRef.current = false;
}
if (!hasIncrementedRef.current) {
hasIncrementedRef.current = true;
setCount(increment());
}
}, [tableContext === null || tableContext === void 0 ? void 0 : tableContext.rerenderAlias, increment]);
let currentVariant = variant;
if (!currentVariant) {
currentVariant = count % 2 ? 'odd' : 'even';
}
const isLast = typeof countRef !== 'undefined' && countRef.count === count;
return {
currentVariant,
isLast,
count
};
}
export function useHandleOddEven({
children
}) {
const trCountRef = React.useRef({
count: 0
});
const [rerenderAlias, setRerenderAlias] = React.useState({});
const forceRerender = React.useCallback(() => {
trCountRef.current.count = 0;
setRerenderAlias({});
}, []);
const isMounted = React.useRef(false);
React.useEffect(() => {
if (isMounted.current) {
forceRerender();
}
isMounted.current = true;
}, [children, forceRerender]);
return {
trCountRef,
rerenderAlias,
setRerenderAlias
};
}
Tr.AccordionContent = TableAccordionContentRow;
//# sourceMappingURL=TableTr.js.map