-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdata-list.tsx
More file actions
181 lines (163 loc) · 5.82 KB
/
Copy pathdata-list.tsx
File metadata and controls
181 lines (163 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* Lightweight list primitives for SIMPLE data — the antidote to dropping a
* full data-grid (toolbar + filters + pagination + selection) on a handful of
* reference rows. Two presentational/low-chrome components:
*
* - element:definition-list — a compact key/value `<dl>` for a single record.
* - element:repeater — a data-bound, chrome-free list: one line per
* row, no toolbar/card/pagination.
*
* Props are read off `schema.properties` (spec convention) with a `schema.props`
* fallback, matching the other `element:*` renderers.
*/
import * as React from 'react';
import { ComponentRegistry } from '@object-ui/core';
import { useAdapter } from '@object-ui/react';
import { cn } from '../../lib/utils';
function readProps<T extends Record<string, any>>(schema: any): T {
const fromProperties = (schema?.properties ?? {}) as T;
const fromProps = (schema?.props ?? {}) as T;
return { ...fromProps, ...fromProperties };
}
function toText(v: unknown): string {
if (v == null || v === '') return '—';
if (typeof v === 'object') return JSON.stringify(v);
return String(v);
}
// ---------------------------------------------------------------------------
// element:definition-list — compact key/value display
// ---------------------------------------------------------------------------
interface DefinitionItem {
term: string;
description?: unknown;
}
function DefinitionListRenderer({ schema }: { schema: any }) {
const props = readProps<{
items?: DefinitionItem[];
columns?: 1 | 2;
inline?: boolean;
className?: string;
}>(schema);
const items = Array.isArray(props.items) ? props.items : [];
const cols = props.columns === 2 ? 'sm:grid-cols-2' : 'grid-cols-1';
if (items.length === 0) {
return <p className="text-sm text-muted-foreground">No details</p>;
}
return (
<dl
className={cn('grid gap-x-6 gap-y-3', cols, schema?.className, props.className)}
data-testid="definition-list"
>
{items.map((it, i) => (
<div
key={i}
className={cn(props.inline ? 'flex items-baseline justify-between gap-3' : 'flex flex-col gap-0.5')}
>
<dt className="text-xs font-medium uppercase tracking-wide text-muted-foreground">
{it.term}
</dt>
<dd className="text-sm text-foreground">{toText(it.description)}</dd>
</div>
))}
</dl>
);
}
ComponentRegistry.register('definition-list', DefinitionListRenderer, {
namespace: 'element',
skipFallback: true,
label: 'Definition List',
category: 'content',
});
// ---------------------------------------------------------------------------
// element:repeater — data-bound, chrome-free list
// ---------------------------------------------------------------------------
interface RepeaterColumn {
field: string;
label?: string;
}
function RepeaterRenderer({ schema }: { schema: any }) {
const props = readProps<{
object?: string;
titleField?: string;
fields?: Array<string | RepeaterColumn>;
filter?: unknown;
sort?: any;
limit?: number;
emptyText?: string;
divided?: boolean;
className?: string;
}>(schema);
const adapter = useAdapter() as any;
const [rows, setRows] = React.useState<any[]>([]);
const [loading, setLoading] = React.useState(true);
const [error, setError] = React.useState<string | null>(null);
const filterKey = React.useMemo(() => (props.filter ? JSON.stringify(props.filter) : ''), [props.filter]);
const cols: RepeaterColumn[] = React.useMemo(
() => (props.fields ?? []).map((f) => (typeof f === 'string' ? { field: f } : f)),
[props.fields],
);
React.useEffect(() => {
let cancelled = false;
if (!adapter || !props.object || typeof adapter.find !== 'function') {
setLoading(false);
return;
}
setLoading(true);
setError(null);
(async () => {
try {
const query: any = {};
if (props.filter) query.$filter = props.filter;
if (props.sort) query.$orderby = props.sort;
if (props.limit) query.$top = props.limit;
const res = await adapter.find(props.object, query);
const data: any[] = res?.data ?? res?.records ?? (Array.isArray(res) ? res : []);
if (!cancelled) setRows(data);
} catch (e: any) {
if (!cancelled) setError(e?.message ?? 'Failed to load');
} finally {
if (!cancelled) setLoading(false);
}
})();
return () => {
cancelled = true;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [adapter, props.object, filterKey, props.limit]);
if (loading) return <p className="py-2 text-sm text-muted-foreground">Loading…</p>;
if (error) return <p className="py-2 text-sm text-destructive">{error}</p>;
if (rows.length === 0) {
return <p className="py-2 text-sm text-muted-foreground">{props.emptyText ?? 'No records'}</p>;
}
return (
<ul
className={cn(props.divided !== false && 'divide-y divide-border', schema?.className, props.className)}
data-testid="repeater"
>
{rows.map((row, i) => (
<li key={row?.id ?? i} className="flex items-baseline gap-3 py-2">
{props.titleField && (
<span className="text-sm font-medium text-foreground">{toText(row[props.titleField])}</span>
)}
{cols.map((c) => (
<span key={c.field} className="text-sm text-muted-foreground">
{toText(row[c.field])}
</span>
))}
</li>
))}
</ul>
);
}
ComponentRegistry.register('repeater', RepeaterRenderer, {
namespace: 'element',
skipFallback: true,
label: 'Repeater',
category: 'content',
});