@@ -18,19 +18,19 @@ require('./simple-table.css');
1818
1919import * as React from 'react' ;
2020import * as ReactDOM from 'react-dom' ;
21- import { $ , Expression , Executor , Dataset } from 'plywood' ;
22- import { Stage , Clicker , Essence , DataCube , Filter , Dimension , Measure } from '../../../common/models/index' ;
21+ import { } from '../../../common/models/index' ;
2322
2423import { classNames } from '../../utils/dom/dom' ;
2524
26- import { SvgIcon } from '../svg-icon/svg-icon' ;
27- import { Scroller , ScrollerPart } from '../scroller/scroller' ;
25+ import { SvgIcon , Scroller , ScrollerPart } from '../index' ;
2826
2927export interface SimpleTableColumn {
3028 label : string ;
3129 field : string | ( ( row : any ) => any ) ;
3230 width : number ;
3331 cellIcon ?: string ;
32+ render ?: ( column : SimpleTableColumn , hovered : boolean ) => JSX . Element ;
33+ data ?: any ;
3434}
3535
3636export interface SimpleTableAction {
@@ -44,12 +44,16 @@ export interface SimpleTableProps extends React.Props<any> {
4444 rows : any [ ] ;
4545 actions ?: SimpleTableAction [ ] ;
4646 onRowClick ?: ( row : any ) => void ;
47+ onHeaderClick ?: ( column : SimpleTableColumn ) => boolean ;
48+
49+ headerHeight ?: number ;
4750}
4851
4952export interface SimpleTableState {
5053 sortColumn ?: SimpleTableColumn ;
5154 sortAscending ?: boolean ;
5255 hoveredRowIndex ?: number ;
56+ hoveredColumnIndex ?: number ;
5357 hoveredActionIndex ?: number ;
5458}
5559
@@ -58,18 +62,32 @@ const HEADER_HEIGHT = 26;
5862const ACTION_WIDTH = 30 ;
5963
6064export class SimpleTable extends React . Component < SimpleTableProps , SimpleTableState > {
65+ static defaultProps = {
66+ actions : [ ] as SimpleTableAction [ ] ,
67+ headerHeight : HEADER_HEIGHT
68+ } ;
69+
6170 constructor ( ) {
6271 super ( ) ;
6372
6473 this . state = { } ;
6574 }
6675
6776 renderHeaders ( columns : SimpleTableColumn [ ] , sortColumn : SimpleTableColumn , sortAscending : boolean ) : JSX . Element {
77+ const { hoveredRowIndex, hoveredColumnIndex } = this . state ;
78+
6879 var items : JSX . Element [ ] = [ ] ;
6980
7081 for ( let i = 0 ; i < columns . length ; i ++ ) {
7182 let column = columns [ i ] ;
7283
84+ let isHovered = hoveredRowIndex === - 1 && i === hoveredColumnIndex ;
85+
86+ if ( column . render ) {
87+ items . push ( column . render ( column , isHovered ) ) ;
88+ continue ;
89+ }
90+
7391 let icon : JSX . Element = null ;
7492 if ( sortColumn && sortColumn === column ) {
7593 icon = < SvgIcon
@@ -79,7 +97,7 @@ export class SimpleTable extends React.Component<SimpleTableProps, SimpleTableSt
7997 }
8098
8199 items . push ( < div
82- className = "header"
100+ className = { classNames ( "header" , { hover : isHovered } ) }
83101 style = { { width : column . width } }
84102 key = { `column-${ i } ` }
85103 > { column . label } { icon } </ div > ) ;
@@ -186,6 +204,8 @@ export class SimpleTable extends React.Component<SimpleTableProps, SimpleTableSt
186204 getLayout ( columns : SimpleTableColumn [ ] , rows : any [ ] , actions : SimpleTableAction [ ] ) {
187205 const width = columns . reduce ( ( a , b ) => a + b . width , 0 ) ;
188206
207+ actions = actions || [ ] ;
208+
189209 const directActionsCount = actions . filter ( ( a ) => ! a . inEllipsis ) . length ;
190210 const indirectActionsCount = directActionsCount !== actions . length ? 1 : 0 ;
191211
@@ -195,7 +215,7 @@ export class SimpleTable extends React.Component<SimpleTableProps, SimpleTableSt
195215 bodyHeight : rows . length * ROW_HEIGHT ,
196216
197217 // Gutters
198- top : HEADER_HEIGHT ,
218+ top : this . props . headerHeight ,
199219 right : directActionsCount * 30 + indirectActionsCount * 30 ,
200220 bottom : 0 ,
201221 left : 0
@@ -240,8 +260,8 @@ export class SimpleTable extends React.Component<SimpleTableProps, SimpleTableSt
240260 var rowIndex = - 1 ; // -1 means header
241261
242262 // Not in the header
243- if ( y > HEADER_HEIGHT ) {
244- rowIndex = Math . floor ( ( y - HEADER_HEIGHT ) / ROW_HEIGHT ) ;
263+ if ( y > this . props . headerHeight ) {
264+ rowIndex = Math . floor ( ( y - this . props . headerHeight ) / ROW_HEIGHT ) ;
245265 }
246266
247267 return rowIndex ;
@@ -302,6 +322,11 @@ export class SimpleTable extends React.Component<SimpleTableProps, SimpleTableSt
302322 }
303323
304324 onHeaderClick ( column : SimpleTableColumn ) {
325+ if ( this . props . onHeaderClick ) {
326+ let shouldSort = this . props . onHeaderClick ( column ) ;
327+ if ( ! shouldSort ) return ;
328+ }
329+
305330 this . setState ( {
306331 sortColumn : column ,
307332 sortAscending : this . state . sortColumn === column ? ! this . state . sortAscending : true
@@ -317,8 +342,10 @@ export class SimpleTable extends React.Component<SimpleTableProps, SimpleTableSt
317342 const headerWidth = this . getHeaderWidth ( columns ) ;
318343
319344 var rowIndex = this . getRowIndex ( y ) ;
345+ var columnIndex = this . getColumnIndex ( x , headerWidth ) ;
320346
321347 this . setState ( {
348+ hoveredColumnIndex : columnIndex ,
322349 hoveredRowIndex : rowIndex > rows . length ? undefined : rowIndex ,
323350 hoveredActionIndex : part === Scroller . RIGHT_GUTTER ? this . getActionIndex ( x , headerWidth ) : undefined
324351 } ) ;
@@ -327,6 +354,7 @@ export class SimpleTable extends React.Component<SimpleTableProps, SimpleTableSt
327354 onMouseLeave ( ) {
328355 this . setState ( {
329356 hoveredRowIndex : undefined ,
357+ hoveredColumnIndex : undefined ,
330358 hoveredActionIndex : undefined
331359 } ) ;
332360 }
0 commit comments