feat(data-grid): show column comments in lookup
This commit is contained in:
12 files changed
+137
-23
No files matched your search
@@ -0,0 +1,53 @@
|
||||
export interface DataGridColumnLookupItem {
|
||||
index: number;
|
||||
name: string;
|
||||
sourceName?: string;
|
||||
comment?: string;
|
||||
}
|
||||
|
||||
export interface DataGridColumnLookupOptions {
|
||||
columns: readonly string[];
|
||||
sourceColumns?: readonly (string | undefined)[];
|
||||
displayableIndexes?: readonly number[];
|
||||
commentByColumn?: ReadonlyMap<string, string>;
|
||||
}
|
||||
|
||||
function normalizedSearchText(value: string): string {
|
||||
return value.trim().toLocaleLowerCase();
|
||||
}
|
||||
|
||||
function nonEmptyComment(value: string | undefined): string | undefined {
|
||||
const trimmed = value?.trim();
|
||||
return trimmed ? trimmed : undefined;
|
||||
}
|
||||
|
||||
export function dataGridColumnCommentFor(commentByColumn: ReadonlyMap<string, string> | undefined, columnName: string, sourceName?: string): string | undefined {
|
||||
if (!commentByColumn) return undefined;
|
||||
const keys = [sourceName, sourceName?.toLocaleLowerCase(), columnName, columnName.toLocaleLowerCase()].filter((value): value is string => !!value);
|
||||
for (const key of keys) {
|
||||
const comment = nonEmptyComment(commentByColumn.get(key));
|
||||
if (comment) return comment;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function buildDataGridColumnLookupItems(options: DataGridColumnLookupOptions): DataGridColumnLookupItem[] {
|
||||
const indexes = options.displayableIndexes ?? options.columns.map((_, index) => index);
|
||||
return indexes.map((index) => {
|
||||
const name = options.columns[index] ?? `#${index + 1}`;
|
||||
const sourceName = options.sourceColumns?.[index];
|
||||
const comment = dataGridColumnCommentFor(options.commentByColumn, name, sourceName);
|
||||
return {
|
||||
index,
|
||||
name,
|
||||
...(sourceName ? { sourceName } : {}),
|
||||
...(comment ? { comment } : {}),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function filterDataGridColumnLookupItems(items: readonly DataGridColumnLookupItem[], query: string): DataGridColumnLookupItem[] {
|
||||
const normalizedQuery = normalizedSearchText(query);
|
||||
if (!normalizedQuery) return [...items];
|
||||
return items.filter((item) => [item.name, item.sourceName, item.comment].filter((value): value is string => !!value).some((value) => value.toLocaleLowerCase().includes(normalizedQuery)));
|
||||
}
|
||||
@@ -1,11 +1,29 @@
|
||||
import { buildDataGridColumnLookupItems, filterDataGridColumnLookupItems } from "./dataGridColumnLookup";
|
||||
|
||||
export interface ColumnVisibilityOption {
|
||||
column: string;
|
||||
comment?: string;
|
||||
index: number;
|
||||
}
|
||||
|
||||
export function filterColumnVisibilityOptions(columns: string[], query: string): ColumnVisibilityOption[] {
|
||||
const normalizedQuery = query.trim().toLowerCase();
|
||||
return columns.map((column, index) => ({ column, index })).filter(({ column }) => !normalizedQuery || column.toLowerCase().includes(normalizedQuery));
|
||||
export interface ColumnVisibilityFilterOptions {
|
||||
sourceColumns?: readonly (string | undefined)[];
|
||||
commentByColumn?: ReadonlyMap<string, string>;
|
||||
}
|
||||
|
||||
export function filterColumnVisibilityOptions(columns: readonly string[], query: string, options: ColumnVisibilityFilterOptions = {}): ColumnVisibilityOption[] {
|
||||
return filterDataGridColumnLookupItems(
|
||||
buildDataGridColumnLookupItems({
|
||||
columns,
|
||||
sourceColumns: options.sourceColumns,
|
||||
commentByColumn: options.commentByColumn,
|
||||
}),
|
||||
query,
|
||||
).map((item) => ({
|
||||
column: item.name,
|
||||
...(item.comment ? { comment: item.comment } : {}),
|
||||
index: item.index,
|
||||
}));
|
||||
}
|
||||
|
||||
export function visibleColumnIndexesForFilter(availableIndexes: number[], hiddenIndexes: ReadonlySet<number>): number[] {
|
||||
|
||||
Reference in new issue
Block a user