@itwin/itwinui-react
Version:
A react component library for iTwinUI
115 lines (114 loc) • 3.05 kB
JavaScript
import * as React from 'react';
import { defaultColumn } from 'react-table';
import cx from 'classnames';
import { Box } from '../../../utils/index.js';
import { TableInstanceContext } from '../utils.js';
export const DefaultCellContext = React.createContext({});
export const DefaultCell = (props) => {
let instance = React.useContext(TableInstanceContext);
let isCustomCell = React.useMemo(
() =>
instance?.columns.find(({ id }) => props.cellProps.column.id === id)
?.Cell !== defaultColumn.Cell,
[instance, props.cellProps.column.id],
);
let defaultCellContext = React.useContext(DefaultCellContext);
let isCellRendererChildrenCustom =
defaultCellContext.children !== props.children;
let isDefaultTextCell =
'string' == typeof props.cellProps.value &&
!isCustomCell &&
!isCellRendererChildrenCustom;
let {
cellElementProps: {
className: cellElementClassName,
style: cellElementStyle,
...cellElementProps
},
children,
startIcon,
endIcon,
cellProps,
isDisabled,
className,
style,
status,
text = isDefaultTextCell ? cellProps.value : void 0,
clamp = !!text,
...rest
} = props;
let { key: cellElementKey, ...cellElementPropsRest } = cellElementProps;
let decorations = {
start: startIcon
? React.createElement(
Box,
{
className: 'iui-table-cell-start-icon',
key: `${cellElementKey}-start`,
},
startIcon,
)
: null,
end: endIcon
? React.createElement(
Box,
{
className: 'iui-table-cell-end-icon',
key: `${cellElementKey}-end`,
},
endIcon,
)
: null,
};
return React.createElement(
Box,
{
...cellElementPropsRest,
key: cellElementKey,
...rest,
className: cx(cellElementClassName, className),
'aria-disabled': isDisabled?.(cellProps.row.original) || void 0,
'data-iui-status': status,
style: {
...cellElementStyle,
...style,
},
},
(() => {
if (text)
return React.createElement(
React.Fragment,
null,
decorations.start,
defaultCellContext.expander,
React.createElement(
Box,
{
className: 'iui-table-cell-default-content',
onClick: (e) => e.stopPropagation(),
},
clamp
? React.createElement(
Box,
{
className: 'iui-line-clamp',
},
text,
)
: text,
),
decorations.end,
defaultCellContext.shadows,
);
return React.createElement(
React.Fragment,
null,
decorations.start,
children,
decorations.end,
);
})(),
);
};
if ('development' === process.env.NODE_ENV)
DefaultCell.displayName = 'DefaultCell';