| 10 | } |
| 11 | |
| 12 | export const LegendView: StatelessComponent<LegendViewProps> = (props: StatelessProps<LegendViewProps>) => { |
| 13 | const rows: TableRow[] = []; |
| 14 | |
| 15 | const addRow = (row: LegendRow, i: number) => { |
| 16 | const fn = symbolMap[row.symbol.shape]; |
| 17 | let jsx: JSX.Element; |
| 18 | if (fn) { |
| 19 | jsx = fn(row.symbol); |
| 20 | } else { |
| 21 | jsx = <span>x</span>; |
| 22 | //console.log(`need to render ${row.symbol.shape} symbol shape`); |
| 23 | } |
| 24 | rows.push({ |
| 25 | cells: [ |
| 26 | { className: 'symbol', content: jsx }, |
| 27 | { className: 'label', content: row.label, title: row.label }, |
| 28 | ], |
| 29 | }); |
| 30 | }; |
| 31 | |
| 32 | const sorted = Object.keys(props.legend.rows).sort((a, b) => +a - +b); |
| 33 | sorted.forEach(i => addRow(props.legend.rows[i], +i)); |
| 34 | |
| 35 | if (sorted.length) { |
| 36 | return ( |
| 37 | <Table |
| 38 | rows={rows} |
| 39 | rowClassName="legend-row" |
| 40 | onRowClick={(e, i) => props.onClick(e, props.legend, i)} |
| 41 | > |
| 42 | {props.legend.title !== undefined && ( |
| 43 | <tr |
| 44 | tabIndex={props.onClick ? 0 : -1} |
| 45 | onClick={e => props.onClick(e as any as MouseEvent, props.legend, null)} |
| 46 | onKeyUp={e => { |
| 47 | if (e.key === KeyCodes.ENTER && props.onClick) { |
| 48 | props.onClick(e as any as MouseEvent, props.legend, null); |
| 49 | } |
| 50 | }} |
| 51 | > |
| 52 | <th colSpan={2}>{props.legend.title}</th> |
| 53 | </tr> |
| 54 | )} |
| 55 | </Table> |
| 56 | ); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | const symbolMap: { [shape: string]: (symbol: LegendRowSymbol) => JSX.Element } = { |
| 61 | |