Skip to content

Commit a0cc2d1

Browse files
committed
[store] useTableCellIdsListener
1 parent da6420f commit a0cc2d1

File tree

7 files changed

+439
-265
lines changed

7 files changed

+439
-265
lines changed

src/types/docs/ui-react.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,6 +2563,68 @@
25632563
* @category Store hooks
25642564
*/
25652565
/// useTableListener
2566+
/**
2567+
* The useTableCellIdsListener hook registers a listener function with a Store
2568+
* that will be called whenever the Cell Ids that appear anywhere in a Table
2569+
* change.
2570+
*
2571+
* This hook is useful for situations where a component needs to register its
2572+
* own specific listener to do more than simply tracking the value (which is
2573+
* more easily done with the useTableCellIds hook).
2574+
*
2575+
* You can either listen to a single Table (by specifying its Id as the method's
2576+
* first parameter) or changes to any Table (by providing `null`).
2577+
*
2578+
* Unlike the addTableCellIdsListener method, which returns a listener Id and
2579+
* requires you to remove it manually, the useTableCellIdsListener hook manages
2580+
* this lifecycle for you: when the listener changes (per its `listenerDeps`
2581+
* dependencies) or the component unmounts, the listener on the underlying Store
2582+
* will be deleted.
2583+
*
2584+
* @param tableId The Id of the Table to listen to, or `null` as a wildcard.
2585+
* @param listener The function that will be called whenever the Cell Ids that
2586+
* appear anywhere in a Table change.
2587+
* @param listenerDeps An optional array of dependencies for the `listener`
2588+
* function, which, if any change, result in the re-registration of the
2589+
* listener. This parameter defaults to an empty array.
2590+
* @param mutator An optional boolean that indicates that the listener mutates
2591+
* Store data.
2592+
* @param storeOrStoreId The Store to register the listener with: omit for the
2593+
* default context Store, provide an Id for a named context Store, or provide an
2594+
* explicit reference.
2595+
* @example
2596+
* This example uses the useTableCellIdsListener hook to create a listener that
2597+
* is scoped to a single component. When the component is unmounted, the
2598+
* listener is removed from the Store.
2599+
*
2600+
* ```jsx
2601+
* const App = ({store}) => (
2602+
* <Provider store={store}>
2603+
* <Pane />
2604+
* </Provider>
2605+
* );
2606+
* const Pane = () => {
2607+
* useTableCellIdsListener('pets', () => console.log('Cell Ids changed'));
2608+
* return <span>App</span>;
2609+
* };
2610+
*
2611+
* const store = createStore().setTables({pets: {fido: {color: 'brown'}}});
2612+
* const app = document.createElement('div');
2613+
* const root = ReactDOMClient.createRoot(app);
2614+
* root.render(<App store={store} />); // !act
2615+
* console.log(store.getListenerStats().tableCellIds);
2616+
* // -> 1
2617+
*
2618+
* store.setRow('pets', 'felix', {species: 'cat'}); // !act
2619+
* // -> 'Cell Ids changed'
2620+
*
2621+
* root.unmount(); // !act
2622+
* console.log(store.getListenerStats().rowIds);
2623+
* // -> 0
2624+
* ```
2625+
* @category Store hooks
2626+
*/
2627+
/// useTableCellIdsListener
25662628
/**
25672629
* The useRowIdsListener hook registers a listener function with a Store that
25682630
* will be called whenever the Row Ids in a Table change.

src/types/ui-react.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
SortedRowIdsListener,
1515
Store,
1616
Table,
17+
TableCellIdsListener,
1718
TableIdsListener,
1819
TableListener,
1920
Tables,
@@ -309,6 +310,15 @@ export function useTableListener(
309310
storeOrStoreId?: StoreOrStoreId,
310311
): void;
311312

313+
/// useTableCellIdsListener
314+
export function useTableCellIdsListener(
315+
tableId: IdOrNull,
316+
listener: TableCellIdsListener,
317+
listenerDeps?: React.DependencyList,
318+
mutator?: boolean,
319+
storeOrStoreId?: StoreOrStoreId,
320+
): void;
321+
312322
/// useRowIdsListener
313323
export function useRowIdsListener(
314324
tableId: IdOrNull,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
SortedRowIdsListener,
5757
Store,
5858
Table,
59+
TableCellIdsListener,
5960
TableIdsListener,
6061
TableListener,
6162
Tables,
@@ -426,6 +427,17 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
426427
storeOrStoreId?: StoreOrStoreId<Schemas>,
427428
) => void;
428429

430+
/// useTableCellIdsListener
431+
useTableCellIdsListener: <
432+
TableIdOrNull extends TableIdFromSchema<Schemas[0]> | null,
433+
>(
434+
tableId: TableIdOrNull,
435+
listener: TableCellIdsListener<Schemas, TableIdOrNull>,
436+
listenerDeps?: React.DependencyList,
437+
mutator?: boolean,
438+
storeOrStoreId?: StoreOrStoreId<Schemas>,
439+
) => void;
440+
429441
/// useRowIdsListener
430442
useRowIdsListener: <
431443
TableIdOrNull extends TableIdFromSchema<Schemas[0]> | null,

src/ui-react/hooks.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
SortedRowIdsListener,
3535
Store,
3636
Table,
37+
TableCellIdsListener,
3738
TableIdsListener,
3839
TableListener,
3940
Tables,
@@ -124,6 +125,7 @@ import {
124125
useSortedRowIds as useSortedRowIdsDecl,
125126
useSortedRowIdsListener as useSortedRowIdsListenerDecl,
126127
useTableCellIds as useTableCellIdsDecl,
128+
useTableCellIdsListener as useTableCellIdsListenerDecl,
127129
useTable as useTableDecl,
128130
useTableIds as useTableIdsDecl,
129131
useTableIdsListener as useTableIdsListenerDecl,
@@ -674,6 +676,22 @@ export const useTableListener: typeof useTableListenerDecl = (
674676
mutator,
675677
);
676678

679+
export const useTableCellIdsListener: typeof useTableCellIdsListenerDecl = (
680+
tableId: IdOrNull,
681+
listener: TableCellIdsListener,
682+
listenerDeps?: React.DependencyList,
683+
mutator?: boolean,
684+
storeOrStoreId?: StoreOrStoreId,
685+
): void =>
686+
useListener(
687+
TABLE + CELL_IDS,
688+
useStoreOrStoreId(storeOrStoreId),
689+
listener,
690+
listenerDeps,
691+
[tableId],
692+
mutator,
693+
);
694+
677695
export const useRowIdsListener: typeof useRowIdsListenerDecl = (
678696
tableId: IdOrNull,
679697
listener: RowIdsListener,

0 commit comments

Comments
 (0)