tinybase 7.2.0-beta.1 → 7.2.0-beta.3
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 +333 -4
- package/@types/queries/with-schemas/index.d.ts +359 -4
- package/@types/ui-react/index.d.ts +484 -15
- package/@types/ui-react/with-schemas/index.d.ts +528 -17
- package/agents.md +34 -0
- package/index.js +67 -12
- 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 +161 -45
- package/omni/with-schemas/index.js +161 -45
- package/package.json +2 -2
- package/queries/index.js +80 -15
- package/queries/with-schemas/index.js +80 -15
- package/readme.md +3 -3
- package/releases.md +31 -3
- package/ui-react/index.js +98 -33
- package/ui-react/with-schemas/index.js +98 -33
- package/ui-react-dom/index.js +34 -14
- package/ui-react-dom/with-schemas/index.js +34 -14
- package/ui-react-inspector/index.js +36 -16
- package/ui-react-inspector/with-schemas/index.js +36 -16
- package/with-schemas/index.js +67 -12
|
@@ -53,7 +53,9 @@ import type {
|
|
|
53
53
|
} from '../persisters/index.d.ts';
|
|
54
54
|
import type {
|
|
55
55
|
ParamValue,
|
|
56
|
+
ParamValueListener,
|
|
56
57
|
ParamValues,
|
|
58
|
+
ParamValuesListener,
|
|
57
59
|
Queries,
|
|
58
60
|
ResultCellIdsListener,
|
|
59
61
|
ResultCellListener,
|
|
@@ -10731,8 +10733,478 @@ export function useResultCellListener(
|
|
|
10731
10733
|
): void;
|
|
10732
10734
|
|
|
10733
10735
|
/**
|
|
10734
|
-
* The
|
|
10735
|
-
*
|
|
10736
|
+
* The useParamValues hook returns an object containing all the parameter values
|
|
10737
|
+
* currently set for a query.
|
|
10738
|
+
*
|
|
10739
|
+
* A Provider component is used to wrap part of an application in a context, and
|
|
10740
|
+
* it can contain a default Queries object or a set of Queries objects named by
|
|
10741
|
+
* Id. The useParamValues hook lets you indicate which Queries object to get
|
|
10742
|
+
* data for: omit the optional final parameter for the default context Queries
|
|
10743
|
+
* object, provide an Id for a named context Queries object, or provide a
|
|
10744
|
+
* Queries object explicitly by reference.
|
|
10745
|
+
*
|
|
10746
|
+
* When first rendered, this hook will create a listener so that changes to the
|
|
10747
|
+
* parameter values will cause a re-render. When the component containing this
|
|
10748
|
+
* hook is unmounted, the listener will be automatically removed.
|
|
10749
|
+
* @param queryId The Id of the query to get parameter values for.
|
|
10750
|
+
* @param queriesOrQueriesId The Queries object to be accessed: omit for the
|
|
10751
|
+
* default context Queries object, provide an Id for a named context Queries
|
|
10752
|
+
* object, or provide an explicit reference.
|
|
10753
|
+
* @returns An object containing all parameter values for the query, or
|
|
10754
|
+
* undefined if the query doesn't exist.
|
|
10755
|
+
* @example
|
|
10756
|
+
* This example creates a Queries object outside the application, which is used
|
|
10757
|
+
* in the useParamValues hook by reference. A change to the parameter values
|
|
10758
|
+
* re-renders the component.
|
|
10759
|
+
*
|
|
10760
|
+
* ```jsx
|
|
10761
|
+
* import React from 'react';
|
|
10762
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
10763
|
+
* import {createRoot} from 'react-dom/client';
|
|
10764
|
+
* import {useParamValues} from 'tinybase/ui-react';
|
|
10765
|
+
*
|
|
10766
|
+
* const store = createStore().setTable('pets', {
|
|
10767
|
+
* fido: {species: 'dog', color: 'brown'},
|
|
10768
|
+
* felix: {species: 'cat', color: 'black'},
|
|
10769
|
+
* });
|
|
10770
|
+
* const queries = createQueries(store);
|
|
10771
|
+
* queries.setQueryDefinition(
|
|
10772
|
+
* 'petsBySpecies',
|
|
10773
|
+
* 'pets',
|
|
10774
|
+
* ({select, where, param}) => {
|
|
10775
|
+
* select('species');
|
|
10776
|
+
* where('species', param('species'));
|
|
10777
|
+
* },
|
|
10778
|
+
* {species: 'dog'},
|
|
10779
|
+
* );
|
|
10780
|
+
*
|
|
10781
|
+
* const App = () => (
|
|
10782
|
+
* <span>{JSON.stringify(useParamValues('petsBySpecies', queries))}</span>
|
|
10783
|
+
* );
|
|
10784
|
+
*
|
|
10785
|
+
* const app = document.createElement('div');
|
|
10786
|
+
* createRoot(app).render(<App />); // !act
|
|
10787
|
+
* console.log(app.innerHTML);
|
|
10788
|
+
* // -> '<span>{"species":"dog"}</span>'
|
|
10789
|
+
*
|
|
10790
|
+
* queries.setParamValue('petsBySpecies', 'species', 'cat'); // !act
|
|
10791
|
+
* console.log(app.innerHTML);
|
|
10792
|
+
* // -> '<span>{"species":"cat"}</span>'
|
|
10793
|
+
* ```
|
|
10794
|
+
* @example
|
|
10795
|
+
* This example creates a Provider context into which a default Queries object
|
|
10796
|
+
* is provided. A component within it then uses the useParamValues hook.
|
|
10797
|
+
*
|
|
10798
|
+
* ```jsx
|
|
10799
|
+
* import {Provider, useParamValues} from 'tinybase/ui-react';
|
|
10800
|
+
* import React from 'react';
|
|
10801
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
10802
|
+
* import {createRoot} from 'react-dom/client';
|
|
10803
|
+
*
|
|
10804
|
+
* const App = ({queries}) => (
|
|
10805
|
+
* <Provider queries={queries}>
|
|
10806
|
+
* <Pane />
|
|
10807
|
+
* </Provider>
|
|
10808
|
+
* );
|
|
10809
|
+
* const Pane = () => (
|
|
10810
|
+
* <span>{JSON.stringify(useParamValues('petsBySpecies'))}</span>
|
|
10811
|
+
* );
|
|
10812
|
+
*
|
|
10813
|
+
* const store = createStore().setTable('pets', {
|
|
10814
|
+
* fido: {species: 'dog'},
|
|
10815
|
+
* felix: {species: 'cat'},
|
|
10816
|
+
* });
|
|
10817
|
+
* const queries = createQueries(store);
|
|
10818
|
+
* queries.setQueryDefinition(
|
|
10819
|
+
* 'petsBySpecies',
|
|
10820
|
+
* 'pets',
|
|
10821
|
+
* ({select, where, param}) => {
|
|
10822
|
+
* select('species');
|
|
10823
|
+
* where('species', param('species'));
|
|
10824
|
+
* },
|
|
10825
|
+
* {species: 'dog'},
|
|
10826
|
+
* );
|
|
10827
|
+
*
|
|
10828
|
+
* const app = document.createElement('div');
|
|
10829
|
+
* createRoot(app).render(<App queries={queries} />); // !act
|
|
10830
|
+
* console.log(app.innerHTML);
|
|
10831
|
+
* // -> '<span>{"species":"dog"}</span>'
|
|
10832
|
+
* ```
|
|
10833
|
+
* @example
|
|
10834
|
+
* This example creates a Provider context into which a Queries object is
|
|
10835
|
+
* provided, named by Id. A component within it then uses the useParamValues
|
|
10836
|
+
* hook.
|
|
10837
|
+
*
|
|
10838
|
+
* ```jsx
|
|
10839
|
+
* import {Provider, useParamValues} from 'tinybase/ui-react';
|
|
10840
|
+
* import React from 'react';
|
|
10841
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
10842
|
+
* import {createRoot} from 'react-dom/client';
|
|
10843
|
+
*
|
|
10844
|
+
* const App = ({queries}) => (
|
|
10845
|
+
* <Provider queriesById={{petQueries: queries}}>
|
|
10846
|
+
* <Pane />
|
|
10847
|
+
* </Provider>
|
|
10848
|
+
* );
|
|
10849
|
+
* const Pane = () => (
|
|
10850
|
+
* <span>
|
|
10851
|
+
* {JSON.stringify(useParamValues('petsBySpecies', 'petQueries'))}
|
|
10852
|
+
* </span>
|
|
10853
|
+
* );
|
|
10854
|
+
*
|
|
10855
|
+
* const store = createStore().setTable('pets', {
|
|
10856
|
+
* fido: {species: 'dog'},
|
|
10857
|
+
* felix: {species: 'cat'},
|
|
10858
|
+
* });
|
|
10859
|
+
* const queries = createQueries(store);
|
|
10860
|
+
* queries.setQueryDefinition(
|
|
10861
|
+
* 'petsBySpecies',
|
|
10862
|
+
* 'pets',
|
|
10863
|
+
* ({select, where, param}) => {
|
|
10864
|
+
* select('species');
|
|
10865
|
+
* where('species', param('species'));
|
|
10866
|
+
* },
|
|
10867
|
+
* {species: 'dog'},
|
|
10868
|
+
* );
|
|
10869
|
+
*
|
|
10870
|
+
* const app = document.createElement('div');
|
|
10871
|
+
* createRoot(app).render(<App queries={queries} />); // !act
|
|
10872
|
+
* console.log(app.innerHTML);
|
|
10873
|
+
* // -> '<span>{"species":"dog"}</span>'
|
|
10874
|
+
* ```
|
|
10875
|
+
* @category Queries hooks
|
|
10876
|
+
* @since v7.2.0
|
|
10877
|
+
*/
|
|
10878
|
+
export function useParamValues(
|
|
10879
|
+
queryId: Id,
|
|
10880
|
+
queriesOrQueriesId?: QueriesOrQueriesId,
|
|
10881
|
+
): ParamValues | undefined;
|
|
10882
|
+
|
|
10883
|
+
/**
|
|
10884
|
+
* The useParamValue hook returns the current value of a single parameter in a
|
|
10885
|
+
* query.
|
|
10886
|
+
*
|
|
10887
|
+
* A Provider component is used to wrap part of an application in a context, and
|
|
10888
|
+
* it can contain a default Queries object or a set of Queries objects named by
|
|
10889
|
+
* Id. The useParamValue hook lets you indicate which Queries object to get data
|
|
10890
|
+
* for: omit the optional final parameter for the default context Queries
|
|
10891
|
+
* object, provide an Id for a named context Queries object, or provide a
|
|
10892
|
+
* Queries object explicitly by reference.
|
|
10893
|
+
*
|
|
10894
|
+
* When first rendered, this hook will create a listener so that changes to the
|
|
10895
|
+
* parameter value will cause a re-render. When the component containing this
|
|
10896
|
+
* hook is unmounted, the listener will be automatically removed.
|
|
10897
|
+
* @param queryId The Id of the query to get the parameter value from.
|
|
10898
|
+
* @param paramId The Id of the parameter to get the value of.
|
|
10899
|
+
* @param queriesOrQueriesId The Queries object to be accessed: omit for the
|
|
10900
|
+
* default context Queries object, provide an Id for a named context Queries
|
|
10901
|
+
* object, or provide an explicit reference.
|
|
10902
|
+
* @returns The value of the parameter, or undefined if it doesn't exist.
|
|
10903
|
+
* @example
|
|
10904
|
+
* This example creates a Queries object outside the application, which is used
|
|
10905
|
+
* in the useParamValue hook by reference. A change to the parameter value
|
|
10906
|
+
* re-renders the component.
|
|
10907
|
+
*
|
|
10908
|
+
* ```jsx
|
|
10909
|
+
* import React from 'react';
|
|
10910
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
10911
|
+
* import {createRoot} from 'react-dom/client';
|
|
10912
|
+
* import {useParamValue} from 'tinybase/ui-react';
|
|
10913
|
+
*
|
|
10914
|
+
* const store = createStore().setTable('pets', {
|
|
10915
|
+
* fido: {species: 'dog', color: 'brown'},
|
|
10916
|
+
* felix: {species: 'cat', color: 'black'},
|
|
10917
|
+
* });
|
|
10918
|
+
* const queries = createQueries(store);
|
|
10919
|
+
* queries.setQueryDefinition(
|
|
10920
|
+
* 'petsBySpecies',
|
|
10921
|
+
* 'pets',
|
|
10922
|
+
* ({select, where, param}) => {
|
|
10923
|
+
* select('species');
|
|
10924
|
+
* where('species', param('species'));
|
|
10925
|
+
* },
|
|
10926
|
+
* {species: 'dog'},
|
|
10927
|
+
* );
|
|
10928
|
+
*
|
|
10929
|
+
* const App = () => (
|
|
10930
|
+
* <span>{useParamValue('petsBySpecies', 'species', queries)}</span>
|
|
10931
|
+
* );
|
|
10932
|
+
*
|
|
10933
|
+
* const app = document.createElement('div');
|
|
10934
|
+
* createRoot(app).render(<App />); // !act
|
|
10935
|
+
* console.log(app.innerHTML);
|
|
10936
|
+
* // -> '<span>dog</span>'
|
|
10937
|
+
*
|
|
10938
|
+
* queries.setParamValue('petsBySpecies', 'species', 'cat'); // !act
|
|
10939
|
+
* console.log(app.innerHTML);
|
|
10940
|
+
* // -> '<span>cat</span>'
|
|
10941
|
+
* ```
|
|
10942
|
+
* @example
|
|
10943
|
+
* This example creates a Provider context into which a default Queries object
|
|
10944
|
+
* is provided. A component within it then uses the useParamValue hook.
|
|
10945
|
+
*
|
|
10946
|
+
* ```jsx
|
|
10947
|
+
* import {Provider, useParamValue} from 'tinybase/ui-react';
|
|
10948
|
+
* import React from 'react';
|
|
10949
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
10950
|
+
* import {createRoot} from 'react-dom/client';
|
|
10951
|
+
*
|
|
10952
|
+
* const App = ({queries}) => (
|
|
10953
|
+
* <Provider queries={queries}>
|
|
10954
|
+
* <Pane />
|
|
10955
|
+
* </Provider>
|
|
10956
|
+
* );
|
|
10957
|
+
* const Pane = () => (
|
|
10958
|
+
* <span>{useParamValue('petsBySpecies', 'species')}</span>
|
|
10959
|
+
* );
|
|
10960
|
+
*
|
|
10961
|
+
* const store = createStore().setTable('pets', {
|
|
10962
|
+
* fido: {species: 'dog'},
|
|
10963
|
+
* felix: {species: 'cat'},
|
|
10964
|
+
* });
|
|
10965
|
+
* const queries = createQueries(store);
|
|
10966
|
+
* queries.setQueryDefinition(
|
|
10967
|
+
* 'petsBySpecies',
|
|
10968
|
+
* 'pets',
|
|
10969
|
+
* ({select, where, param}) => {
|
|
10970
|
+
* select('species');
|
|
10971
|
+
* where('species', param('species'));
|
|
10972
|
+
* },
|
|
10973
|
+
* {species: 'dog'},
|
|
10974
|
+
* );
|
|
10975
|
+
*
|
|
10976
|
+
* const app = document.createElement('div');
|
|
10977
|
+
* createRoot(app).render(<App queries={queries} />); // !act
|
|
10978
|
+
* console.log(app.innerHTML);
|
|
10979
|
+
* // -> '<span>dog</span>'
|
|
10980
|
+
* ```
|
|
10981
|
+
* @example
|
|
10982
|
+
* This example creates a Provider context into which a Queries object is
|
|
10983
|
+
* provided, named by Id. A component within it then uses the useParamValue
|
|
10984
|
+
* hook.
|
|
10985
|
+
*
|
|
10986
|
+
* ```jsx
|
|
10987
|
+
* import {Provider, useParamValue} from 'tinybase/ui-react';
|
|
10988
|
+
* import React from 'react';
|
|
10989
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
10990
|
+
* import {createRoot} from 'react-dom/client';
|
|
10991
|
+
*
|
|
10992
|
+
* const App = ({queries}) => (
|
|
10993
|
+
* <Provider queriesById={{petQueries: queries}}>
|
|
10994
|
+
* <Pane />
|
|
10995
|
+
* </Provider>
|
|
10996
|
+
* );
|
|
10997
|
+
* const Pane = () => (
|
|
10998
|
+
* <span>{useParamValue('petsBySpecies', 'species', 'petQueries')}</span>
|
|
10999
|
+
* );
|
|
11000
|
+
*
|
|
11001
|
+
* const store = createStore().setTable('pets', {
|
|
11002
|
+
* fido: {species: 'dog'},
|
|
11003
|
+
* felix: {species: 'cat'},
|
|
11004
|
+
* });
|
|
11005
|
+
* const queries = createQueries(store);
|
|
11006
|
+
* queries.setQueryDefinition(
|
|
11007
|
+
* 'petsBySpecies',
|
|
11008
|
+
* 'pets',
|
|
11009
|
+
* ({select, where, param}) => {
|
|
11010
|
+
* select('species');
|
|
11011
|
+
* where('species', param('species'));
|
|
11012
|
+
* },
|
|
11013
|
+
* {species: 'dog'},
|
|
11014
|
+
* );
|
|
11015
|
+
*
|
|
11016
|
+
* const app = document.createElement('div');
|
|
11017
|
+
* createRoot(app).render(<App queries={queries} />); // !act
|
|
11018
|
+
* console.log(app.innerHTML);
|
|
11019
|
+
* // -> '<span>dog</span>'
|
|
11020
|
+
* ```
|
|
11021
|
+
* @category Queries hooks
|
|
11022
|
+
* @since v7.2.0
|
|
11023
|
+
*/
|
|
11024
|
+
export function useParamValue(
|
|
11025
|
+
queryId: Id,
|
|
11026
|
+
paramId: Id,
|
|
11027
|
+
queriesOrQueriesId?: QueriesOrQueriesId,
|
|
11028
|
+
): ParamValue | undefined;
|
|
11029
|
+
|
|
11030
|
+
/**
|
|
11031
|
+
* The useParamValuesListener hook registers a listener function with a Queries
|
|
11032
|
+
* object that will be called whenever the parameter values for a query change.
|
|
11033
|
+
*
|
|
11034
|
+
* This hook is useful for situations where a component needs to register its
|
|
11035
|
+
* own specific listener to do more than simply tracking the values (which is
|
|
11036
|
+
* more easily done with the useParamValues hook).
|
|
11037
|
+
*
|
|
11038
|
+
* Unlike the addParamValuesListener method, which returns a listener Id and
|
|
11039
|
+
* requires you to remove it manually, the useParamValuesListener hook manages
|
|
11040
|
+
* this lifecycle for you: when the listener changes (per its `listenerDeps`
|
|
11041
|
+
* dependencies) or the component unmounts, the listener on the underlying
|
|
11042
|
+
* Queries object will be deleted.
|
|
11043
|
+
* @param queryId The Id of the query to listen to, or `null` as a wildcard.
|
|
11044
|
+
* @param listener The function that will be called whenever the parameter
|
|
11045
|
+
* values for the query change.
|
|
11046
|
+
* @param listenerDeps An optional array of dependencies for the `listener`
|
|
11047
|
+
* function, which, if any change, result in the re-registration of the
|
|
11048
|
+
* listener. This parameter defaults to an empty array.
|
|
11049
|
+
* @param queriesOrQueriesId The Queries object to register the listener with:
|
|
11050
|
+
* omit for the default context Queries object, provide an Id for a named
|
|
11051
|
+
* context Queries object, or provide an explicit reference.
|
|
11052
|
+
* @example
|
|
11053
|
+
* This example uses the useParamValuesListener hook to create a listener that
|
|
11054
|
+
* is scoped to a single component. When the component is unmounted, the
|
|
11055
|
+
* listener is removed from the Queries object.
|
|
11056
|
+
*
|
|
11057
|
+
* ```jsx
|
|
11058
|
+
* import React from 'react';
|
|
11059
|
+
* import {createRoot} from 'react-dom/client';
|
|
11060
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
11061
|
+
* import {Provider, useParamValuesListener} from 'tinybase/ui-react';
|
|
11062
|
+
*
|
|
11063
|
+
* const App = ({queries}) => (
|
|
11064
|
+
* <Provider queries={queries}>
|
|
11065
|
+
* <Pane />
|
|
11066
|
+
* </Provider>
|
|
11067
|
+
* );
|
|
11068
|
+
* const Pane = () => {
|
|
11069
|
+
* useParamValuesListener('petsBySpecies', () =>
|
|
11070
|
+
* console.log('Param values changed'),
|
|
11071
|
+
* );
|
|
11072
|
+
* return <span>App</span>;
|
|
11073
|
+
* };
|
|
11074
|
+
*
|
|
11075
|
+
* const store = createStore().setTable('pets', {
|
|
11076
|
+
* fido: {species: 'dog'},
|
|
11077
|
+
* felix: {species: 'cat'},
|
|
11078
|
+
* });
|
|
11079
|
+
* const queries = createQueries(store);
|
|
11080
|
+
* queries.setQueryDefinition(
|
|
11081
|
+
* 'petsBySpecies',
|
|
11082
|
+
* 'pets',
|
|
11083
|
+
* ({select, where, param}) => {
|
|
11084
|
+
* select('species');
|
|
11085
|
+
* where('species', param('species'));
|
|
11086
|
+
* },
|
|
11087
|
+
* {species: 'dog'},
|
|
11088
|
+
* );
|
|
11089
|
+
* const app = document.createElement('div');
|
|
11090
|
+
* const root = createRoot(app);
|
|
11091
|
+
* root.render(<App queries={queries} />); // !act
|
|
11092
|
+
* console.log(queries.getListenerStats().paramValues);
|
|
11093
|
+
* // -> 1
|
|
11094
|
+
*
|
|
11095
|
+
* queries.setParamValue('petsBySpecies', 'species', 'cat'); // !act
|
|
11096
|
+
* // -> 'Param values changed'
|
|
11097
|
+
*
|
|
11098
|
+
* root.unmount(); // !act
|
|
11099
|
+
* console.log(queries.getListenerStats().paramValues);
|
|
11100
|
+
* // -> 0
|
|
11101
|
+
* ```
|
|
11102
|
+
* @category Queries hooks
|
|
11103
|
+
* @since v7.2.0
|
|
11104
|
+
*/
|
|
11105
|
+
export function useParamValuesListener(
|
|
11106
|
+
queryId: IdOrNull,
|
|
11107
|
+
listener: ParamValuesListener,
|
|
11108
|
+
listenerDeps?: React.DependencyList,
|
|
11109
|
+
queriesOrQueriesId?: QueriesOrQueriesId,
|
|
11110
|
+
): void;
|
|
11111
|
+
|
|
11112
|
+
/**
|
|
11113
|
+
* The useParamValueListener hook registers a listener function with a Queries
|
|
11114
|
+
* object that will be called whenever a single parameter value for a query
|
|
11115
|
+
* changes.
|
|
11116
|
+
*
|
|
11117
|
+
* This hook is useful for situations where a component needs to register its
|
|
11118
|
+
* own specific listener to do more than simply tracking the value (which is
|
|
11119
|
+
* more easily done with the useParamValue hook).
|
|
11120
|
+
*
|
|
11121
|
+
* You can either listen to a single parameter (by specifying the query Id and
|
|
11122
|
+
* parameter Id as the method's first two parameters) or changes to any
|
|
11123
|
+
* parameter (by providing `null` wildcards).
|
|
11124
|
+
*
|
|
11125
|
+
* Both the `queryId` and `paramId` parameters can be wildcarded with `null`.
|
|
11126
|
+
* You can listen to a specific parameter in a specific query, any parameter in
|
|
11127
|
+
* any query, for example - or every other combination of wildcards.
|
|
11128
|
+
*
|
|
11129
|
+
* Unlike the addParamValueListener method, which returns a listener Id and
|
|
11130
|
+
* requires you to remove it manually, the useParamValueListener hook manages
|
|
11131
|
+
* this lifecycle for you: when the listener changes (per its `listenerDeps`
|
|
11132
|
+
* dependencies) or the component unmounts, the listener on the underlying
|
|
11133
|
+
* Queries object will be deleted.
|
|
11134
|
+
* @param queryId The Id of the query to listen to, or `null` as a wildcard.
|
|
11135
|
+
* @param paramId The Id of the parameter to listen to, or `null` as a wildcard.
|
|
11136
|
+
* @param listener The function that will be called whenever the parameter value
|
|
11137
|
+
* changes.
|
|
11138
|
+
* @param listenerDeps An optional array of dependencies for the `listener`
|
|
11139
|
+
* function, which, if any change, result in the re-registration of the
|
|
11140
|
+
* listener. This parameter defaults to an empty array.
|
|
11141
|
+
* @param queriesOrQueriesId The Queries object to register the listener with:
|
|
11142
|
+
* omit for the default context Queries object, provide an Id for a named
|
|
11143
|
+
* context Queries object, or provide an explicit reference.
|
|
11144
|
+
* @example
|
|
11145
|
+
* This example uses the useParamValueListener hook to create a listener that
|
|
11146
|
+
* is scoped to a single component. When the component is unmounted, the
|
|
11147
|
+
* listener is removed from the Queries object.
|
|
11148
|
+
*
|
|
11149
|
+
* ```jsx
|
|
11150
|
+
* import React from 'react';
|
|
11151
|
+
* import {createRoot} from 'react-dom/client';
|
|
11152
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
11153
|
+
* import {Provider, useParamValueListener} from 'tinybase/ui-react';
|
|
11154
|
+
*
|
|
11155
|
+
* const App = ({queries}) => (
|
|
11156
|
+
* <Provider queries={queries}>
|
|
11157
|
+
* <Pane />
|
|
11158
|
+
* </Provider>
|
|
11159
|
+
* );
|
|
11160
|
+
* const Pane = () => {
|
|
11161
|
+
* useParamValueListener('petsBySpecies', 'species', () =>
|
|
11162
|
+
* console.log('Param value changed'),
|
|
11163
|
+
* );
|
|
11164
|
+
* return <span>App</span>;
|
|
11165
|
+
* };
|
|
11166
|
+
*
|
|
11167
|
+
* const store = createStore().setTable('pets', {
|
|
11168
|
+
* fido: {species: 'dog'},
|
|
11169
|
+
* felix: {species: 'cat'},
|
|
11170
|
+
* });
|
|
11171
|
+
* const queries = createQueries(store);
|
|
11172
|
+
* queries.setQueryDefinition(
|
|
11173
|
+
* 'petsBySpecies',
|
|
11174
|
+
* 'pets',
|
|
11175
|
+
* ({select, where, param}) => {
|
|
11176
|
+
* select('species');
|
|
11177
|
+
* where('species', param('species'));
|
|
11178
|
+
* },
|
|
11179
|
+
* {species: 'dog'},
|
|
11180
|
+
* );
|
|
11181
|
+
* const app = document.createElement('div');
|
|
11182
|
+
* const root = createRoot(app);
|
|
11183
|
+
* root.render(<App queries={queries} />); // !act
|
|
11184
|
+
* console.log(queries.getListenerStats().paramValue);
|
|
11185
|
+
* // -> 1
|
|
11186
|
+
*
|
|
11187
|
+
* queries.setParamValue('petsBySpecies', 'species', 'cat'); // !act
|
|
11188
|
+
* // -> 'Param value changed'
|
|
11189
|
+
*
|
|
11190
|
+
* root.unmount(); // !act
|
|
11191
|
+
* console.log(queries.getListenerStats().paramValue);
|
|
11192
|
+
* // -> 0
|
|
11193
|
+
* ```
|
|
11194
|
+
* @category Queries hooks
|
|
11195
|
+
* @since v7.2.0
|
|
11196
|
+
*/
|
|
11197
|
+
export function useParamValueListener(
|
|
11198
|
+
queryId: IdOrNull,
|
|
11199
|
+
paramId: IdOrNull,
|
|
11200
|
+
listener: ParamValueListener,
|
|
11201
|
+
listenerDeps?: React.DependencyList,
|
|
11202
|
+
queriesOrQueriesId?: QueriesOrQueriesId,
|
|
11203
|
+
): void;
|
|
11204
|
+
|
|
11205
|
+
/**
|
|
11206
|
+
* The useSetParamValueCallback hook returns a parameterized callback that can
|
|
11207
|
+
* be used to set a single parameter value for a query.
|
|
10736
11208
|
*
|
|
10737
11209
|
* This hook is useful, for example, when creating an event handler that will
|
|
10738
11210
|
* update query parameters based on user interaction. In this case, the
|
|
@@ -10776,17 +11248,14 @@ export function useResultCellListener(
|
|
|
10776
11248
|
* parameter defaults to an empty array.
|
|
10777
11249
|
* @returns A parameterized callback for subsequent use.
|
|
10778
11250
|
* @example
|
|
10779
|
-
* This example uses the
|
|
11251
|
+
* This example uses the useSetParamValueCallback hook to create an event
|
|
10780
11252
|
* handler which updates a query parameter when an input element changes.
|
|
10781
11253
|
*
|
|
10782
11254
|
* ```jsx
|
|
10783
11255
|
* import React from 'react';
|
|
10784
11256
|
* import {createRoot} from 'react-dom/client';
|
|
10785
11257
|
* import {createQueries, createStore} from 'tinybase';
|
|
10786
|
-
* import {
|
|
10787
|
-
* useResultTable,
|
|
10788
|
-
* useSetQueryParamValueCallback,
|
|
10789
|
-
* } from 'tinybase/ui-react';
|
|
11258
|
+
* import {useResultTable, useSetParamValueCallback} from 'tinybase/ui-react';
|
|
10790
11259
|
*
|
|
10791
11260
|
* const store = createStore().setTable('pets', {
|
|
10792
11261
|
* fido: {species: 'dog', color: 'brown'},
|
|
@@ -10805,7 +11274,7 @@ export function useResultCellListener(
|
|
|
10805
11274
|
* );
|
|
10806
11275
|
*
|
|
10807
11276
|
* const App = () => {
|
|
10808
|
-
* const handleInput =
|
|
11277
|
+
* const handleInput = useSetParamValueCallback(
|
|
10809
11278
|
* 'dogOrCat',
|
|
10810
11279
|
* 'species',
|
|
10811
11280
|
* (e) => e.target.value,
|
|
@@ -10850,7 +11319,7 @@ export function useResultCellListener(
|
|
|
10850
11319
|
* @category Queries hooks
|
|
10851
11320
|
* @since v7.2.0
|
|
10852
11321
|
*/
|
|
10853
|
-
export function
|
|
11322
|
+
export function useSetParamValueCallback<Parameter>(
|
|
10854
11323
|
queryId: Id | GetId<Parameter>,
|
|
10855
11324
|
paramId: Id | GetId<Parameter>,
|
|
10856
11325
|
getParamValue: (parameter: Parameter, queries: Queries) => ParamValue,
|
|
@@ -10861,8 +11330,8 @@ export function useSetQueryParamValueCallback<Parameter>(
|
|
|
10861
11330
|
): ParameterizedCallback<Parameter>;
|
|
10862
11331
|
|
|
10863
11332
|
/**
|
|
10864
|
-
* The
|
|
10865
|
-
*
|
|
11333
|
+
* The useSetParamValuesCallback hook returns a parameterized callback that can
|
|
11334
|
+
* be used to set multiple parameter values for a query at once.
|
|
10866
11335
|
*
|
|
10867
11336
|
* This hook is useful, for example, when creating an event handler that will
|
|
10868
11337
|
* update multiple query parameters based on user interaction. In this case, the
|
|
@@ -10904,7 +11373,7 @@ export function useSetQueryParamValueCallback<Parameter>(
|
|
|
10904
11373
|
* parameter defaults to an empty array.
|
|
10905
11374
|
* @returns A parameterized callback for subsequent use.
|
|
10906
11375
|
* @example
|
|
10907
|
-
* This example uses the
|
|
11376
|
+
* This example uses the useSetParamValuesCallback hook to create an event
|
|
10908
11377
|
* handler which updates multiple query parameters when a form is submitted.
|
|
10909
11378
|
*
|
|
10910
11379
|
* ```jsx
|
|
@@ -10913,7 +11382,7 @@ export function useSetQueryParamValueCallback<Parameter>(
|
|
|
10913
11382
|
* import {createQueries, createStore} from 'tinybase';
|
|
10914
11383
|
* import {
|
|
10915
11384
|
* useResultTable,
|
|
10916
|
-
*
|
|
11385
|
+
* useSetParamValuesCallback,
|
|
10917
11386
|
* } from 'tinybase/ui-react';
|
|
10918
11387
|
*
|
|
10919
11388
|
* const store = createStore().setTable('pets', {
|
|
@@ -10935,7 +11404,7 @@ export function useSetQueryParamValueCallback<Parameter>(
|
|
|
10935
11404
|
* );
|
|
10936
11405
|
*
|
|
10937
11406
|
* const App = () => {
|
|
10938
|
-
* const handleSubmit =
|
|
11407
|
+
* const handleSubmit = useSetParamValuesCallback(
|
|
10939
11408
|
* 'speciesAndColor',
|
|
10940
11409
|
* (e) => {
|
|
10941
11410
|
* e.preventDefault();
|
|
@@ -11004,7 +11473,7 @@ export function useSetQueryParamValueCallback<Parameter>(
|
|
|
11004
11473
|
* @category Queries hooks
|
|
11005
11474
|
* @since v7.2.0
|
|
11006
11475
|
*/
|
|
11007
|
-
export function
|
|
11476
|
+
export function useSetParamValuesCallback<Parameter>(
|
|
11008
11477
|
queryId: Id | GetId<Parameter>,
|
|
11009
11478
|
getParamValues: (parameter: Parameter, queries: Queries) => ParamValues,
|
|
11010
11479
|
getParamValuesDeps?: React.DependencyList,
|