Skip to content

Commit 9b2bb98

Browse files
authored
feature(List): auto-create columns from static data (#1085)
1 parent 42c750e commit 9b2bb98

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

packages/components/src/components/List/model/ReactTable.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ export class ReactTable<T> {
158158
s.updateTableColumnDef(getOrCreateColumnDef(s.property)),
159159
);
160160

161+
this.list.loader.staticDataProperties.forEach((property) => {
162+
getOrCreateColumnDef(property);
163+
});
164+
161165
return Array.from(columnDefsMap.values());
162166
}
163167
}

packages/components/src/components/List/model/loading/IncrementalLoader.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useEffect } from "react";
1111
import { times } from "remeda";
1212
import { IncrementalLoaderState } from "@/components/List/model/loading/IncrementalLoaderState";
1313
import { hash } from "object-code";
14+
import type { PropertyName } from "@/components/List/model/types";
1415

1516
type AsyncResourceLoadingState = AsyncResource["state"]["value"];
1617

@@ -23,6 +24,7 @@ export class IncrementalLoader<T> {
2324
public readonly manualFiltering: boolean;
2425
public readonly manualPagination: boolean;
2526
public readonly loaderState: IncrementalLoaderState<T>;
27+
public readonly staticDataProperties: PropertyName<T>[] = [];
2628

2729
private constructor(list: List<T>, shape: IncrementalLoaderShape<T> = {}) {
2830
const { source } = shape;
@@ -52,6 +54,8 @@ export class IncrementalLoader<T> {
5254
this.manualSorting = manualSorting ?? this.manualPagination;
5355
this.list.filters.forEach((f) => f.onFilterUpdated(() => this.reset()));
5456
this.list.search?.onUpdated(() => this.reset());
57+
58+
this.initStaticDataProperties();
5559
}
5660

5761
public static useNew<T>(
@@ -61,6 +65,27 @@ export class IncrementalLoader<T> {
6165
return new IncrementalLoader(list, shape);
6266
}
6367

68+
private initStaticDataProperties() {
69+
const addPropertiesOfDataEntry = (data: unknown) => {
70+
if (typeof data !== "object" || data === null) {
71+
return;
72+
}
73+
74+
(Object.keys(data) as PropertyName<T>[])
75+
.filter((p) => !this.staticDataProperties.includes(p))
76+
.forEach((p) => {
77+
this.staticDataProperties.push(p);
78+
});
79+
};
80+
81+
if ("staticData" in this.dataSource) {
82+
this.dataSource.staticData
83+
// collect properties from just the first 100 items
84+
.slice(0, 100)
85+
.forEach(addPropertiesOfDataEntry);
86+
}
87+
}
88+
6489
private reset(): void {
6590
this.loaderState.reset();
6691
}

0 commit comments

Comments
 (0)