Skip to content

Commit 3fb3875

Browse files
committed
[hooks] useSortedRowIds object arg
Fixes #214
1 parent 58866b4 commit 3fb3875

File tree

7 files changed

+568
-389
lines changed

7 files changed

+568
-389
lines changed

src/@types/ui-react/docs.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,55 @@
15231523
* @since v2.0.0
15241524
*/
15251525
/// useSortedRowIds
1526+
1527+
/**
1528+
* When called with an object as the first argument, the useSortedRowIds method
1529+
* destructures it to make it easier to skip optional parameters.
1530+
* @param args A SortedRowIdsArgs object containing the Id of the Table in the
1531+
* Store, and optional `cellId`, `descending`, `offset`, and `limit` parameters.
1532+
* @param storeOrStoreId The Store to be accessed: omit for the default context
1533+
* Store, provide an Id for a named context Store, or provide an explicit
1534+
* reference.
1535+
* @returns An array of the sorted Ids of every Row in the Table.
1536+
* @example
1537+
* This example creates a Store outside the application, which is used in the
1538+
* useSortedRowIds hook by reference. A change to the data in the Store
1539+
* re-renders the component.
1540+
*
1541+
* ```jsx
1542+
* import React from 'react';
1543+
* import {createRoot} from 'react-dom/client';
1544+
* import {createStore} from 'tinybase';
1545+
* import {useSortedRowIds} from 'tinybase/ui-react';
1546+
*
1547+
* const store = createStore().setTables({
1548+
* pets: {
1549+
* fido: {species: 'dog'},
1550+
* felix: {species: 'cat'},
1551+
* },
1552+
* });
1553+
* const App = () => (
1554+
* <span>
1555+
* {JSON.stringify(
1556+
* useSortedRowIds({tableId: 'pets', cellId: 'species'}, store),
1557+
* )}
1558+
* </span>
1559+
* );
1560+
*
1561+
* const app = document.createElement('div');
1562+
* createRoot(app).render(<App />); // !act
1563+
* console.log(app.innerHTML);
1564+
* // -> '<span>["felix","fido"]</span>'
1565+
*
1566+
* store.setRow('pets', 'cujo', {species: 'wolf'}); // !act
1567+
* console.log(app.innerHTML);
1568+
* // -> '<span>["felix","fido","cujo"]</span>'
1569+
* ```
1570+
* @category Store hooks
1571+
* @since v6.1.0
1572+
*/
1573+
/// useSortedRowIds.2
1574+
15261575
/**
15271576
* The useHasRow hook returns a boolean indicating whether a given Row exists in
15281577
* the Store, and registers a listener so that any changes to that result will

src/@types/ui-react/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import type {
6363
RowCountListener,
6464
RowIdsListener,
6565
RowListener,
66+
SortedRowIdsArgs,
6667
SortedRowIdsListener,
6768
Store,
6869
Table,
@@ -190,6 +191,12 @@ export function useSortedRowIds(
190191
storeOrStoreId?: StoreOrStoreId,
191192
): Ids;
192193

194+
/// useSortedRowIds.2
195+
export function useSortedRowIds(
196+
args: SortedRowIdsArgs,
197+
storeOrStoreId?: StoreOrStoreId,
198+
): Ids;
199+
193200
/// useHasRow
194201
export function useHasRow(
195202
tableId: Id,

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import type {
114114
RowCountListener,
115115
RowIdsListener,
116116
RowListener,
117+
SortedRowIdsArgs,
117118
SortedRowIdsListener,
118119
Store,
119120
Table,
@@ -236,18 +237,26 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
236237
storeOrStoreId?: StoreOrStoreId<Schemas>,
237238
) => Ids;
238239

239-
/// useSortedRowIds
240-
useSortedRowIds: <
241-
TableId extends TableIdFromSchema<Schemas[0]>,
242-
CellId extends CellIdFromSchema<Schemas[0], TableId>,
243-
>(
244-
tableId: TableId,
245-
cellId?: CellId,
246-
descending?: boolean,
247-
offset?: number,
248-
limit?: number,
249-
storeOrStoreId?: StoreOrStoreId<Schemas>,
250-
) => Ids;
240+
useSortedRowIds: {
241+
/// useSortedRowIds
242+
<
243+
TableId extends TableIdFromSchema<Schemas[0]>,
244+
CellId extends CellIdFromSchema<Schemas[0], TableId>,
245+
>(
246+
tableId: TableId,
247+
cellId?: CellId,
248+
descending?: boolean,
249+
offset?: number,
250+
limit?: number,
251+
storeOrStoreId?: StoreOrStoreId<Schemas>,
252+
): Ids;
253+
254+
/// useSortedRowIds.2
255+
<TableId extends TableIdFromSchema<Schemas[0]>>(
256+
args: SortedRowIdsArgs<Schemas[0], TableId>,
257+
storeOrStoreId?: StoreOrStoreId<Schemas>,
258+
): Ids;
259+
};
251260

252261
/// useHasRow
253262
useHasRow: <TableId extends TableIdFromSchema<Schemas[0]>>(
@@ -627,14 +636,7 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
627636
descending: Descending,
628637
offset: Offset,
629638
limit: Limit,
630-
listener: SortedRowIdsListener<
631-
Schemas,
632-
TableId,
633-
CellIdOrUndefined,
634-
Descending,
635-
Offset,
636-
Limit
637-
>,
639+
listener: SortedRowIdsListener<Schemas, TableId, CellIdOrUndefined>,
638640
listenerDeps?: React.DependencyList,
639641
mutator?: boolean,
640642
storeOrStoreId?: StoreOrStoreId<Schemas>,

src/ui-react/hooks.ts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import type {
6161
RowCountListener,
6262
RowIdsListener,
6363
RowListener,
64+
SortedRowIdsArgs,
6465
SortedRowIdsListener,
6566
Store,
6667
Table,
@@ -242,7 +243,7 @@ import {
242243
arrayMap,
243244
} from '../common/array.ts';
244245
import {ListenerArgument} from '../common/listeners.ts';
245-
import {IdObj, objIsEqual} from '../common/obj.ts';
246+
import {IdObj, isObject, objIsEqual} from '../common/obj.ts';
246247
import {
247248
getUndefined,
248249
ifNotUndefined,
@@ -480,6 +481,23 @@ const useCheckpointAction = (
480481
);
481482
};
482483

484+
const useSortedRowIdsImpl = (
485+
tableId: Id,
486+
cellId?: Id,
487+
descending?: boolean,
488+
offset?: number,
489+
limit?: number | undefined,
490+
storeOrStoreId?: StoreOrStoreId,
491+
): Ids =>
492+
useListenable(
493+
SORTED_ROW_IDS,
494+
useStoreOrStoreById(storeOrStoreId),
495+
ReturnType.Array,
496+
[tableId, cellId, descending, offset, limit],
497+
);
498+
499+
// ---
500+
483501
export const useCreateStore: typeof useCreateStoreDecl = (
484502
create: () => Store,
485503
createDeps: DependencyList = EMPTY_ARRAY,
@@ -599,18 +617,31 @@ export const useRowIds: typeof useRowIdsDecl = (
599617
);
600618

601619
export const useSortedRowIds: typeof useSortedRowIdsDecl = (
602-
tableId: Id,
603-
cellId?: Id,
620+
tableIdOrArgs: Id | SortedRowIdsArgs,
621+
cellIdOrStoreOrStoreId?: Id | StoreOrStoreId,
604622
descending?: boolean,
605-
offset = 0,
606-
limit?: number,
623+
offset?: number,
624+
limit?: number | undefined,
607625
storeOrStoreId?: StoreOrStoreId,
608626
): Ids =>
609-
useListenable(
610-
SORTED_ROW_IDS,
611-
useStoreOrStoreById(storeOrStoreId),
612-
ReturnType.Array,
613-
[tableId, cellId, descending, offset, limit],
627+
(useSortedRowIdsImpl as any)(
628+
...(isObject(tableIdOrArgs)
629+
? [
630+
tableIdOrArgs.tableId,
631+
tableIdOrArgs.cellId,
632+
tableIdOrArgs.descending ?? false,
633+
tableIdOrArgs.offset ?? 0,
634+
tableIdOrArgs.limit,
635+
cellIdOrStoreOrStoreId,
636+
]
637+
: [
638+
tableIdOrArgs,
639+
cellIdOrStoreOrStoreId,
640+
descending,
641+
offset,
642+
limit,
643+
storeOrStoreId,
644+
]),
614645
);
615646

616647
export const useHasRow: typeof useHasRowDecl = (

0 commit comments

Comments
 (0)