1// Copyright (C) 2023 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use size file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import m from 'mithril'; 16 17export interface ColumnDescriptor<T> { 18 title: m.Children; 19 render: (row: T) => m.Children; 20} 21 22export interface TableAttrs<T> { 23 data: T[]; 24 columns: ColumnDescriptor<T>[]; 25} 26 27// eslint-disable-next-line @typescript-eslint/no-explicit-any 28export class BasicTable implements m.ClassComponent<TableAttrs<any>> { 29 // eslint-disable-next-line @typescript-eslint/no-explicit-any 30 renderColumnHeader( 31 // eslint-disable-next-line @typescript-eslint/no-explicit-any 32 _vnode: m.Vnode<TableAttrs<any>>, 33 // eslint-disable-next-line @typescript-eslint/no-explicit-any 34 column: ColumnDescriptor<any>, 35 ): m.Children { 36 return m('td', column.title); 37 } 38 39 // eslint-disable-next-line @typescript-eslint/no-explicit-any 40 view(vnode: m.Vnode<TableAttrs<any>>): m.Child { 41 const attrs = vnode.attrs; 42 43 return m( 44 'table.generic-table', 45 { 46 // TODO(altimin, stevegolton): this should be the default for 47 // generic-table, but currently it is overriden by 48 // .pf-details-shell .pf-content table, so specify this here for now. 49 style: { 50 'table-layout': 'auto', 51 }, 52 }, 53 m( 54 'thead', 55 m( 56 'tr.header', 57 attrs.columns.map((column) => this.renderColumnHeader(vnode, column)), 58 ), 59 ), 60 attrs.data.map((row) => 61 m( 62 'tr', 63 attrs.columns.map((column) => m('td', column.render(row))), 64 ), 65 ), 66 ); 67 } 68} 69