40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { assertI18nLabel, type DataTableModel, type DataTableVM } from "@hivegrid/view-models";
|
|
import { renderDataTable } from "@hivegrid/ui-subframe";
|
|
|
|
import { defineAdapter } from "./defineAdapter.js";
|
|
|
|
const DEFAULT_EMPTY_LABEL_KEY = "ui.data-table.empty";
|
|
|
|
/**
|
|
* Normalizes a domain {@link DataTableVM} into the {@link DataTableModel}
|
|
* prop-model the UI implementation renders. Validates every i18n key
|
|
* (columns + caption) and fills in defaults (column align, empty label,
|
|
* row count) so render implementations stay dumb.
|
|
*/
|
|
export function toDataTableModel(vm: DataTableVM): DataTableModel {
|
|
const columns = vm.columns.map((column) => {
|
|
assertI18nLabel(column);
|
|
return {
|
|
key: column.key,
|
|
labelKey: column.labelKey,
|
|
align: column.align ?? "left",
|
|
};
|
|
});
|
|
|
|
const captionKey = vm.caption ? assertI18nLabel(vm.caption).labelKey : undefined;
|
|
|
|
return {
|
|
captionKey,
|
|
columns,
|
|
rows: vm.rows,
|
|
emptyLabelKey: vm.emptyLabelKey ?? DEFAULT_EMPTY_LABEL_KEY,
|
|
rowCount: vm.rows.length,
|
|
};
|
|
}
|
|
|
|
export const dataTableAdapter = defineAdapter({
|
|
name: "dataTable",
|
|
toProps: toDataTableModel,
|
|
defaultRenderer: renderDataTable,
|
|
});
|