Skip to content

Commit 2953c49

Browse files
committed
[hooks] Docs & types for set callback upgrades
1 parent 337efe9 commit 2953c49

File tree

4 files changed

+74
-36
lines changed

4 files changed

+74
-36
lines changed

src/types/docs/ui-react.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@
134134
* @category Checkpoints
135135
*/
136136
/// UndoOrRedoInformation
137+
/**
138+
* The GetId type describes a function which, when passed a parameter, will
139+
* return an Id.
140+
*
141+
* This type is used in hooks that create callbacks - like the
142+
* useSetTableCallback hook or useSetRowCallback hook - so that the Id arguments
143+
* of the object to set can also be dependent on the event or parameter
144+
* provided (as well as the object itself being set).
145+
*/
146+
/// GetId
137147
/**
138148
* The useCreateStore hook is used to create a Store within a React application
139149
* with convenient memoization.
@@ -2124,13 +2134,15 @@
21242134
* The Store to which the callback will make the mutation (indicated by the
21252135
* hook's `storeOrStoreId` parameter) is always automatically used as a hook
21262136
* dependency for the callback.
2127-
* @param tableId The Id of the Table in the Store to set.
2137+
* @param tableId The Id of the Table in the Store to set, or a GetId function
2138+
* that will return it.
21282139
* @param getTable A function which returns the Table object that will be used
21292140
* to update the Store, based on the parameter the callback will receive (and
21302141
* which is most likely a DOM event).
21312142
* @param getTableDeps An optional array of dependencies for the `getTable`
21322143
* function, which, if any change, result in the regeneration of the callback.
2133-
* This parameter defaults to an empty array.
2144+
* This parameter defaults to an empty array. Also use this to indicate the
2145+
* dependencies of a GetId functions if used as the tableId argument.
21342146
* @param storeOrStoreId The Store to be updated: omit for the default context
21352147
* Store, provide an Id for a named context Store, or provide an explicit
21362148
* reference.
@@ -2201,14 +2213,18 @@
22012213
* The Store to which the callback will make the mutation (indicated by the
22022214
* hook's `storeOrStoreId` parameter) is always automatically used as a hook
22032215
* dependency for the callback.
2204-
* @param tableId The Id of the Table in the Store.
2205-
* @param rowId The Id of the Row in the Table to set.
2216+
* @param tableId The Id of the Table in the Store, or a GetId function that
2217+
* will return it.
2218+
* @param rowId The Id of the Row in the Table to set, or a GetId function that
2219+
* will return it.
22062220
* @param getRow A function which returns the Row object that will be used to
22072221
* update the Store, based on the parameter the callback will receive (and which
22082222
* is most likely a DOM event).
22092223
* @param getRowDeps An optional array of dependencies for the `getRow`
22102224
* function, which, if any change, result in the regeneration of the callback.
2211-
* This parameter defaults to an empty array.
2225+
* This parameter defaults to an empty array. Also use this to indicate the
2226+
* dependencies of any GetId functions if used as the tableId or rowId
2227+
* arguments.
22122228
* @param storeOrStoreId The Store to be updated: omit for the default context
22132229
* Store, provide an Id for a named context Store, or provide an explicit
22142230
* reference.
@@ -2287,13 +2303,15 @@
22872303
* specify `reuseRowIds` to be `false`, then the Id will be a monotonically
22882304
* increasing string representation of an increasing integer, regardless of any
22892305
* you may have previously deleted.
2290-
* @param tableId The Id of the Table in the Store.
2306+
* @param tableId The Id of the Table in the Store, or a GetId function
2307+
* that will return it.
22912308
* @param getRow A function which returns the Row object that will be used to
22922309
* update the Store, based on the parameter the callback will receive (and which
22932310
* is most likely a DOM event).
22942311
* @param getRowDeps An optional array of dependencies for the `getRow`
22952312
* function, which, if any change, result in the regeneration of the callback.
2296-
* This parameter defaults to an empty array.
2313+
* This parameter defaults to an empty array. Also use this to indicate the
2314+
* dependencies of a GetId functions if used as the tableId argument.
22972315
* @param storeOrStoreId The Store to be updated: omit for the default context
22982316
* Store, provide an Id for a named context Store, or provide an explicit
22992317
* reference.
@@ -2367,14 +2385,18 @@
23672385
* The Store to which the callback will make the mutation (indicated by the
23682386
* hook's `storeOrStoreId` parameter) is always automatically used as a hook
23692387
* dependency for the callback.
2370-
* @param tableId The Id of the Table in the Store.
2371-
* @param rowId The Id of the Row in the Table to set.
2388+
* @param tableId The Id of the Table in the Store, or a GetId function that
2389+
* will return it.
2390+
* @param rowId The Id of the Row in the Table to set, or a GetId function that
2391+
* will return it.
23722392
* @param getPartialRow A function which returns the partial Row object that
23732393
* will be used to update the Store, based on the parameter the callback will
23742394
* receive (and which is most likely a DOM event).
23752395
* @param getPartialRowDeps An optional array of dependencies for the `getRow`
23762396
* function, which, if any change, result in the regeneration of the callback.
2377-
* This parameter defaults to an empty array.
2397+
* This parameter defaults to an empty array. Also use this to indicate the
2398+
* dependencies of any GetId functions if used as the tableId, rowId, or cellId
2399+
* arguments.
23782400
* @param storeOrStoreId The Store to be updated: omit for the default context
23792401
* Store, provide an Id for a named context Store, or provide an explicit
23802402
* reference.
@@ -2447,15 +2469,20 @@
24472469
* The Store to which the callback will make the mutation (indicated by the
24482470
* hook's `storeOrStoreId` parameter) is always automatically used as a hook
24492471
* dependency for the callback.
2450-
* @param tableId The Id of the Table in the Store.
2451-
* @param rowId The Id of the Row in the Table.
2452-
* @param cellId The Id of the Cell in the Row to set.
2472+
* @param tableId The Id of the Table in the Store, or a GetId function that
2473+
* will return it.
2474+
* @param rowId The Id of the Row in the Table, or a GetId function that will
2475+
* return it.
2476+
* @param cellId The Id of the Cell in the Row to set, or a GetId function that
2477+
* will return it.
24532478
* @param getCell A function which returns the Cell value that will be used to
24542479
* update the Store, or a MapCell function to update it, based on the parameter
24552480
* the callback will receive (and which is most likely a DOM event).
24562481
* @param getCellDeps An optional array of dependencies for the `getCell`
24572482
* function, which, if any change, result in the regeneration of the callback.
2458-
* This parameter defaults to an empty array.
2483+
* This parameter defaults to an empty array. Also use this to indicate the
2484+
* dependencies of any GetId functions if used as the tableId, rowId, or cellId
2485+
* arguments.
24592486
* @param storeOrStoreId The Store to be updated: omit for the default context
24602487
* Store, provide an Id for a named context Store, or provide an explicit
24612488
* reference.
@@ -2719,13 +2746,15 @@
27192746
* The Store to which the callback will make the mutation (indicated by the
27202747
* hook's `storeOrStoreId` parameter) is always automatically used as a hook
27212748
* dependency for the callback.
2722-
* @param valueId The Id of the Value in the Store to set.
2749+
* @param valueId The Id of the Value in the Store to set, or a GetId function
2750+
* that will return it.
27232751
* @param getValue A function which returns the Value object that will be used
27242752
* to update the Store, based on the parameter the callback will receive (and
27252753
* which is most likely a DOM event).
27262754
* @param getValueDeps An optional array of dependencies for the `getValue`
27272755
* function, which, if any change, result in the regeneration of the callback.
2728-
* This parameter defaults to an empty array.
2756+
* This parameter defaults to an empty array. Also use this to indicate the
2757+
* dependencies of a GetId function if used as the valueId argument.
27292758
* @param storeOrStoreId The Store to be updated: omit for the default context
27302759
* Store, provide an Id for a named context Store, or provide an explicit
27312760
* reference.

src/types/ui-react.d.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ export type CheckpointsOrCheckpointsId = Checkpoints | Id;
8484
/// UndoOrRedoInformation
8585
export type UndoOrRedoInformation = [boolean, Callback, Id | undefined, string];
8686

87+
/// GetId
88+
export type GetId<Parameter> = (parameter: Parameter, store: Store) => Id;
89+
8790
/// useCreateStore
8891
export function useCreateStore(
8992
create: () => Store,
@@ -223,7 +226,7 @@ export function useSetTablesCallback<Parameter>(
223226

224227
/// useSetTableCallback
225228
export function useSetTableCallback<Parameter>(
226-
tableId: Id,
229+
tableId: Id | GetId<Parameter>,
227230
getTable: (parameter: Parameter, store: Store) => Table,
228231
getTableDeps?: React.DependencyList,
229232
storeOrStoreId?: StoreOrStoreId,
@@ -233,8 +236,8 @@ export function useSetTableCallback<Parameter>(
233236

234237
/// useSetRowCallback
235238
export function useSetRowCallback<Parameter>(
236-
tableId: Id,
237-
rowId: Id,
239+
tableId: Id | GetId<Parameter>,
240+
rowId: Id | GetId<Parameter>,
238241
getRow: (parameter: Parameter, store: Store) => Row,
239242
getRowDeps?: React.DependencyList,
240243
storeOrStoreId?: StoreOrStoreId,
@@ -244,7 +247,7 @@ export function useSetRowCallback<Parameter>(
244247

245248
/// useAddRowCallback
246249
export function useAddRowCallback<Parameter>(
247-
tableId: Id,
250+
tableId: Id | GetId<Parameter>,
248251
getRow: (parameter: Parameter, store: Store) => Row,
249252
getRowDeps?: React.DependencyList,
250253
storeOrStoreId?: StoreOrStoreId,
@@ -255,8 +258,8 @@ export function useAddRowCallback<Parameter>(
255258

256259
/// useSetPartialRowCallback
257260
export function useSetPartialRowCallback<Parameter>(
258-
tableId: Id,
259-
rowId: Id,
261+
tableId: Id | GetId<Parameter>,
262+
rowId: Id | GetId<Parameter>,
260263
getPartialRow: (parameter: Parameter, store: Store) => Row,
261264
getPartialRowDeps?: React.DependencyList,
262265
storeOrStoreId?: StoreOrStoreId,
@@ -266,9 +269,9 @@ export function useSetPartialRowCallback<Parameter>(
266269

267270
/// useSetCellCallback
268271
export function useSetCellCallback<Parameter>(
269-
tableId: Id,
270-
rowId: Id,
271-
cellId: Id,
272+
tableId: Id | GetId<Parameter>,
273+
rowId: Id | GetId<Parameter>,
274+
cellId: Id | GetId<Parameter>,
272275
getCell: (parameter: Parameter, store: Store) => Cell | MapCell,
273276
getCellDeps?: React.DependencyList,
274277
storeOrStoreId?: StoreOrStoreId,
@@ -296,7 +299,7 @@ export function useSetPartialValuesCallback<Parameter>(
296299

297300
/// useSetValueCallback
298301
export function useSetValueCallback<Parameter>(
299-
valueId: Id,
302+
valueId: Id | GetId<Parameter>,
300303
getValue: (parameter: Parameter, store: Store) => Value | MapValue,
301304
getValueDeps?: React.DependencyList,
302305
storeOrStoreId?: StoreOrStoreId,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ type CheckpointsOrCheckpointsId<Schemas extends OptionalSchemas> =
3232

3333
type UndoOrRedoInformation = [boolean, Callback, Id | undefined, string];
3434

35+
type GetId<Schemas extends OptionalSchemas, Parameter, Id> = (
36+
parameter: Parameter,
37+
store: Store<Schemas>,
38+
) => Id;
39+
3540
type ExtraProps = {[propName: string]: any};
3641

3742
type TablesProps<Schemas extends OptionalSchemas> = {

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
CurrentCheckpointProps,
1818
ExtraProps,
1919
ForwardCheckpointsProps,
20+
GetId,
2021
IndexProps,
2122
IndexesOrIndexesId,
2223
LinkedRowsProps,
@@ -294,7 +295,7 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
294295
TableId extends TableIdFromSchema<Schemas[0]>,
295296
SetTable = Table<Schemas[0], TableId, true>,
296297
>(
297-
tableId: TableId,
298+
tableId: TableId | GetId<Schemas, Parameter, TableId>,
298299
getTable: (
299300
parameter: Parameter,
300301
store: Store<Schemas>,
@@ -311,8 +312,8 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
311312
TableId extends TableIdFromSchema<Schemas[0]>,
312313
SetRow = Row<Schemas[0], TableId, true>,
313314
>(
314-
tableId: TableId,
315-
rowId: Id,
315+
tableId: TableId | GetId<Schemas, Parameter, TableId>,
316+
rowId: Id | GetId<Schemas, Parameter, Id>,
316317
getRow: (parameter: Parameter, store: Store<Schemas>) => NoInfer<SetRow>,
317318
getRowDeps?: React.DependencyList,
318319
storeOrStoreId?: StoreOrStoreId<Schemas>,
@@ -326,7 +327,7 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
326327
TableId extends TableIdFromSchema<Schemas[0]>,
327328
AddRow = Row<Schemas[0], TableId, true>,
328329
>(
329-
tableId: TableId,
330+
tableId: TableId | GetId<Schemas, Parameter, TableId>,
330331
getRow: (parameter: Parameter, store: Store<Schemas>) => NoInfer<AddRow>,
331332
getRowDeps?: React.DependencyList,
332333
storeOrStoreId?: StoreOrStoreId<Schemas>,
@@ -341,8 +342,8 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
341342
TableId extends TableIdFromSchema<Schemas[0]>,
342343
SetPartialRow = Row<Schemas[0], TableId, true>,
343344
>(
344-
tableId: TableId,
345-
rowId: Id,
345+
tableId: TableId | GetId<Schemas, Parameter, TableId>,
346+
rowId: Id | GetId<Schemas, Parameter, Id>,
346347
getPartialRow: (
347348
parameter: Parameter,
348349
store: Store<Schemas>,
@@ -362,9 +363,9 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
362363
| Cell<Schemas[0], TableId, CellId>
363364
| MapCell<Schemas[0], TableId, CellId>,
364365
>(
365-
tableId: TableId,
366-
rowId: Id,
367-
cellId: CellId,
366+
tableId: TableId | GetId<Schemas, Parameter, TableId>,
367+
rowId: Id | GetId<Schemas, Parameter, Id>,
368+
cellId: CellId | GetId<Schemas, Parameter, CellId>,
368369
getCell: (
369370
parameter: Parameter,
370371
store: Store<Schemas>,
@@ -408,7 +409,7 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
408409
ValueId extends ValueIdFromSchema<Schemas[1]>,
409410
SetOrMapValue = Value<Schemas[1], ValueId> | MapValue<Schemas[1], ValueId>,
410411
>(
411-
valueId: ValueId,
412+
valueId: ValueId | GetId<Schemas, Parameter, ValueId>,
412413
getValue: (
413414
parameter: Parameter,
414415
store: Store<Schemas>,

0 commit comments

Comments
 (0)