Skip to content

Commit da6420f

Browse files
committed
[store] useTableCellIds
1 parent 353d157 commit da6420f

File tree

7 files changed

+421
-270
lines changed

7 files changed

+421
-270
lines changed

src/types/docs/ui-react.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,88 @@
515515
* @category Store hooks
516516
*/
517517
/// useTable
518+
/**
519+
* The useTableCellIds hook returns the Ids of every Cell used across the whole
520+
* Table, and registers a listener so that any changes to that result will cause
521+
* a re-render.
522+
*
523+
* A Provider component is used to wrap part of an application in a context, and
524+
* it can contain a default Store or a set of Store objects named by Id. The
525+
* useTableCellIds hook lets you indicate which Store to get data for: omit the
526+
* optional final parameter for the default context Store, provide an Id for a
527+
* named context Store, or provide a Store explicitly by reference.
528+
*
529+
* When first rendered, this hook will create a listener so that changes to the
530+
* Table Cell Ids will cause a re-render. When the component containing this
531+
* hook is unmounted, the listener will be automatically removed.
532+
*
533+
* @param tableId The Id of the Table in the Store.
534+
* @param storeOrStoreId The Store to be accessed: omit for the default context
535+
* Store, provide an Id for a named context Store, or provide an explicit
536+
* reference.
537+
* @returns An array of the Ids of every Cell used across the whole Table.
538+
* @example
539+
* This example creates a Store outside the application, which is used in the
540+
* useTableCellIds hook by reference. A change to the data in the Store
541+
* re-renders the component.
542+
*
543+
* ```jsx
544+
* const store = createStore().setCell('pets', 'fido', 'color', 'brown');
545+
* const App = () => (
546+
* <span>{JSON.stringify(useTableCellIds('pets', store))}</span>
547+
* );
548+
*
549+
* const app = document.createElement('div');
550+
* ReactDOMClient.createRoot(app).render(<App />); // !act
551+
* console.log(app.innerHTML);
552+
* // -> '<span>["color"]</span>'
553+
*
554+
* store.setCell('pets', 'felix', 'species', 'cat'); // !act
555+
* console.log(app.innerHTML);
556+
* // -> '<span>["color","species"]</span>'
557+
* ```
558+
* @example
559+
* This example creates a Provider context into which a default Store is
560+
* provided. A component within it then uses the useTableCellIds hook.
561+
*
562+
* ```jsx
563+
* const App = ({store}) => (
564+
* <Provider store={store}>
565+
* <Pane />
566+
* </Provider>
567+
* );
568+
* const Pane = () => <span>{JSON.stringify(useTableCellIds('pets'))}</span>;
569+
*
570+
* const store = createStore().setCell('pets', 'fido', 'color', 'brown');
571+
* const app = document.createElement('div');
572+
* ReactDOMClient.createRoot(app).render(<App store={store} />); // !act
573+
* console.log(app.innerHTML);
574+
* // -> '<span>["color"]</span>'
575+
* ```
576+
* @example
577+
* This example creates a Provider context into which a Store is provided, named
578+
* by Id. A component within it then uses the useTableCellIds hook.
579+
*
580+
* ```jsx
581+
* const App = ({store}) => (
582+
* <Provider storesById={{petStore: store}}>
583+
* <Pane />
584+
* </Provider>
585+
* );
586+
* const Pane = () => (
587+
* <span>{JSON.stringify(useTableCellIds('pets', 'petStore'))}</span>
588+
* );
589+
*
590+
* const store = createStore().setCell('pets', 'fido', 'color', 'brown');
591+
* const app = document.createElement('div');
592+
* ReactDOMClient.createRoot(app).render(<App store={store} />); // !act
593+
* console.log(app.innerHTML);
594+
* // -> '<span>["color"]</span>'
595+
* ```
596+
* @category Store hooks
597+
* @since v3.3
598+
*/
599+
/// useTableCellIds
518600
/**
519601
* The useRowIds hook returns the Ids of every Row in a given Table, and
520602
* registers a listener so that any changes to that result will cause a
@@ -594,6 +676,7 @@
594676
* @category Store hooks
595677
*/
596678
/// useRowIds
679+
597680
/**
598681
* The useSortedRowIds hook returns the sorted (and optionally, paginated) Ids
599682
* of every Row in a given Table, and registers a listener so that any changes

src/types/ui-react.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ export function useTableIds(storeOrStoreId?: StoreOrStoreId): Ids;
8989
/// useTable
9090
export function useTable(tableId: Id, storeOrStoreId?: StoreOrStoreId): Table;
9191

92+
/// useTableCellIds
93+
export function useTableCellIds(
94+
tableId: Id,
95+
storeOrStoreId?: StoreOrStoreId,
96+
): Ids;
97+
9298
/// useRowIds
9399
export function useRowIds(tableId: Id, storeOrStoreId?: StoreOrStoreId): Ids;
94100

src/types/with-schemas/ui-react.d.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
139139
storeOrStoreId?: StoreOrStoreId<Schemas>,
140140
) => Table<Schemas[0], TableId>;
141141

142+
/// useTableCellIds
143+
useTableCellIds: <TableId extends TableIdFromSchema<Schemas[0]>>(
144+
tableId: TableId,
145+
storeOrStoreId?: StoreOrStoreId<Schemas>,
146+
) => CellIdFromSchema<Schemas[0], TableId>[];
147+
142148
/// useRowIds
143149
useRowIds: (
144150
tableId: TableIdFromSchema<Schemas[0]>,
@@ -166,14 +172,11 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
166172
) => Row<Schemas[0], TableId>;
167173

168174
/// useCellIds
169-
useCellIds: <
170-
TableId extends TableIdFromSchema<Schemas[0]>,
171-
Ids extends CellIdFromSchema<Schemas[0], TableId>[],
172-
>(
175+
useCellIds: <TableId extends TableIdFromSchema<Schemas[0]>>(
173176
tableId: TableId,
174177
rowId: Id,
175178
storeOrStoreId?: StoreOrStoreId<Schemas>,
176-
) => Ids;
179+
) => CellIdFromSchema<Schemas[0], TableId>[];
177180

178181
/// useCell
179182
useCell: <

src/ui-react/hooks.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ import {
123123
useSliceRowIdsListener as useSliceRowIdsListenerDecl,
124124
useSortedRowIds as useSortedRowIdsDecl,
125125
useSortedRowIdsListener as useSortedRowIdsListenerDecl,
126+
useTableCellIds as useTableCellIdsDecl,
126127
useTable as useTableDecl,
127128
useTableIds as useTableIdsDecl,
128129
useTableIdsListener as useTableIdsListenerDecl,
@@ -318,6 +319,17 @@ export const useTable: typeof useTableDecl = (
318319
): Table =>
319320
useListenable(TABLE, useStoreOrStoreId(storeOrStoreId), {}, [tableId]);
320321

322+
export const useTableCellIds: typeof useTableCellIdsDecl = (
323+
tableId: Id,
324+
storeOrStoreId?: StoreOrStoreId,
325+
): Ids =>
326+
useListenable(
327+
TABLE + CELL_IDS,
328+
useStoreOrStoreId(storeOrStoreId),
329+
[],
330+
[tableId],
331+
);
332+
321333
export const useRowIds: typeof useRowIdsDecl = (
322334
tableId: Id,
323335
storeOrStoreId?: StoreOrStoreId,

0 commit comments

Comments
 (0)