• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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';
16import {CustomTable} from './custom_table';
17
18export interface ColumnDescriptor<T> {
19  readonly title: m.Children;
20  render: (row: T) => m.Children;
21}
22
23export interface BasicTableAttrs<T> {
24  readonly data: ReadonlyArray<T>;
25  readonly columns: ReadonlyArray<ColumnDescriptor<T>>;
26  onreorder?: (from: number, to: number) => void;
27  readonly className?: string;
28}
29
30export class BasicTable<T> implements m.ClassComponent<BasicTableAttrs<T>> {
31  view({attrs}: m.Vnode<BasicTableAttrs<T>>): m.Children {
32    return m(CustomTable<T>, {
33      columns: [
34        {
35          columns: attrs.columns.map((c) => ({
36            title: c.title,
37            render: (row: T) => ({cell: c.render(row)}),
38          })),
39          reorder: attrs.onreorder,
40        },
41      ],
42      data: attrs.data,
43      className: attrs.className,
44    });
45  }
46}
47