*/
import styled from '@emotion/styled';
import classNames from 'classnames';
+import { times } from 'lodash';
import { ComponentProps, createContext, ReactNode, useContext } from 'react';
import tw from 'twin.macro';
import { themeBorder, themeColor } from '../helpers';
import { FCProps } from '../types/misc';
-interface TableBaseProps extends ComponentProps<'table'> {
+export interface TableProps extends ComponentProps<'table'> {
+ columnCount: number;
+ columnWidths?: Array<number | string>;
header?: ReactNode;
noHeaderTopBorder?: boolean;
noSidePadding?: boolean;
}
-interface GenericTableProps extends TableBaseProps {
- columnCount: number;
- gridTemplate?: never;
-}
-
-interface CustomTableProps extends TableBaseProps {
- columnCount?: never;
- gridTemplate: string;
-}
-
-export type TableProps = GenericTableProps | CustomTableProps;
-
export function Table(props: TableProps) {
- const { className, header, children, noHeaderTopBorder, noSidePadding, ...rest } = props;
+ const {
+ className,
+ columnCount,
+ columnWidths = [],
+ header,
+ children,
+ noHeaderTopBorder,
+ noSidePadding,
+ ...rest
+ } = props;
return (
<StyledTable
)}
{...rest}
>
+ <colgroup>
+ {times(columnCount, (i) => (
+ <col key={i} width={columnWidths[i] ?? 'auto'} />
+ ))}
+ </colgroup>
{header && (
<thead>
<CellTypeContext.Provider value="th">{header}</CellTypeContext.Provider>
}
`;
-export const ContentCell = styled(CellComponent)`
- ${tw`sw-text-left sw-justify-start`}
-`;
-export const NumericalCell = styled(CellComponent)`
- ${tw`sw-text-right sw-justify-end`}
-`;
-export const RatingCell = styled(CellComponent)`
- ${tw`sw-text-right sw-justify-end`}
-`;
-export const CheckboxCell = styled(CellComponent)`
- ${tw`sw-text-center`}
- ${tw`sw-flex`}
- ${tw`sw-items-center sw-justify-center`}
-`;
+const CellTypeContext = createContext<'th' | 'td'>('td');
+type CellComponentProps = ComponentProps<'th' | 'td'>;
+
+export function CellComponent(props: CellComponentProps) {
+ const containerType = useContext(CellTypeContext);
+ return <CellComponentStyled as={containerType} {...props} />;
+}
+
+export function ContentCell({ children, ...props }: CellComponentProps) {
+ return (
+ <CellComponent {...props}>
+ <div className="sw-text-left sw-justify-start sw-flex sw-items-center">{children}</div>
+ </CellComponent>
+ );
+}
-const StyledTable = styled.table<GenericTableProps | CustomTableProps>`
- display: grid;
- grid-template-columns: ${(props) => props.gridTemplate ?? `repeat(${props.columnCount}, 1fr)`};
+export function NumericalCell({ children, ...props }: CellComponentProps) {
+ return (
+ <CellComponent {...props}>
+ <div className="sw-text-right sw-justify-end sw-flex sw-items-center">{children}</div>
+ </CellComponent>
+ );
+}
+
+export function RatingCell({ children, ...props }: CellComponentProps) {
+ return (
+ <CellComponent {...props}>
+ <div className="sw-text-right sw-justify-end sw-flex sw-items-center">{children}</div>
+ </CellComponent>
+ );
+}
+
+export function CheckboxCell({ children, ...props }: CellComponentProps) {
+ return (
+ <CellComponent {...props}>
+ <div className="sw-text-center sw-justify-center sw-flex sw-items-center">{children}</div>
+ </CellComponent>
+ );
+}
+
+const StyledTable = styled.table`
width: 100%;
border-collapse: collapse;
-
- thead,
- tbody,
- tr {
- display: contents;
- }
`;
const CellComponentStyled = styled.td`
color: ${themeColor('pageContent')};
- ${tw`sw-flex sw-items-center`}
${tw`sw-body-sm`}
${tw`sw-py-4 sw-px-2`}
- ${tw`sw-align-top`}
+ ${tw`sw-align-middle`}
thead > tr > & {
color: ${themeColor('pageTitle')};
${tw`sw-body-sm-highlight`}
}
`;
-
-const CellTypeContext = createContext<'th' | 'td'>('td');
-
-export function CellComponent(props: ComponentProps<'th' | 'td'>) {
- const containerType = useContext(CellTypeContext);
- return <CellComponentStyled as={containerType} {...props} />;
-}
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { ContentCell, Table, TableRow } from 'design-system';
-import { sortBy } from 'lodash';
+import { sortBy, times } from 'lodash';
import * as React from 'react';
import withKeyboardNavigation from '../../../components/hoc/withKeyboardNavigation';
import { getComponentMeasureUniqueKey } from '../../../helpers/component';
+import { isDefined } from '../../../helpers/types';
import { BranchLike } from '../../../types/branch-like';
import { ComponentQualifier } from '../../../types/component';
import { ComponentMeasure, Metric } from '../../../types/types';
ComponentQualifier.SubPortfolio,
].includes(baseComponent.qualifier as ComponentQualifier);
+ const columnCount = metrics.length + Number(canBePinned) + Number(showAnalysisDate) + 1;
return (
<div className="big-spacer-bottom table-wrapper">
<Table
- gridTemplate={`${canBePinned ? 'min-content' : ''} auto repeat(${
- metrics.length + (showAnalysisDate ? 1 : 0)
- }, max-content)`}
+ columnCount={columnCount}
+ columnWidths={[
+ canBePinned ? '1%' : undefined,
+ 'auto',
+ ...times(columnCount - 1, () => '1%'),
+ ].filter(isDefined)}
header={
baseComponent && (
<TableRow>
showAnalysisDate={showAnalysisDate}
/>
<TableRow>
- <ContentCell className="sw-col-span-full" />
+ <ContentCell colSpan={columnCount} />
</TableRow>
</>
)}
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { ContentCell, NumericalCell, Table, TableRow, TableRowInteractive } from 'design-system';
+import { times } from 'lodash';
import * as React from 'react';
import { getLocalizedMetricName } from '../../../helpers/l10n';
import { BranchLike } from '../../../types/branch-like';
const otherMetrics = (complementary[metric.key] || []).map((key) => metrics[key]);
return (
<Table
- gridTemplate={`1fr repeat(${otherMetrics.length + 1}, min-content)`}
+ columnCount={otherMetrics.length + 2}
+ columnWidths={['auto', ...times(otherMetrics.length + 1, () => '1%')]}
header={
otherMetrics.length > 0 && (
<TableRow>