UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

115 lines 4.24 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["variant", "noWrap", "className"], _excluded2 = ["expanded", "disabled", "noAnimation", "onClick", "onOpened", "onClosed"]; import "core-js/modules/web.dom-collections.iterator.js"; import React from 'react'; import classnames from 'classnames'; import { TableAccordionHead } from './table-accordion/TableAccordionHead'; import { TableNavigationHead } from './table-navigation/TableNavigationHead'; import { TableAccordionContentRow } from './table-accordion/TableAccordionContent'; import { TableContext } from './TableContext'; export default function Tr(componentProps) { var _tableContext$allProp, _tableContext$allProp2, _tableContext$allProp3; const { variant, noWrap, className: _className } = componentProps, restProps = _objectWithoutProperties(componentProps, _excluded); const { currentVariant, isLast, count } = useHandleTrVariant({ variant }); const className = classnames('dnb-table__tr', _className, currentVariant && `dnb-table__tr--${currentVariant}`, isLast && 'dnb-table__tr--last', noWrap && 'dnb-table--no-wrap'); const tableContext = React.useContext(TableContext); const deprecatedAccordionProp = tableContext === null || tableContext === void 0 ? void 0 : (_tableContext$allProp = tableContext.allProps) === null || _tableContext$allProp === void 0 ? void 0 : _tableContext$allProp.accordion; if (deprecatedAccordionProp || (tableContext === null || tableContext === void 0 ? void 0 : (_tableContext$allProp2 = tableContext.allProps) === null || _tableContext$allProp2 === void 0 ? void 0 : _tableContext$allProp2.mode) == 'accordion') { return React.createElement(TableAccordionHead, _extends({ count: count, className: className }, restProps)); } if ((tableContext === null || tableContext === void 0 ? void 0 : (_tableContext$allProp3 = tableContext.allProps) === null || _tableContext$allProp3 === void 0 ? void 0 : _tableContext$allProp3.mode) == 'navigation') { return React.createElement(TableNavigationHead, _extends({ className: className }, restProps)); } const { expanded, disabled, noAnimation, onClick, onOpened, onClosed } = restProps, trProps = _objectWithoutProperties(restProps, _excluded2); return React.createElement("tr", _extends({ role: "row", className: className }, trProps)); } function useHandleTrVariant(_ref) { let { variant } = _ref; const tableContext = React.useContext(TableContext); const countRef = tableContext === null || tableContext === void 0 ? void 0 : tableContext.trCountRef.current; const increment = () => { 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; }; const [count, setCount] = React.useState(() => { if (typeof window === 'undefined') { return increment(); } }); React.useEffect(() => { setCount(increment()); }, [tableContext === null || tableContext === void 0 ? void 0 : tableContext.rerenderAlias]); 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(_ref2) { let { children } = _ref2; 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]); return { trCountRef, rerenderAlias, setRerenderAlias }; } Tr.AccordionContent = TableAccordionContentRow; //# sourceMappingURL=TableTr.js.map