tinybase 7.1.0 → 7.2.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/@types/queries/index.d.ts +196 -22
- package/@types/queries/with-schemas/index.d.ts +210 -22
- package/@types/ui-react/index.d.ts +285 -0
- package/@types/ui-react/with-schemas/index.d.ts +318 -0
- package/index.js +29 -5
- package/min/index.js +1 -1
- package/min/index.js.gz +0 -0
- package/min/omni/index.js +1 -1
- package/min/omni/index.js.gz +0 -0
- package/min/omni/with-schemas/index.js +1 -1
- package/min/omni/with-schemas/index.js.gz +0 -0
- package/min/queries/index.js +1 -1
- package/min/queries/index.js.gz +0 -0
- package/min/queries/with-schemas/index.js +1 -1
- package/min/queries/with-schemas/index.js.gz +0 -0
- package/min/ui-react/index.js +1 -1
- package/min/ui-react/index.js.gz +0 -0
- package/min/ui-react/with-schemas/index.js +1 -1
- package/min/ui-react/with-schemas/index.js.gz +0 -0
- package/min/ui-react-dom/index.js +1 -1
- package/min/ui-react-dom/index.js.gz +0 -0
- package/min/ui-react-dom/with-schemas/index.js +1 -1
- package/min/ui-react-dom/with-schemas/index.js.gz +0 -0
- package/min/ui-react-inspector/index.js +1 -1
- package/min/ui-react-inspector/index.js.gz +0 -0
- package/min/ui-react-inspector/with-schemas/index.js +1 -1
- package/min/ui-react-inspector/with-schemas/index.js.gz +0 -0
- package/min/with-schemas/index.js +1 -1
- package/min/with-schemas/index.js.gz +0 -0
- package/omni/index.js +131 -24
- package/omni/with-schemas/index.js +131 -24
- package/package.json +1 -1
- package/queries/index.js +41 -5
- package/queries/with-schemas/index.js +41 -5
- package/readme.md +2 -2
- package/ui-react/index.js +102 -19
- package/ui-react/with-schemas/index.js +102 -19
- package/ui-react-dom/index.js +39 -13
- package/ui-react-dom/with-schemas/index.js +39 -13
- package/ui-react-inspector/index.js +41 -15
- package/ui-react-inspector/with-schemas/index.js +41 -15
- package/with-schemas/index.js +29 -5
|
@@ -52,6 +52,8 @@ import type {
|
|
|
52
52
|
StatusListener,
|
|
53
53
|
} from '../persisters/index.d.ts';
|
|
54
54
|
import type {
|
|
55
|
+
ParamValue,
|
|
56
|
+
ParamValues,
|
|
55
57
|
Queries,
|
|
56
58
|
ResultCellIdsListener,
|
|
57
59
|
ResultCellListener,
|
|
@@ -10728,6 +10730,289 @@ export function useResultCellListener(
|
|
|
10728
10730
|
queriesOrQueriesId?: QueriesOrQueriesId,
|
|
10729
10731
|
): void;
|
|
10730
10732
|
|
|
10733
|
+
/**
|
|
10734
|
+
* The useSetQueryParamValueCallback hook returns a parameterized callback that
|
|
10735
|
+
* can be used to set a single parameter value for a query.
|
|
10736
|
+
*
|
|
10737
|
+
* This hook is useful, for example, when creating an event handler that will
|
|
10738
|
+
* update query parameters based on user interaction. In this case, the
|
|
10739
|
+
* parameter will likely be the event, so that you can use data from it to
|
|
10740
|
+
* update the query parameter.
|
|
10741
|
+
*
|
|
10742
|
+
* The third parameter is a function which will produce the parameter value that
|
|
10743
|
+
* will then be used to update the query in the callback.
|
|
10744
|
+
*
|
|
10745
|
+
* If that function has any other dependencies, the changing of which should
|
|
10746
|
+
* also cause the callback to be recreated, you can provide them in an array in
|
|
10747
|
+
* the optional fourth parameter, just as you would for any React hook with
|
|
10748
|
+
* dependencies.
|
|
10749
|
+
*
|
|
10750
|
+
* For convenience, you can optionally provide a `then` function (with its own
|
|
10751
|
+
* set of dependencies) which will be called just after the query parameter has
|
|
10752
|
+
* been updated.
|
|
10753
|
+
*
|
|
10754
|
+
* The Queries object to which the callback will make the mutation (indicated by
|
|
10755
|
+
* the hook's `queriesOrQueriesId` parameter) is always automatically used as a
|
|
10756
|
+
* hook dependency for the callback.
|
|
10757
|
+
* @param queryId The Id of the query to update, or a GetId function that will
|
|
10758
|
+
* return it.
|
|
10759
|
+
* @param paramId The Id of the parameter to update, or a GetId function that
|
|
10760
|
+
* will return it.
|
|
10761
|
+
* @param getParamValue A function which returns the parameter value that will
|
|
10762
|
+
* be used to update the query, based on the parameter the callback will receive
|
|
10763
|
+
* (and which is most likely a DOM event).
|
|
10764
|
+
* @param getParamValueDeps An optional array of dependencies for the
|
|
10765
|
+
* `getParamValue` function, which, if any change, result in the regeneration of
|
|
10766
|
+
* the callback. This parameter defaults to an empty array. Also use this to
|
|
10767
|
+
* indicate the dependencies of any GetId functions if used as the queryId or
|
|
10768
|
+
* paramId arguments.
|
|
10769
|
+
* @param queriesOrQueriesId The Queries object to be updated: omit for the
|
|
10770
|
+
* default context Queries object, provide an Id for a named context Queries
|
|
10771
|
+
* object, or provide an explicit reference.
|
|
10772
|
+
* @param then A function which is called after the mutation, with a reference
|
|
10773
|
+
* to the Queries object and the parameter value used in the update.
|
|
10774
|
+
* @param thenDeps An optional array of dependencies for the `then` function,
|
|
10775
|
+
* which, if any change, result in the regeneration of the callback. This
|
|
10776
|
+
* parameter defaults to an empty array.
|
|
10777
|
+
* @returns A parameterized callback for subsequent use.
|
|
10778
|
+
* @example
|
|
10779
|
+
* This example uses the useSetQueryParamValueCallback hook to create an event
|
|
10780
|
+
* handler which updates a query parameter when an input element changes.
|
|
10781
|
+
*
|
|
10782
|
+
* ```jsx
|
|
10783
|
+
* import React from 'react';
|
|
10784
|
+
* import {createRoot} from 'react-dom/client';
|
|
10785
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
10786
|
+
* import {
|
|
10787
|
+
* useResultTable,
|
|
10788
|
+
* useSetQueryParamValueCallback,
|
|
10789
|
+
* } from 'tinybase/ui-react';
|
|
10790
|
+
*
|
|
10791
|
+
* const store = createStore().setTable('pets', {
|
|
10792
|
+
* fido: {species: 'dog', color: 'brown'},
|
|
10793
|
+
* felix: {species: 'cat', color: 'black'},
|
|
10794
|
+
* cujo: {species: 'dog', color: 'black'},
|
|
10795
|
+
* });
|
|
10796
|
+
* const queries = createQueries(store);
|
|
10797
|
+
* queries.setQueryDefinition(
|
|
10798
|
+
* 'dogOrCat',
|
|
10799
|
+
* 'pets',
|
|
10800
|
+
* ({select, where, param}) => {
|
|
10801
|
+
* select('species');
|
|
10802
|
+
* where('species', param('species'));
|
|
10803
|
+
* },
|
|
10804
|
+
* {species: 'dog'},
|
|
10805
|
+
* );
|
|
10806
|
+
*
|
|
10807
|
+
* const App = () => {
|
|
10808
|
+
* const handleInput = useSetQueryParamValueCallback(
|
|
10809
|
+
* 'dogOrCat',
|
|
10810
|
+
* 'species',
|
|
10811
|
+
* (e) => e.target.value,
|
|
10812
|
+
* [],
|
|
10813
|
+
* queries,
|
|
10814
|
+
* (_queries, paramValue) => console.log(`Updated: ${paramValue}`),
|
|
10815
|
+
* );
|
|
10816
|
+
* return (
|
|
10817
|
+
* <div>
|
|
10818
|
+
* <input id="input" onInput={handleInput} />
|
|
10819
|
+
* {JSON.stringify(useResultTable('dogOrCat', queries))}
|
|
10820
|
+
* </div>
|
|
10821
|
+
* );
|
|
10822
|
+
* };
|
|
10823
|
+
*
|
|
10824
|
+
* const app = document.createElement('div');
|
|
10825
|
+
* createRoot(app).render(<App />); // !act
|
|
10826
|
+
* const input = app.querySelector('input');
|
|
10827
|
+
* console.log(app.innerHTML);
|
|
10828
|
+
* // ->
|
|
10829
|
+
* `
|
|
10830
|
+
* <div>
|
|
10831
|
+
* <input id="input">
|
|
10832
|
+
* {"fido":{"species":"dog"},"cujo":{"species":"dog"}}
|
|
10833
|
+
* </div>
|
|
10834
|
+
* `;
|
|
10835
|
+
*
|
|
10836
|
+
* // User types 'cat' in the input and event fires:
|
|
10837
|
+
* input.value = 'cat';
|
|
10838
|
+
* // -> input Event('input', {bubbles: true})
|
|
10839
|
+
* // -> 'Updated: cat'
|
|
10840
|
+
*
|
|
10841
|
+
* console.log(app.innerHTML);
|
|
10842
|
+
* // ->
|
|
10843
|
+
* `
|
|
10844
|
+
* <div>
|
|
10845
|
+
* <input id="input">
|
|
10846
|
+
* {"felix":{"species":"cat"}}
|
|
10847
|
+
* </div>
|
|
10848
|
+
* `;
|
|
10849
|
+
* ```
|
|
10850
|
+
* @category Queries hooks
|
|
10851
|
+
* @since v7.2.0
|
|
10852
|
+
*/
|
|
10853
|
+
export function useSetQueryParamValueCallback<Parameter>(
|
|
10854
|
+
queryId: Id | GetId<Parameter>,
|
|
10855
|
+
paramId: Id | GetId<Parameter>,
|
|
10856
|
+
getParamValue: (parameter: Parameter, queries: Queries) => ParamValue,
|
|
10857
|
+
getParamValueDeps?: React.DependencyList,
|
|
10858
|
+
queriesOrQueriesId?: QueriesOrQueriesId,
|
|
10859
|
+
then?: (queries: Queries, paramValue: ParamValue) => void,
|
|
10860
|
+
thenDeps?: React.DependencyList,
|
|
10861
|
+
): ParameterizedCallback<Parameter>;
|
|
10862
|
+
|
|
10863
|
+
/**
|
|
10864
|
+
* The useSetQueryParamValuesCallback hook returns a parameterized callback that
|
|
10865
|
+
* can be used to set multiple parameter values for a query at once.
|
|
10866
|
+
*
|
|
10867
|
+
* This hook is useful, for example, when creating an event handler that will
|
|
10868
|
+
* update multiple query parameters based on user interaction. In this case, the
|
|
10869
|
+
* parameter will likely be the event, so that you can use data from it to
|
|
10870
|
+
* update the query parameters.
|
|
10871
|
+
*
|
|
10872
|
+
* The second parameter is a function which will produce the parameter values
|
|
10873
|
+
* object that will then be used to update the query in the callback.
|
|
10874
|
+
*
|
|
10875
|
+
* If that function has any other dependencies, the changing of which should
|
|
10876
|
+
* also cause the callback to be recreated, you can provide them in an array in
|
|
10877
|
+
* the optional third parameter, just as you would for any React hook with
|
|
10878
|
+
* dependencies.
|
|
10879
|
+
*
|
|
10880
|
+
* For convenience, you can optionally provide a `then` function (with its own
|
|
10881
|
+
* set of dependencies) which will be called just after the query parameters
|
|
10882
|
+
* have been updated.
|
|
10883
|
+
*
|
|
10884
|
+
* The Queries object to which the callback will make the mutation (indicated by
|
|
10885
|
+
* the hook's `queriesOrQueriesId` parameter) is always automatically used as a
|
|
10886
|
+
* hook dependency for the callback.
|
|
10887
|
+
* @param queryId The Id of the query to update, or a GetId function that will
|
|
10888
|
+
* return it.
|
|
10889
|
+
* @param getParamValues A function which returns the parameter values object
|
|
10890
|
+
* that will be used to update the query, based on the parameter the callback
|
|
10891
|
+
* will receive (and which is most likely a DOM event).
|
|
10892
|
+
* @param getParamValuesDeps An optional array of dependencies for the
|
|
10893
|
+
* `getParamValues` function, which, if any change, result in the regeneration
|
|
10894
|
+
* of the callback. This parameter defaults to an empty array. Also use this to
|
|
10895
|
+
* indicate the dependencies of any GetId functions if used as the queryId
|
|
10896
|
+
* argument.
|
|
10897
|
+
* @param queriesOrQueriesId The Queries object to be updated: omit for the
|
|
10898
|
+
* default context Queries object, provide an Id for a named context Queries
|
|
10899
|
+
* object, or provide an explicit reference.
|
|
10900
|
+
* @param then A function which is called after the mutation, with a reference
|
|
10901
|
+
* to the Queries object and the parameter values used in the update.
|
|
10902
|
+
* @param thenDeps An optional array of dependencies for the `then` function,
|
|
10903
|
+
* which, if any change, result in the regeneration of the callback. This
|
|
10904
|
+
* parameter defaults to an empty array.
|
|
10905
|
+
* @returns A parameterized callback for subsequent use.
|
|
10906
|
+
* @example
|
|
10907
|
+
* This example uses the useSetQueryParamValuesCallback hook to create an event
|
|
10908
|
+
* handler which updates multiple query parameters when a form is submitted.
|
|
10909
|
+
*
|
|
10910
|
+
* ```jsx
|
|
10911
|
+
* import React from 'react';
|
|
10912
|
+
* import {createRoot} from 'react-dom/client';
|
|
10913
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
10914
|
+
* import {
|
|
10915
|
+
* useResultTable,
|
|
10916
|
+
* useSetQueryParamValuesCallback,
|
|
10917
|
+
* } from 'tinybase/ui-react';
|
|
10918
|
+
*
|
|
10919
|
+
* const store = createStore().setTable('pets', {
|
|
10920
|
+
* fido: {species: 'dog', color: 'brown'},
|
|
10921
|
+
* felix: {species: 'cat', color: 'black'},
|
|
10922
|
+
* cujo: {species: 'dog', color: 'black'},
|
|
10923
|
+
* });
|
|
10924
|
+
* const queries = createQueries(store);
|
|
10925
|
+
* queries.setQueryDefinition(
|
|
10926
|
+
* 'speciesAndColor',
|
|
10927
|
+
* 'pets',
|
|
10928
|
+
* ({select, where, param}) => {
|
|
10929
|
+
* select('species');
|
|
10930
|
+
* select('color');
|
|
10931
|
+
* where('species', param('species'));
|
|
10932
|
+
* where('color', param('color'));
|
|
10933
|
+
* },
|
|
10934
|
+
* {species: 'dog', color: 'brown'},
|
|
10935
|
+
* );
|
|
10936
|
+
*
|
|
10937
|
+
* const App = () => {
|
|
10938
|
+
* const handleSubmit = useSetQueryParamValuesCallback(
|
|
10939
|
+
* 'speciesAndColor',
|
|
10940
|
+
* (e) => {
|
|
10941
|
+
* e.preventDefault();
|
|
10942
|
+
* return {
|
|
10943
|
+
* species: e.target.querySelector('#species').value,
|
|
10944
|
+
* color: e.target.querySelector('#color').value,
|
|
10945
|
+
* };
|
|
10946
|
+
* },
|
|
10947
|
+
* [],
|
|
10948
|
+
* queries,
|
|
10949
|
+
* (_, paramValues) =>
|
|
10950
|
+
* console.log(`Updated: ${JSON.stringify(paramValues)}`),
|
|
10951
|
+
* );
|
|
10952
|
+
* return (
|
|
10953
|
+
* <div>
|
|
10954
|
+
* <form onSubmit={handleSubmit}>
|
|
10955
|
+
* <input id="species" />
|
|
10956
|
+
* <input id="color" />
|
|
10957
|
+
* <button type="submit">Filter</button>
|
|
10958
|
+
* </form>
|
|
10959
|
+
* {JSON.stringify(useResultTable('speciesAndColor', queries))}
|
|
10960
|
+
* </div>
|
|
10961
|
+
* );
|
|
10962
|
+
* };
|
|
10963
|
+
*
|
|
10964
|
+
* const app = document.createElement('div');
|
|
10965
|
+
* createRoot(app).render(<App />); // !act
|
|
10966
|
+
* const form = app.querySelector('form');
|
|
10967
|
+
* console.log(app.innerHTML);
|
|
10968
|
+
* // ->
|
|
10969
|
+
* `
|
|
10970
|
+
* <div>
|
|
10971
|
+
* <form>
|
|
10972
|
+
* <input id="species">
|
|
10973
|
+
* <input id="color">
|
|
10974
|
+
* <button type="submit">Filter</button>
|
|
10975
|
+
* </form>
|
|
10976
|
+
* {"fido":{"species":"dog","color":"brown"}}
|
|
10977
|
+
* </div>
|
|
10978
|
+
* `;
|
|
10979
|
+
*
|
|
10980
|
+
* // User fills form with new values:
|
|
10981
|
+
* const species = form.querySelector('#species');
|
|
10982
|
+
* const color = form.querySelector('#color');
|
|
10983
|
+
*
|
|
10984
|
+
* species.value = 'cat'; // !act
|
|
10985
|
+
* color.value = 'black'; // !act
|
|
10986
|
+
*
|
|
10987
|
+
* // And submits the form:
|
|
10988
|
+
* // -> form SubmitEvent('submit', {bubbles: true})
|
|
10989
|
+
* // -> 'Updated: {"species":"cat","color":"black"}'
|
|
10990
|
+
*
|
|
10991
|
+
* console.log(app.innerHTML);
|
|
10992
|
+
* // ->
|
|
10993
|
+
* `
|
|
10994
|
+
* <div>
|
|
10995
|
+
* <form>
|
|
10996
|
+
* <input id="species">
|
|
10997
|
+
* <input id="color">
|
|
10998
|
+
* <button type="submit">Filter</button>
|
|
10999
|
+
* </form>
|
|
11000
|
+
* {"felix":{"species":"cat","color":"black"}}
|
|
11001
|
+
* </div>
|
|
11002
|
+
* `;
|
|
11003
|
+
* ```
|
|
11004
|
+
* @category Queries hooks
|
|
11005
|
+
* @since v7.2.0
|
|
11006
|
+
*/
|
|
11007
|
+
export function useSetQueryParamValuesCallback<Parameter>(
|
|
11008
|
+
queryId: Id | GetId<Parameter>,
|
|
11009
|
+
getParamValues: (parameter: Parameter, queries: Queries) => ParamValues,
|
|
11010
|
+
getParamValuesDeps?: React.DependencyList,
|
|
11011
|
+
queriesOrQueriesId?: QueriesOrQueriesId,
|
|
11012
|
+
then?: (queries: Queries, paramValues: ParamValues) => void,
|
|
11013
|
+
thenDeps?: React.DependencyList,
|
|
11014
|
+
): ParameterizedCallback<Parameter>;
|
|
11015
|
+
|
|
10731
11016
|
/**
|
|
10732
11017
|
* The useCreateCheckpoints hook is used to create a Checkpoints object within a
|
|
10733
11018
|
* React application with convenient memoization.
|
|
@@ -99,6 +99,8 @@ import type {
|
|
|
99
99
|
StatusListener,
|
|
100
100
|
} from '../../persisters/with-schemas/index.d.ts';
|
|
101
101
|
import type {
|
|
102
|
+
ParamValue,
|
|
103
|
+
ParamValues,
|
|
102
104
|
Queries,
|
|
103
105
|
ResultCell,
|
|
104
106
|
ResultCellIdsListener,
|
|
@@ -11774,6 +11776,322 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
|
|
|
11774
11776
|
queriesOrQueriesId?: QueriesOrQueriesId<Schemas>,
|
|
11775
11777
|
) => void;
|
|
11776
11778
|
|
|
11779
|
+
/**
|
|
11780
|
+
* The useSetQueryParamValueCallback hook returns a parameterized callback that
|
|
11781
|
+
* can be used to set a single parameter value for a query.
|
|
11782
|
+
*
|
|
11783
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
11784
|
+
*
|
|
11785
|
+
* ```ts override
|
|
11786
|
+
* useSetQueryParamValueCallback<Parameter>(
|
|
11787
|
+
* queryId: Id | GetId<Parameter>,
|
|
11788
|
+
* paramId: Id | GetId<Parameter>,
|
|
11789
|
+
* getParamValue: (parameter: Parameter, queries: Queries) => ParamValue,
|
|
11790
|
+
* getParamValueDeps?: React.DependencyList,
|
|
11791
|
+
* queriesOrQueriesId?: QueriesOrQueriesId,
|
|
11792
|
+
* then?: (queries: Queries, paramValue: ParamValue) => void,
|
|
11793
|
+
* thenDeps?: React.DependencyList,
|
|
11794
|
+
* ): ParameterizedCallback<Parameter>;
|
|
11795
|
+
* ```
|
|
11796
|
+
*
|
|
11797
|
+
* This hook is useful, for example, when creating an event handler that will
|
|
11798
|
+
* update query parameters based on user interaction. In this case, the
|
|
11799
|
+
* parameter will likely be the event, so that you can use data from it to
|
|
11800
|
+
* update the query parameter.
|
|
11801
|
+
*
|
|
11802
|
+
* The third parameter is a function which will produce the parameter value that
|
|
11803
|
+
* will then be used to update the query in the callback.
|
|
11804
|
+
*
|
|
11805
|
+
* If that function has any other dependencies, the changing of which should
|
|
11806
|
+
* also cause the callback to be recreated, you can provide them in an array in
|
|
11807
|
+
* the optional fourth parameter, just as you would for any React hook with
|
|
11808
|
+
* dependencies.
|
|
11809
|
+
*
|
|
11810
|
+
* For convenience, you can optionally provide a `then` function (with its own
|
|
11811
|
+
* set of dependencies) which will be called just after the query parameter has
|
|
11812
|
+
* been updated.
|
|
11813
|
+
*
|
|
11814
|
+
* The Queries object to which the callback will make the mutation (indicated by
|
|
11815
|
+
* the hook's `queriesOrQueriesId` parameter) is always automatically used as a
|
|
11816
|
+
* hook dependency for the callback.
|
|
11817
|
+
* @param queryId The Id of the query to update, or a GetId function that will
|
|
11818
|
+
* return it.
|
|
11819
|
+
* @param paramId The Id of the parameter to update, or a GetId function that
|
|
11820
|
+
* will return it.
|
|
11821
|
+
* @param getParamValue A function which returns the parameter value that will
|
|
11822
|
+
* be used to update the query, based on the parameter the callback will receive
|
|
11823
|
+
* (and which is most likely a DOM event).
|
|
11824
|
+
* @param getParamValueDeps An optional array of dependencies for the
|
|
11825
|
+
* `getParamValue` function, which, if any change, result in the regeneration of
|
|
11826
|
+
* the callback. This parameter defaults to an empty array. Also use this to
|
|
11827
|
+
* indicate the dependencies of any GetId functions if used as the queryId or
|
|
11828
|
+
* paramId arguments.
|
|
11829
|
+
* @param queriesOrQueriesId The Queries object to be updated: omit for the
|
|
11830
|
+
* default context Queries object, provide an Id for a named context Queries
|
|
11831
|
+
* object, or provide an explicit reference.
|
|
11832
|
+
* @param then A function which is called after the mutation, with a reference
|
|
11833
|
+
* to the Queries object and the parameter value used in the update.
|
|
11834
|
+
* @param thenDeps An optional array of dependencies for the `then` function,
|
|
11835
|
+
* which, if any change, result in the regeneration of the callback. This
|
|
11836
|
+
* parameter defaults to an empty array.
|
|
11837
|
+
* @returns A parameterized callback for subsequent use.
|
|
11838
|
+
* @example
|
|
11839
|
+
* This example uses the useSetQueryParamValueCallback hook to create an event
|
|
11840
|
+
* handler which updates a query parameter when an input element changes.
|
|
11841
|
+
*
|
|
11842
|
+
* ```jsx
|
|
11843
|
+
* import React from 'react';
|
|
11844
|
+
* import {createRoot} from 'react-dom/client';
|
|
11845
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
11846
|
+
* import {
|
|
11847
|
+
* useResultTable,
|
|
11848
|
+
* useSetQueryParamValueCallback,
|
|
11849
|
+
* } from 'tinybase/ui-react';
|
|
11850
|
+
*
|
|
11851
|
+
* const store = createStore().setTable('pets', {
|
|
11852
|
+
* fido: {species: 'dog', color: 'brown'},
|
|
11853
|
+
* felix: {species: 'cat', color: 'black'},
|
|
11854
|
+
* cujo: {species: 'dog', color: 'black'},
|
|
11855
|
+
* });
|
|
11856
|
+
* const queries = createQueries(store);
|
|
11857
|
+
* queries.setQueryDefinition(
|
|
11858
|
+
* 'dogOrCat',
|
|
11859
|
+
* 'pets',
|
|
11860
|
+
* ({select, where, param}) => {
|
|
11861
|
+
* select('species');
|
|
11862
|
+
* where('species', param('species'));
|
|
11863
|
+
* },
|
|
11864
|
+
* {species: 'dog'},
|
|
11865
|
+
* );
|
|
11866
|
+
*
|
|
11867
|
+
* const App = () => {
|
|
11868
|
+
* const handleInput = useSetQueryParamValueCallback(
|
|
11869
|
+
* 'dogOrCat',
|
|
11870
|
+
* 'species',
|
|
11871
|
+
* (e) => e.target.value,
|
|
11872
|
+
* [],
|
|
11873
|
+
* queries,
|
|
11874
|
+
* (_queries, paramValue) => console.log(`Updated: ${paramValue}`),
|
|
11875
|
+
* );
|
|
11876
|
+
* return (
|
|
11877
|
+
* <div>
|
|
11878
|
+
* <input id="input" onInput={handleInput} />
|
|
11879
|
+
* {JSON.stringify(useResultTable('dogOrCat', queries))}
|
|
11880
|
+
* </div>
|
|
11881
|
+
* );
|
|
11882
|
+
* };
|
|
11883
|
+
*
|
|
11884
|
+
* const app = document.createElement('div');
|
|
11885
|
+
* createRoot(app).render(<App />); // !act
|
|
11886
|
+
* const input = app.querySelector('input');
|
|
11887
|
+
* console.log(app.innerHTML);
|
|
11888
|
+
* // ->
|
|
11889
|
+
* `
|
|
11890
|
+
* <div>
|
|
11891
|
+
* <input id="input">
|
|
11892
|
+
* {"fido":{"species":"dog"},"cujo":{"species":"dog"}}
|
|
11893
|
+
* </div>
|
|
11894
|
+
* `;
|
|
11895
|
+
*
|
|
11896
|
+
* // User types 'cat' in the input and event fires:
|
|
11897
|
+
* input.value = 'cat';
|
|
11898
|
+
* // -> input Event('input', {bubbles: true})
|
|
11899
|
+
* // -> 'Updated: cat'
|
|
11900
|
+
*
|
|
11901
|
+
* console.log(app.innerHTML);
|
|
11902
|
+
* // ->
|
|
11903
|
+
* `
|
|
11904
|
+
* <div>
|
|
11905
|
+
* <input id="input">
|
|
11906
|
+
* {"felix":{"species":"cat"}}
|
|
11907
|
+
* </div>
|
|
11908
|
+
* `;
|
|
11909
|
+
* ```
|
|
11910
|
+
* @category Queries hooks
|
|
11911
|
+
* @since v7.2.0
|
|
11912
|
+
*/
|
|
11913
|
+
useSetQueryParamValueCallback: <Parameter>(
|
|
11914
|
+
queryId: Id | GetId<Schemas, Parameter, Id>,
|
|
11915
|
+
paramId: Id | GetId<Schemas, Parameter, Id>,
|
|
11916
|
+
getParamValue: (
|
|
11917
|
+
parameter: Parameter,
|
|
11918
|
+
queries: Queries<Schemas>,
|
|
11919
|
+
) => ParamValue,
|
|
11920
|
+
getParamValueDeps?: React.DependencyList,
|
|
11921
|
+
queriesOrQueriesId?: QueriesOrQueriesId<Schemas>,
|
|
11922
|
+
then?: (queries: Queries<Schemas>, paramValue: ParamValue) => void,
|
|
11923
|
+
thenDeps?: React.DependencyList,
|
|
11924
|
+
) => ParameterizedCallback<Parameter>;
|
|
11925
|
+
|
|
11926
|
+
/**
|
|
11927
|
+
* The useSetQueryParamValuesCallback hook returns a parameterized callback that
|
|
11928
|
+
* can be used to set multiple parameter values for a query at once.
|
|
11929
|
+
*
|
|
11930
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
11931
|
+
*
|
|
11932
|
+
* ```ts override
|
|
11933
|
+
* useSetQueryParamValuesCallback<Parameter>(
|
|
11934
|
+
* queryId: Id | GetId<Parameter>,
|
|
11935
|
+
* getParamValues: (parameter: Parameter, queries: Queries) => ParamValues,
|
|
11936
|
+
* getParamValuesDeps?: React.DependencyList,
|
|
11937
|
+
* queriesOrQueriesId?: QueriesOrQueriesId,
|
|
11938
|
+
* then?: (queries: Queries, paramValues: ParamValues) => void,
|
|
11939
|
+
* thenDeps?: React.DependencyList,
|
|
11940
|
+
* ): ParameterizedCallback<Parameter>;
|
|
11941
|
+
* ```
|
|
11942
|
+
*
|
|
11943
|
+
* This hook is useful, for example, when creating an event handler that will
|
|
11944
|
+
* update multiple query parameters based on user interaction. In this case, the
|
|
11945
|
+
* parameter will likely be the event, so that you can use data from it to
|
|
11946
|
+
* update the query parameters.
|
|
11947
|
+
*
|
|
11948
|
+
* The second parameter is a function which will produce the parameter values
|
|
11949
|
+
* object that will then be used to update the query in the callback.
|
|
11950
|
+
*
|
|
11951
|
+
* If that function has any other dependencies, the changing of which should
|
|
11952
|
+
* also cause the callback to be recreated, you can provide them in an array in
|
|
11953
|
+
* the optional third parameter, just as you would for any React hook with
|
|
11954
|
+
* dependencies.
|
|
11955
|
+
*
|
|
11956
|
+
* For convenience, you can optionally provide a `then` function (with its own
|
|
11957
|
+
* set of dependencies) which will be called just after the query parameters
|
|
11958
|
+
* have been updated.
|
|
11959
|
+
*
|
|
11960
|
+
* The Queries object to which the callback will make the mutation (indicated by
|
|
11961
|
+
* the hook's `queriesOrQueriesId` parameter) is always automatically used as a
|
|
11962
|
+
* hook dependency for the callback.
|
|
11963
|
+
* @param queryId The Id of the query to update, or a GetId function that will
|
|
11964
|
+
* return it.
|
|
11965
|
+
* @param getParamValues A function which returns the parameter values object
|
|
11966
|
+
* that will be used to update the query, based on the parameter the callback
|
|
11967
|
+
* will receive (and which is most likely a DOM event).
|
|
11968
|
+
* @param getParamValuesDeps An optional array of dependencies for the
|
|
11969
|
+
* `getParamValues` function, which, if any change, result in the regeneration
|
|
11970
|
+
* of the callback. This parameter defaults to an empty array. Also use this to
|
|
11971
|
+
* indicate the dependencies of any GetId functions if used as the queryId
|
|
11972
|
+
* argument.
|
|
11973
|
+
* @param queriesOrQueriesId The Queries object to be updated: omit for the
|
|
11974
|
+
* default context Queries object, provide an Id for a named context Queries
|
|
11975
|
+
* object, or provide an explicit reference.
|
|
11976
|
+
* @param then A function which is called after the mutation, with a reference
|
|
11977
|
+
* to the Queries object and the parameter values used in the update.
|
|
11978
|
+
* @param thenDeps An optional array of dependencies for the `then` function,
|
|
11979
|
+
* which, if any change, result in the regeneration of the callback. This
|
|
11980
|
+
* parameter defaults to an empty array.
|
|
11981
|
+
* @returns A parameterized callback for subsequent use.
|
|
11982
|
+
* @example
|
|
11983
|
+
* This example uses the useSetQueryParamValuesCallback hook to create an event
|
|
11984
|
+
* handler which updates multiple query parameters when a form is submitted.
|
|
11985
|
+
*
|
|
11986
|
+
* ```jsx
|
|
11987
|
+
* import React from 'react';
|
|
11988
|
+
* import {createRoot} from 'react-dom/client';
|
|
11989
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
11990
|
+
* import {
|
|
11991
|
+
* useResultTable,
|
|
11992
|
+
* useSetQueryParamValuesCallback,
|
|
11993
|
+
* } from 'tinybase/ui-react';
|
|
11994
|
+
*
|
|
11995
|
+
* const store = createStore().setTable('pets', {
|
|
11996
|
+
* fido: {species: 'dog', color: 'brown'},
|
|
11997
|
+
* felix: {species: 'cat', color: 'black'},
|
|
11998
|
+
* cujo: {species: 'dog', color: 'black'},
|
|
11999
|
+
* });
|
|
12000
|
+
* const queries = createQueries(store);
|
|
12001
|
+
* queries.setQueryDefinition(
|
|
12002
|
+
* 'speciesAndColor',
|
|
12003
|
+
* 'pets',
|
|
12004
|
+
* ({select, where, param}) => {
|
|
12005
|
+
* select('species');
|
|
12006
|
+
* select('color');
|
|
12007
|
+
* where('species', param('species'));
|
|
12008
|
+
* where('color', param('color'));
|
|
12009
|
+
* },
|
|
12010
|
+
* {species: 'dog', color: 'brown'},
|
|
12011
|
+
* );
|
|
12012
|
+
*
|
|
12013
|
+
* const App = () => {
|
|
12014
|
+
* const handleSubmit = useSetQueryParamValuesCallback(
|
|
12015
|
+
* 'speciesAndColor',
|
|
12016
|
+
* (e) => {
|
|
12017
|
+
* e.preventDefault();
|
|
12018
|
+
* return {
|
|
12019
|
+
* species: e.target.querySelector('#species').value,
|
|
12020
|
+
* color: e.target.querySelector('#color').value,
|
|
12021
|
+
* };
|
|
12022
|
+
* },
|
|
12023
|
+
* [],
|
|
12024
|
+
* queries,
|
|
12025
|
+
* (_, paramValues) =>
|
|
12026
|
+
* console.log(`Updated: ${JSON.stringify(paramValues)}`),
|
|
12027
|
+
* );
|
|
12028
|
+
* return (
|
|
12029
|
+
* <div>
|
|
12030
|
+
* <form onSubmit={handleSubmit}>
|
|
12031
|
+
* <input id="species" />
|
|
12032
|
+
* <input id="color" />
|
|
12033
|
+
* <button type="submit">Filter</button>
|
|
12034
|
+
* </form>
|
|
12035
|
+
* {JSON.stringify(useResultTable('speciesAndColor', queries))}
|
|
12036
|
+
* </div>
|
|
12037
|
+
* );
|
|
12038
|
+
* };
|
|
12039
|
+
*
|
|
12040
|
+
* const app = document.createElement('div');
|
|
12041
|
+
* createRoot(app).render(<App />); // !act
|
|
12042
|
+
* const form = app.querySelector('form');
|
|
12043
|
+
* console.log(app.innerHTML);
|
|
12044
|
+
* // ->
|
|
12045
|
+
* `
|
|
12046
|
+
* <div>
|
|
12047
|
+
* <form>
|
|
12048
|
+
* <input id="species">
|
|
12049
|
+
* <input id="color">
|
|
12050
|
+
* <button type="submit">Filter</button>
|
|
12051
|
+
* </form>
|
|
12052
|
+
* {"fido":{"species":"dog","color":"brown"}}
|
|
12053
|
+
* </div>
|
|
12054
|
+
* `;
|
|
12055
|
+
*
|
|
12056
|
+
* // User fills form with new values:
|
|
12057
|
+
* const species = form.querySelector('#species');
|
|
12058
|
+
* const color = form.querySelector('#color');
|
|
12059
|
+
*
|
|
12060
|
+
* species.value = 'cat'; // !act
|
|
12061
|
+
* color.value = 'black'; // !act
|
|
12062
|
+
*
|
|
12063
|
+
* // And submits the form:
|
|
12064
|
+
* // -> form SubmitEvent('submit', {bubbles: true})
|
|
12065
|
+
* // -> 'Updated: {"species":"cat","color":"black"}'
|
|
12066
|
+
*
|
|
12067
|
+
* console.log(app.innerHTML);
|
|
12068
|
+
* // ->
|
|
12069
|
+
* `
|
|
12070
|
+
* <div>
|
|
12071
|
+
* <form>
|
|
12072
|
+
* <input id="species">
|
|
12073
|
+
* <input id="color">
|
|
12074
|
+
* <button type="submit">Filter</button>
|
|
12075
|
+
* </form>
|
|
12076
|
+
* {"felix":{"species":"cat","color":"black"}}
|
|
12077
|
+
* </div>
|
|
12078
|
+
* `;
|
|
12079
|
+
* ```
|
|
12080
|
+
* @category Queries hooks
|
|
12081
|
+
* @since v7.2.0
|
|
12082
|
+
*/
|
|
12083
|
+
useSetQueryParamValuesCallback: <Parameter>(
|
|
12084
|
+
queryId: Id | GetId<Schemas, Parameter, Id>,
|
|
12085
|
+
getParamValues: (
|
|
12086
|
+
parameter: Parameter,
|
|
12087
|
+
queries: Queries<Schemas>,
|
|
12088
|
+
) => ParamValues,
|
|
12089
|
+
getParamValuesDeps?: React.DependencyList,
|
|
12090
|
+
queriesOrQueriesId?: QueriesOrQueriesId<Schemas>,
|
|
12091
|
+
then?: (queries: Queries<Schemas>, paramValues: ParamValues) => void,
|
|
12092
|
+
thenDeps?: React.DependencyList,
|
|
12093
|
+
) => ParameterizedCallback<Parameter>;
|
|
12094
|
+
|
|
11777
12095
|
/**
|
|
11778
12096
|
* The useCreateCheckpoints hook is used to create a Checkpoints object within a
|
|
11779
12097
|
* React application with convenient memoization.
|