tinybase 4.0.0 → 4.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/tools.cjs +1 -1
- package/lib/cjs/tools.cjs.gz +0 -0
- package/lib/cjs/ui-react-dom.cjs +1 -0
- package/lib/cjs/ui-react-dom.cjs.gz +0 -0
- package/lib/cjs/ui-react.cjs +1 -1
- package/lib/cjs/ui-react.cjs.gz +0 -0
- package/lib/cjs-es6/tools.cjs +1 -1
- package/lib/cjs-es6/tools.cjs.gz +0 -0
- package/lib/cjs-es6/ui-react-dom.cjs +1 -0
- package/lib/cjs-es6/ui-react-dom.cjs.gz +0 -0
- package/lib/cjs-es6/ui-react.cjs +1 -1
- package/lib/cjs-es6/ui-react.cjs.gz +0 -0
- package/lib/debug/tools.js +25 -4
- package/lib/debug/ui-react-dom.js +205 -0
- package/lib/debug/ui-react.js +21 -13
- package/lib/es6/tools.js +1 -1
- package/lib/es6/tools.js.gz +0 -0
- package/lib/es6/ui-react-dom.js +1 -0
- package/lib/es6/ui-react-dom.js.gz +0 -0
- package/lib/es6/ui-react.js +1 -1
- package/lib/es6/ui-react.js.gz +0 -0
- package/lib/tools.js +1 -1
- package/lib/tools.js.gz +0 -0
- package/lib/types/ui-react-dom.d.ts +544 -0
- package/lib/types/ui-react.d.ts +59 -18
- package/lib/types/with-schemas/internal/ui-react.d.ts +15 -0
- package/lib/types/with-schemas/ui-react-dom.d.ts +590 -0
- package/lib/types/with-schemas/ui-react.d.ts +44 -18
- package/lib/ui-react-dom.js +1 -0
- package/lib/ui-react-dom.js.gz +0 -0
- package/lib/ui-react.js +1 -1
- package/lib/ui-react.js.gz +0 -0
- package/lib/umd/tools.js +1 -1
- package/lib/umd/tools.js.gz +0 -0
- package/lib/umd/ui-react-dom.js +1 -0
- package/lib/umd/ui-react-dom.js.gz +0 -0
- package/lib/umd/ui-react.js +1 -1
- package/lib/umd/ui-react.js.gz +0 -0
- package/lib/umd-es6/tools.js +1 -1
- package/lib/umd-es6/tools.js.gz +0 -0
- package/lib/umd-es6/ui-react-dom.js +1 -0
- package/lib/umd-es6/ui-react-dom.js.gz +0 -0
- package/lib/umd-es6/ui-react.js +1 -1
- package/lib/umd-es6/ui-react.js.gz +0 -0
- package/package.json +32 -28
- package/readme.md +50 -33
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ui-react-dom module of the TinyBase project provides components to make
|
|
3
|
+
* it easy to create web-based reactive apps with Store objects.
|
|
4
|
+
*
|
|
5
|
+
* The components in this module use the react-dom module and so are not
|
|
6
|
+
* appropriate for environments like React Native (although those in the
|
|
7
|
+
* lower-level ui-react module are).
|
|
8
|
+
* @see UI Components demos
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
* @module ui-react-dom
|
|
11
|
+
* @since v4.1.0
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {CellIdFromSchema, TableIdFromSchema} from './internal/store';
|
|
15
|
+
import {
|
|
16
|
+
CellProps,
|
|
17
|
+
ComponentReturnType,
|
|
18
|
+
ExtraProps,
|
|
19
|
+
StoreOrStoreId,
|
|
20
|
+
ValueProps,
|
|
21
|
+
} from './internal/ui-react';
|
|
22
|
+
import {ComponentType} from 'react';
|
|
23
|
+
import {Id} from '../common';
|
|
24
|
+
import {OptionalSchemas} from '../store';
|
|
25
|
+
|
|
26
|
+
// TableInHtmlTableProps
|
|
27
|
+
export type TableInHtmlTableProps<
|
|
28
|
+
Schemas extends OptionalSchemas,
|
|
29
|
+
TableIds extends TableIdFromSchema<Schemas[0]> = TableIdFromSchema<
|
|
30
|
+
Schemas[0]
|
|
31
|
+
>,
|
|
32
|
+
> = TableIds extends infer TableId
|
|
33
|
+
? TableId extends TableIdFromSchema<Schemas[0]>
|
|
34
|
+
? {
|
|
35
|
+
/**
|
|
36
|
+
* The Id of the Table in the Store to be rendered.
|
|
37
|
+
*/
|
|
38
|
+
readonly tableId: TableId;
|
|
39
|
+
/**
|
|
40
|
+
* The Store to be accessed: omit for the default context Store, provide an Id
|
|
41
|
+
* for a named context Store, or provide an explicit reference.
|
|
42
|
+
*/
|
|
43
|
+
readonly store?: StoreOrStoreId<Schemas>;
|
|
44
|
+
/**
|
|
45
|
+
* A custom component for rendering each Cell in the Table (to override the
|
|
46
|
+
* default CellView component).
|
|
47
|
+
*/
|
|
48
|
+
readonly cellComponent?: ComponentType<CellProps<Schemas>>;
|
|
49
|
+
/**
|
|
50
|
+
* A function for generating extra props for each custom Cell component based
|
|
51
|
+
* on its Id.
|
|
52
|
+
*/
|
|
53
|
+
readonly getCellComponentProps?: (rowId: Id, cellId: Id) => ExtraProps;
|
|
54
|
+
/**
|
|
55
|
+
* A string className to use on the root of the resulting element.
|
|
56
|
+
*/
|
|
57
|
+
readonly className?: string;
|
|
58
|
+
/**
|
|
59
|
+
* Whether a header row should be rendered at the top of the table, defaulting
|
|
60
|
+
* to `true`.
|
|
61
|
+
*/
|
|
62
|
+
readonly headerRow?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Whether an Id column should be rendered on the left of the table,
|
|
65
|
+
* defaulting to `true`.
|
|
66
|
+
*/
|
|
67
|
+
readonly idColumn?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* An optional list of Cell Ids to use for rendering a prescribed set of the
|
|
70
|
+
* Row's Cells in a given order.
|
|
71
|
+
*/
|
|
72
|
+
readonly customCellIds?: CellIdFromSchema<Schemas[0], TableId>[];
|
|
73
|
+
}
|
|
74
|
+
: never
|
|
75
|
+
: never;
|
|
76
|
+
|
|
77
|
+
// SortedTableInHtmlTableProps
|
|
78
|
+
export type SortedTableInHtmlTableProps<
|
|
79
|
+
Schemas extends OptionalSchemas,
|
|
80
|
+
TableIds extends TableIdFromSchema<Schemas[0]> = TableIdFromSchema<
|
|
81
|
+
Schemas[0]
|
|
82
|
+
>,
|
|
83
|
+
> = TableIds extends infer TableId
|
|
84
|
+
? TableId extends TableIdFromSchema<Schemas[0]>
|
|
85
|
+
? {
|
|
86
|
+
/**
|
|
87
|
+
* The Id of the Table in the Store to be rendered.
|
|
88
|
+
*/
|
|
89
|
+
readonly tableId: TableId;
|
|
90
|
+
/**
|
|
91
|
+
* The Id of the Cell whose values are used for the sorting. If omitted, the
|
|
92
|
+
* view will sort the Row Id itself.
|
|
93
|
+
*/
|
|
94
|
+
readonly cellId?: Id;
|
|
95
|
+
/**
|
|
96
|
+
* Whether the sorting should be in descending order.
|
|
97
|
+
*/
|
|
98
|
+
readonly descending?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* The number of Row Ids to skip for pagination purposes.
|
|
101
|
+
*/
|
|
102
|
+
readonly offset?: number;
|
|
103
|
+
/**
|
|
104
|
+
* The maximum number of Row Ids to return.
|
|
105
|
+
*/
|
|
106
|
+
readonly limit?: number;
|
|
107
|
+
/**
|
|
108
|
+
* The Store to be accessed: omit for the default context Store, provide an Id
|
|
109
|
+
* for a named context Store, or provide an explicit reference.
|
|
110
|
+
*/
|
|
111
|
+
readonly store?: StoreOrStoreId<Schemas>;
|
|
112
|
+
/**
|
|
113
|
+
* A custom component for rendering each Cell in the Table (to override the
|
|
114
|
+
* default CellView component).
|
|
115
|
+
*/
|
|
116
|
+
readonly cellComponent?: ComponentType<CellProps<Schemas>>;
|
|
117
|
+
/**
|
|
118
|
+
* A function for generating extra props for each custom Cell component based
|
|
119
|
+
* on its Id.
|
|
120
|
+
*/
|
|
121
|
+
readonly getCellComponentProps?: (rowId: Id, cellId: Id) => ExtraProps;
|
|
122
|
+
/**
|
|
123
|
+
* A string className to use on the root of the resulting element.
|
|
124
|
+
*/
|
|
125
|
+
readonly className?: string;
|
|
126
|
+
/**
|
|
127
|
+
* Whether a header row should be rendered at the top of the table, defaulting
|
|
128
|
+
* to `true`.
|
|
129
|
+
*/
|
|
130
|
+
readonly headerRow?: boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Whether an Id column should be rendered on the left of the table,
|
|
133
|
+
* defaulting to `true`.
|
|
134
|
+
*/
|
|
135
|
+
readonly idColumn?: boolean;
|
|
136
|
+
/**
|
|
137
|
+
* An optional list of Cell Ids to use for rendering a prescribed set of the
|
|
138
|
+
* Row's Cells in a given order.
|
|
139
|
+
*/
|
|
140
|
+
readonly customCellIds?: CellIdFromSchema<Schemas[0], TableId>[];
|
|
141
|
+
/**
|
|
142
|
+
* Whether the table should be interactive such that clicking a header changes
|
|
143
|
+
* the sorting and/or direction.
|
|
144
|
+
*/
|
|
145
|
+
readonly sortOnClick?: boolean;
|
|
146
|
+
}
|
|
147
|
+
: never
|
|
148
|
+
: never;
|
|
149
|
+
|
|
150
|
+
// ValuesInHtmlTableProps
|
|
151
|
+
export type ValuesInHtmlTableProps<Schemas extends OptionalSchemas> = {
|
|
152
|
+
/**
|
|
153
|
+
* The Store to be accessed: omit for the default context Store, provide an Id
|
|
154
|
+
* for a named context Store, or provide an explicit reference.
|
|
155
|
+
*/
|
|
156
|
+
readonly store?: StoreOrStoreId<Schemas>;
|
|
157
|
+
/**
|
|
158
|
+
* A custom component for rendering each Value in the Store (to override the
|
|
159
|
+
* default ValueView component).
|
|
160
|
+
*/
|
|
161
|
+
readonly valueComponent?: ComponentType<ValueProps<Schemas>>;
|
|
162
|
+
/**
|
|
163
|
+
* A function for generating extra props for each custom Value component based
|
|
164
|
+
* on its Id.
|
|
165
|
+
*/
|
|
166
|
+
readonly getValueComponentProps?: (valueId: Id) => ExtraProps;
|
|
167
|
+
/**
|
|
168
|
+
* A string className to use on the root of the resulting element.
|
|
169
|
+
*/
|
|
170
|
+
readonly className?: string;
|
|
171
|
+
/**
|
|
172
|
+
* Whether a header row should be rendered at the top of the table, defaulting
|
|
173
|
+
* to `true`.
|
|
174
|
+
*/
|
|
175
|
+
readonly headerRow?: boolean;
|
|
176
|
+
/**
|
|
177
|
+
* Whether an Id column should be rendered on the left of the table,
|
|
178
|
+
* defaulting to `true`.
|
|
179
|
+
*/
|
|
180
|
+
readonly idColumn?: boolean;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export type WithSchemas<Schemas extends OptionalSchemas> = {
|
|
184
|
+
/**
|
|
185
|
+
* The TableInHtmlTable component renders the contents of a single Table in a
|
|
186
|
+
* Store as an HTML <table> element, and registers a listener so that any
|
|
187
|
+
* changes to that result will cause a re-render.
|
|
188
|
+
*
|
|
189
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
190
|
+
*
|
|
191
|
+
* ```ts override
|
|
192
|
+
* TableInHtmlTable(
|
|
193
|
+
* props: TableInHtmlTableProps,
|
|
194
|
+
* ): ComponentReturnType;
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* The component's props identify which Table to render based on Table Id, and
|
|
198
|
+
* Store (which is either the default context Store, a named context Store, or
|
|
199
|
+
* by explicit reference).
|
|
200
|
+
*
|
|
201
|
+
* See the <TableInHtmlTable /> demo for this component in action.
|
|
202
|
+
*
|
|
203
|
+
* This component renders a Table by iterating over its Row objects. By default
|
|
204
|
+
* these are in turn rendered with the RowInHtmlTr component, but you can
|
|
205
|
+
* override this behavior by providing a `rowComponent` prop, a custom component
|
|
206
|
+
* of your own that will render a Row based on RowProps. You can also pass
|
|
207
|
+
* additional props to your custom component with the `getRowComponentProps`
|
|
208
|
+
* callback prop.
|
|
209
|
+
*
|
|
210
|
+
* This component uses the useRowIds hook under the covers, which means that any
|
|
211
|
+
* changes to the structure of the Table will cause a re-render.
|
|
212
|
+
*
|
|
213
|
+
* You can use the `headerRow` and `idColumn` props to control whether the Ids
|
|
214
|
+
* appear in a <th> element at the top of the table, and the start of each row.
|
|
215
|
+
* @param props The props for this component.
|
|
216
|
+
* @returns A rendering of the Table in a <table> element.
|
|
217
|
+
* @see <TableInHtmlTable /> demo
|
|
218
|
+
* @example
|
|
219
|
+
* This example creates a Provider context into which a default Store is
|
|
220
|
+
* provided. The TableInHtmlTable component within it then renders the Table in
|
|
221
|
+
* a <table> element with a CSS class.
|
|
222
|
+
*
|
|
223
|
+
* ```jsx
|
|
224
|
+
* const App = ({store}) => (
|
|
225
|
+
* <Provider store={store}>
|
|
226
|
+
* <Pane />
|
|
227
|
+
* </Provider>
|
|
228
|
+
* );
|
|
229
|
+
* const Pane = () => <TableInHtmlTable tableId="pets" className="table" />;
|
|
230
|
+
*
|
|
231
|
+
* const store = createStore().setTable('pets', {
|
|
232
|
+
* fido: {species: 'dog'},
|
|
233
|
+
* felix: {species: 'cat'},
|
|
234
|
+
* });
|
|
235
|
+
* const app = document.createElement('div');
|
|
236
|
+
* ReactDOMClient.createRoot(app).render(<App store={store} />); // !act
|
|
237
|
+
* console.log(app.innerHTML);
|
|
238
|
+
* // ->
|
|
239
|
+
* `
|
|
240
|
+
* <table class="table">
|
|
241
|
+
* <thead>
|
|
242
|
+
* <tr>
|
|
243
|
+
* <th>Id</th>
|
|
244
|
+
* <th>species</th>
|
|
245
|
+
* </tr>
|
|
246
|
+
* </thead>
|
|
247
|
+
* <tbody>
|
|
248
|
+
* <tr>
|
|
249
|
+
* <th>fido</th>
|
|
250
|
+
* <td>dog</td>
|
|
251
|
+
* </tr>
|
|
252
|
+
* <tr>
|
|
253
|
+
* <th>felix</th>
|
|
254
|
+
* <td>cat</td>
|
|
255
|
+
* </tr>
|
|
256
|
+
* </tbody>
|
|
257
|
+
* </table>
|
|
258
|
+
* `;
|
|
259
|
+
* ```
|
|
260
|
+
* @example
|
|
261
|
+
* This example creates a Provider context into which a default Store is
|
|
262
|
+
* provided. The TableInHtmlTable component within it then renders the Table
|
|
263
|
+
* with a custom Row component and a custom props callback. The header row at
|
|
264
|
+
* the top of the table and the Id column at the start of each row is removed.
|
|
265
|
+
*
|
|
266
|
+
* ```jsx
|
|
267
|
+
* const App = ({store}) => (
|
|
268
|
+
* <Provider store={store}>
|
|
269
|
+
* <Pane />
|
|
270
|
+
* </Provider>
|
|
271
|
+
* );
|
|
272
|
+
* const Pane = () => (
|
|
273
|
+
* <TableInHtmlTable
|
|
274
|
+
* tableId="pets"
|
|
275
|
+
* cellComponent={FormattedCellView}
|
|
276
|
+
* getCellComponentProps={(rowId, cellId) => ({bold: rowId == 'fido'})}
|
|
277
|
+
* headerRow={false}
|
|
278
|
+
* idColumn={false}
|
|
279
|
+
* />
|
|
280
|
+
* );
|
|
281
|
+
* const FormattedCellView = ({tableId, rowId, cellId, bold}) => (
|
|
282
|
+
* <>
|
|
283
|
+
* {bold ? <b>{rowId}</b> : rowId}:
|
|
284
|
+
* <CellView tableId={tableId} rowId={rowId} cellId={cellId} />
|
|
285
|
+
* </>
|
|
286
|
+
* );
|
|
287
|
+
*
|
|
288
|
+
* const store = createStore().setTable('pets', {
|
|
289
|
+
* fido: {species: 'dog'},
|
|
290
|
+
* felix: {species: 'cat'},
|
|
291
|
+
* });
|
|
292
|
+
* const app = document.createElement('div');
|
|
293
|
+
* ReactDOMClient.createRoot(app).render(<App store={store} />); // !act
|
|
294
|
+
* console.log(app.innerHTML);
|
|
295
|
+
* // ->
|
|
296
|
+
* `
|
|
297
|
+
* <table>
|
|
298
|
+
* <tbody>
|
|
299
|
+
* <tr>
|
|
300
|
+
* <td><b>fido</b>:dog</td>
|
|
301
|
+
* </tr>
|
|
302
|
+
* <tr>
|
|
303
|
+
* <td>felix:cat</td>
|
|
304
|
+
* </tr>
|
|
305
|
+
* </tbody>
|
|
306
|
+
* </table>
|
|
307
|
+
* `;
|
|
308
|
+
* ```
|
|
309
|
+
* @category Store components
|
|
310
|
+
* @since v4.1.0
|
|
311
|
+
*/
|
|
312
|
+
TableInHtmlTable: (
|
|
313
|
+
props: TableInHtmlTableProps<Schemas>,
|
|
314
|
+
) => ComponentReturnType;
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* The SortedTableInHtmlTable component renders the contents of a single sorted
|
|
318
|
+
* Table in a Store, as an HTML <table> element, and registers a listener so
|
|
319
|
+
* that any changes to that result will cause a re-render.
|
|
320
|
+
*
|
|
321
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
322
|
+
*
|
|
323
|
+
* ```ts override
|
|
324
|
+
* SortedTableInHtmlTable(
|
|
325
|
+
* props: SortedTableInHtmlTableProps,
|
|
326
|
+
* ): ComponentReturnType;
|
|
327
|
+
* ```
|
|
328
|
+
*
|
|
329
|
+
* The component's props identify which Table to render based on Table Id, and
|
|
330
|
+
* Store (which is either the default context Store, a named context Store, or
|
|
331
|
+
* by explicit reference). It also takes a Cell Id to sort by and a boolean to
|
|
332
|
+
* indicate that the sorting should be in descending order. The `offset` and
|
|
333
|
+
* `limit` props are used to paginate results, but default to `0` and
|
|
334
|
+
* `undefined` to return all available Row Ids if not specified.
|
|
335
|
+
*
|
|
336
|
+
* See the <SortedTableInHtmlTable /> demo for this component in action.
|
|
337
|
+
*
|
|
338
|
+
* This component renders a Table by iterating over its Row objects, in the
|
|
339
|
+
* order dictated by the sort parameters. By default these are in turn rendered
|
|
340
|
+
* with the RowInHtmlTr component, but you can override this behavior by
|
|
341
|
+
* providing a `rowComponent` prop, a custom component of your own that will
|
|
342
|
+
* render a Row based on RowProps. You can also pass additional props to your
|
|
343
|
+
* custom component with the `getRowComponentProps` callback prop.
|
|
344
|
+
*
|
|
345
|
+
* This component uses the useSortedRowIds hook under the covers, which means
|
|
346
|
+
* that any changes to the structure or sorting of the Table will cause a
|
|
347
|
+
* re-render.
|
|
348
|
+
*
|
|
349
|
+
* You can use the `headerRow` and `idColumn` props to control whether the Ids
|
|
350
|
+
* appear in a <th> element at the top of the table, and the start of each row.
|
|
351
|
+
*
|
|
352
|
+
* The `sortOnClick` prop makes the table's sorting interactive such that the
|
|
353
|
+
* user can click on a column heading to sort by that column. The style classes
|
|
354
|
+
* `sorted` and `ascending` (or `descending`) are added so that you can provide
|
|
355
|
+
* hints to the user how the sorting is being applied.
|
|
356
|
+
* @param props The props for this component.
|
|
357
|
+
* @returns A rendering of the Table in a <table> element.
|
|
358
|
+
* @example
|
|
359
|
+
* This example creates a Provider context into which a default Store is
|
|
360
|
+
* provided. The SortedTableInHtmlTable component within it then renders the
|
|
361
|
+
* Table in a <table> element with a CSS class.
|
|
362
|
+
*
|
|
363
|
+
* ```jsx
|
|
364
|
+
* const App = ({store}) => (
|
|
365
|
+
* <Provider store={store}>
|
|
366
|
+
* <Pane />
|
|
367
|
+
* </Provider>
|
|
368
|
+
* );
|
|
369
|
+
* const Pane = () => (
|
|
370
|
+
* <SortedTableInHtmlTable
|
|
371
|
+
* tableId="pets"
|
|
372
|
+
* cellId="species"
|
|
373
|
+
* className="table"
|
|
374
|
+
* />
|
|
375
|
+
* );
|
|
376
|
+
*
|
|
377
|
+
* const store = createStore().setTables({
|
|
378
|
+
* pets: {
|
|
379
|
+
* fido: {species: 'dog'},
|
|
380
|
+
* felix: {species: 'cat'},
|
|
381
|
+
* },
|
|
382
|
+
* });
|
|
383
|
+
* const app = document.createElement('div');
|
|
384
|
+
* ReactDOMClient.createRoot(app).render(<App store={store} />); // !act
|
|
385
|
+
* console.log(app.innerHTML);
|
|
386
|
+
* // ->
|
|
387
|
+
* `
|
|
388
|
+
* <table class="table">
|
|
389
|
+
* <thead>
|
|
390
|
+
* <tr>
|
|
391
|
+
* <th>Id</th>
|
|
392
|
+
* <th class="sorted ascending">species</th>
|
|
393
|
+
* </tr>
|
|
394
|
+
* </thead>
|
|
395
|
+
* <tbody>
|
|
396
|
+
* <tr>
|
|
397
|
+
* <th>felix</th>
|
|
398
|
+
* <td>cat</td>
|
|
399
|
+
* </tr>
|
|
400
|
+
* <tr>
|
|
401
|
+
* <th>fido</th>
|
|
402
|
+
* <td>dog</td>
|
|
403
|
+
* </tr>
|
|
404
|
+
* </tbody>
|
|
405
|
+
* </table>
|
|
406
|
+
* `;
|
|
407
|
+
* ```
|
|
408
|
+
* @example
|
|
409
|
+
* This example creates a Provider context into which a default Store is
|
|
410
|
+
* provided. The SortedTableInHtmlTable component within it then renders the
|
|
411
|
+
* Table with a custom Row component and a custom props callback. The header row
|
|
412
|
+
* at the top of the table and the Id column at the start of each row is
|
|
413
|
+
* removed.
|
|
414
|
+
*
|
|
415
|
+
* ```jsx
|
|
416
|
+
* const App = ({store}) => (
|
|
417
|
+
* <Provider store={store}>
|
|
418
|
+
* <Pane />
|
|
419
|
+
* </Provider>
|
|
420
|
+
* );
|
|
421
|
+
* const Pane = () => (
|
|
422
|
+
* <SortedTableInHtmlTable
|
|
423
|
+
* tableId="pets"
|
|
424
|
+
* cellId="species"
|
|
425
|
+
* cellComponent={FormattedCellView}
|
|
426
|
+
* getCellComponentProps={(rowId, cellId) => ({bold: rowId == 'fido'})}
|
|
427
|
+
* headerRow={false}
|
|
428
|
+
* idColumn={false}
|
|
429
|
+
* />
|
|
430
|
+
* );
|
|
431
|
+
* const FormattedCellView = ({tableId, rowId, cellId, bold}) => (
|
|
432
|
+
* <>
|
|
433
|
+
* {bold ? <b>{rowId}</b> : rowId}:
|
|
434
|
+
* <CellView tableId={tableId} rowId={rowId} cellId={cellId} />
|
|
435
|
+
* </>
|
|
436
|
+
* );
|
|
437
|
+
*
|
|
438
|
+
* const store = createStore().setTables({
|
|
439
|
+
* pets: {
|
|
440
|
+
* fido: {species: 'dog'},
|
|
441
|
+
* felix: {species: 'cat'},
|
|
442
|
+
* },
|
|
443
|
+
* });
|
|
444
|
+
* const app = document.createElement('div');
|
|
445
|
+
* ReactDOMClient.createRoot(app).render(<App store={store} />); // !act
|
|
446
|
+
* console.log(app.innerHTML);
|
|
447
|
+
* // ->
|
|
448
|
+
* `
|
|
449
|
+
* <table>
|
|
450
|
+
* <tbody>
|
|
451
|
+
* <tr>
|
|
452
|
+
* <td>felix:cat</td>
|
|
453
|
+
* </tr>
|
|
454
|
+
* <tr>
|
|
455
|
+
* <td><b>fido</b>:dog</td>
|
|
456
|
+
* </tr>
|
|
457
|
+
* </tbody>
|
|
458
|
+
* </table>
|
|
459
|
+
* `;
|
|
460
|
+
* ```
|
|
461
|
+
* @category Store components
|
|
462
|
+
* @since v4.1.0
|
|
463
|
+
*/
|
|
464
|
+
SortedTableInHtmlTable: (
|
|
465
|
+
props: SortedTableInHtmlTableProps<Schemas>,
|
|
466
|
+
) => ComponentReturnType;
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* The ValuesInHtmlTable component renders the keyed value contents of a Store
|
|
470
|
+
* as an HTML <table> element, and registers a listener so that any changes to
|
|
471
|
+
* that result will cause a re-render.
|
|
472
|
+
*
|
|
473
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
474
|
+
*
|
|
475
|
+
* ```ts override
|
|
476
|
+
* ValuesInHtmlTable(
|
|
477
|
+
* props: ValuesInHtmlTableProps,
|
|
478
|
+
* ): ComponentReturnType;
|
|
479
|
+
* ```
|
|
480
|
+
*
|
|
481
|
+
* The component's props identify which Row to render based on Table Id, Row Id,
|
|
482
|
+
* and Store (which is either the default context Store, a named context Store,
|
|
483
|
+
* or an explicit reference).
|
|
484
|
+
*
|
|
485
|
+
* See the <ValuesInHtmlTable /> demo for this component in action.
|
|
486
|
+
*
|
|
487
|
+
* This component renders a Store by iterating over its Value objects. By
|
|
488
|
+
* default these are in turn rendered with the ValueInHtmlTr component, but you
|
|
489
|
+
* can override this behavior by providing a `valueComponent` prop, a custom
|
|
490
|
+
* component of your own that will render a Value based on ValueProps. You can
|
|
491
|
+
* also pass additional props to your custom component with the
|
|
492
|
+
* `getValueComponentProps` callback prop.
|
|
493
|
+
*
|
|
494
|
+
* This component uses the useValueIds hook under the covers, which means that
|
|
495
|
+
* any changes to the structure of the Values in the Store will cause a
|
|
496
|
+
* re-render.
|
|
497
|
+
*
|
|
498
|
+
* You can use the `headerRow` and `idColumn` props to control whether labels
|
|
499
|
+
* and Ids appear in a <th> element at the top of the table, and the start of
|
|
500
|
+
* each row.
|
|
501
|
+
* @param props The props for this component.
|
|
502
|
+
* @returns A rendering of the Values in a <table> element.
|
|
503
|
+
* @example
|
|
504
|
+
* This example creates a Provider context into which a default Store is
|
|
505
|
+
* provided. The ValuesInHtmlTable component within it then renders the Values
|
|
506
|
+
* in a <table> element with a CSS class.
|
|
507
|
+
*
|
|
508
|
+
* ```jsx
|
|
509
|
+
* const App = ({store}) => (
|
|
510
|
+
* <Provider store={store}>
|
|
511
|
+
* <Pane />
|
|
512
|
+
* </Provider>
|
|
513
|
+
* );
|
|
514
|
+
* const Pane = () => <ValuesInHtmlTable className="values" />;
|
|
515
|
+
*
|
|
516
|
+
* const store = createStore().setValues({open: true, employees: 3});
|
|
517
|
+
* const app = document.createElement('div');
|
|
518
|
+
* ReactDOMClient.createRoot(app).render(<App store={store} />); // !act
|
|
519
|
+
* console.log(app.innerHTML);
|
|
520
|
+
* // ->
|
|
521
|
+
* `
|
|
522
|
+
* <table class="values">
|
|
523
|
+
* <thead>
|
|
524
|
+
* <tr>
|
|
525
|
+
* <th>Id</th>
|
|
526
|
+
* <th>Value</th>
|
|
527
|
+
* </tr>
|
|
528
|
+
* </thead>
|
|
529
|
+
* <tbody>
|
|
530
|
+
* <tr>
|
|
531
|
+
* <th>open</th>
|
|
532
|
+
* <td>true</td>
|
|
533
|
+
* </tr>
|
|
534
|
+
* <tr>
|
|
535
|
+
* <th>employees</th>
|
|
536
|
+
* <td>3</td>
|
|
537
|
+
* </tr>
|
|
538
|
+
* </tbody>
|
|
539
|
+
* </table>
|
|
540
|
+
* `;
|
|
541
|
+
* ```
|
|
542
|
+
* @example
|
|
543
|
+
* This example creates a Provider context into which a default Store is
|
|
544
|
+
* provided. The ValuesInHtmlTable component within it then renders the Row
|
|
545
|
+
* with a custom Cell component and a custom props callback. The header row at
|
|
546
|
+
* the top of the table and the Id column at the start of each row is removed.
|
|
547
|
+
*
|
|
548
|
+
* ```jsx
|
|
549
|
+
* const App = ({store}) => (
|
|
550
|
+
* <Provider store={store}>
|
|
551
|
+
* <Pane />
|
|
552
|
+
* </Provider>
|
|
553
|
+
* );
|
|
554
|
+
* const Pane = () => (
|
|
555
|
+
* <ValuesInHtmlTable
|
|
556
|
+
* valueComponent={FormattedValueView}
|
|
557
|
+
* getValueComponentProps={(valueId) => ({bold: valueId == 'open'})}
|
|
558
|
+
* headerRow={false}
|
|
559
|
+
* idColumn={false}
|
|
560
|
+
* />
|
|
561
|
+
* );
|
|
562
|
+
* const FormattedValueView = ({valueId, bold}) => (
|
|
563
|
+
* <>
|
|
564
|
+
* {bold ? <b>{valueId}</b> : valueId}
|
|
565
|
+
* {': '}
|
|
566
|
+
* <ValueView valueId={valueId} />
|
|
567
|
+
* </>
|
|
568
|
+
* );
|
|
569
|
+
*
|
|
570
|
+
* const store = createStore().setValues({open: true, employees: 3});
|
|
571
|
+
* const app = document.createElement('div');
|
|
572
|
+
* ReactDOMClient.createRoot(app).render(<App store={store} />); // !act
|
|
573
|
+
* console.log(app.innerHTML);
|
|
574
|
+
* // ->
|
|
575
|
+
* `
|
|
576
|
+
* <table>
|
|
577
|
+
* <tbody>
|
|
578
|
+
* <tr><td><b>open</b>: true</td></tr>
|
|
579
|
+
* <tr><td>employees: 3</td></tr>
|
|
580
|
+
* </tbody>
|
|
581
|
+
* </table>
|
|
582
|
+
* `;
|
|
583
|
+
* ```
|
|
584
|
+
* @category Store components
|
|
585
|
+
* @since v4.1.0
|
|
586
|
+
*/
|
|
587
|
+
ValuesInHtmlTable: (
|
|
588
|
+
props: ValuesInHtmlTableProps<Schemas>,
|
|
589
|
+
) => ComponentReturnType;
|
|
590
|
+
};
|