|
| 1 | +import * as React from 'react'; |
| 2 | +import { IChartTableProps } from './ChartTable.types'; |
| 3 | +import { IChartTableStyleProps, IChartTableStyles } from '../../index'; |
| 4 | +import { classNamesFunction, getRTL, initializeComponentRef } from '@fluentui/react/lib/Utilities'; |
| 5 | +import { IImageExportOptions } from '../../types/index'; |
| 6 | +import { toImage } from '../../utilities/image-export-utils'; |
| 7 | + |
| 8 | +const getClassNames = classNamesFunction<IChartTableStyleProps, IChartTableStyles>(); |
| 9 | + |
| 10 | +export class ChartTableBase extends React.Component<IChartTableProps> { |
| 11 | + private _isRTL: boolean; |
| 12 | + private _rootElem: HTMLDivElement | null; |
| 13 | + |
| 14 | + constructor(props: IChartTableProps) { |
| 15 | + super(props); |
| 16 | + initializeComponentRef(this); |
| 17 | + this._isRTL = getRTL(props.theme); |
| 18 | + } |
| 19 | + public toImage = (opts?: IImageExportOptions): Promise<string> => { |
| 20 | + return toImage(this._rootElem, undefined, this._isRTL, opts); |
| 21 | + }; |
| 22 | + |
| 23 | + public render(): JSX.Element { |
| 24 | + const { headers, rows, width, height, styles, theme } = this.props; |
| 25 | + |
| 26 | + const classNames = getClassNames(styles!, { |
| 27 | + theme: theme!, |
| 28 | + }); |
| 29 | + |
| 30 | + if (!headers || headers.length === 0) { |
| 31 | + return <div>No data available</div>; |
| 32 | + } |
| 33 | + |
| 34 | + return ( |
| 35 | + <div |
| 36 | + ref={el => (this._rootElem = el)} |
| 37 | + className={classNames.root} |
| 38 | + style={{ height: height ? `${height}px` : '650px', overflow: 'hidden' }} |
| 39 | + > |
| 40 | + <svg width={width ?? '100%'} height={height ?? '650px'}> |
| 41 | + <foreignObject x="0" y="0" width="100%" height="100%"> |
| 42 | + <div |
| 43 | + style={{ |
| 44 | + maxHeight: height ? `${height}px` : '650px', |
| 45 | + overflowY: 'auto', |
| 46 | + overflowX: 'auto', |
| 47 | + }} |
| 48 | + > |
| 49 | + <table |
| 50 | + className={classNames.table} |
| 51 | + style={{ |
| 52 | + width: width ? `${width}px` : '100%', |
| 53 | + }} |
| 54 | + > |
| 55 | + <thead> |
| 56 | + <tr> |
| 57 | + {headers.map((header, idx) => ( |
| 58 | + <th key={idx} className={classNames.headerCell}> |
| 59 | + {header} |
| 60 | + </th> |
| 61 | + ))} |
| 62 | + </tr> |
| 63 | + </thead> |
| 64 | + {rows && rows.length > 0 && ( |
| 65 | + <tbody> |
| 66 | + {rows.map((row, rowIdx) => ( |
| 67 | + <tr key={rowIdx}> |
| 68 | + {row.map((cell, colIdx) => ( |
| 69 | + <td key={colIdx} className={classNames.bodyCell}> |
| 70 | + {cell} |
| 71 | + </td> |
| 72 | + ))} |
| 73 | + </tr> |
| 74 | + ))} |
| 75 | + </tbody> |
| 76 | + )} |
| 77 | + </table> |
| 78 | + </div> |
| 79 | + </foreignObject> |
| 80 | + </svg> |
| 81 | + </div> |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
0 commit comments