@wordpress/views 1.15.1 → 1.16.0
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/README.md +2 -1
- package/build/filter-utils.cjs.map +1 -1
- package/build/use-view-config.cjs +8 -3
- package/build/use-view-config.cjs.map +2 -2
- package/build-module/filter-utils.mjs.map +1 -1
- package/build-module/use-view-config.mjs +8 -3
- package/build-module/use-view-config.mjs.map +2 -2
- package/build-types/use-view-config.d.ts +15 -8
- package/build-types/use-view-config.d.ts.map +1 -1
- package/package.json +8 -8
- package/src/use-view-config.ts +25 -9
package/README.md
CHANGED
|
@@ -60,9 +60,10 @@ _Parameters_
|
|
|
60
60
|
- _params_ `Object`:
|
|
61
61
|
- _params.kind_ `string`: The kind of the entity.
|
|
62
62
|
- _params.name_ `string`: The name of the entity.
|
|
63
|
+
- _params.fields_ `[?(string|string[])]`: Subset of top-level config properties to request, as an array or a comma-separated string (mapped to the REST API `_fields` parameter). When omitted, the full config is requested.
|
|
63
64
|
|
|
64
65
|
_Returns_
|
|
65
66
|
|
|
66
|
-
- `Object`: An object containing the `default_view`, `default_layouts`, and `
|
|
67
|
+
- `Object`: An object containing the `default_view`, `default_layouts`, `view_list`, and `form` configuration for the entity.
|
|
67
68
|
|
|
68
69
|
<!-- END TOKEN(Autogenerated API docs) -->
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/filter-utils.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport type { View, Filter } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport type { ActiveViewOverrides } from './types';\n\nconst SCALAR_VALUES = [\n\t'titleField',\n\t'mediaField',\n\t'descriptionField',\n\t'showTitle',\n\t'showMedia',\n\t'showDescription',\n\t'showLevels',\n\t'infiniteScrollEnabled',\n] as const;\n\n/**\n * Merges activeViewOverrides into a view.\n * Filters: Active filters take precedence; same-field filters are replaced.\n * Sort: Active sort is applied only if current sort matches the default.\n *\n * @param view The view to merge overrides into.\n * @param activeViewOverrides The tab-specific overrides to apply.\n * @param defaultView The default view configuration.\n * @return A new view with merged overrides, or the original view if no overrides.\n */\nexport function mergeActiveViewOverrides(\n\tview: View,\n\tactiveViewOverrides?: ActiveViewOverrides,\n\tdefaultView?: View\n): View {\n\tif ( ! activeViewOverrides ) {\n\t\treturn view;\n\t}\n\n\tlet result = view;\n\n\t// Merge scalar overrides
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport type { View, Filter } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport type { ActiveViewOverrides } from './types';\n\nconst SCALAR_VALUES = [\n\t'titleField',\n\t'mediaField',\n\t'descriptionField',\n\t'showTitle',\n\t'showMedia',\n\t'showDescription',\n\t'showLevels',\n\t'infiniteScrollEnabled',\n] as const;\n\n/**\n * Merges activeViewOverrides into a view.\n * Filters: Active filters take precedence; same-field filters are replaced.\n * Sort: Active sort is applied only if current sort matches the default.\n *\n * @param view The view to merge overrides into.\n * @param activeViewOverrides The tab-specific overrides to apply.\n * @param defaultView The default view configuration.\n * @return A new view with merged overrides, or the original view if no overrides.\n */\nexport function mergeActiveViewOverrides(\n\tview: View,\n\tactiveViewOverrides?: ActiveViewOverrides,\n\tdefaultView?: View\n): View {\n\tif ( ! activeViewOverrides ) {\n\t\treturn view;\n\t}\n\n\tlet result = view;\n\n\t// Merge scalar overrides — always win over persisted values\n\tfor ( const key of SCALAR_VALUES ) {\n\t\tif ( key in activeViewOverrides ) {\n\t\t\tresult = { ...result, [ key ]: activeViewOverrides[ key ] };\n\t\t}\n\t}\n\n\t// Merge filters\n\tif (\n\t\tactiveViewOverrides.filters &&\n\t\tactiveViewOverrides.filters.length > 0\n\t) {\n\t\tconst activeFields = new Set(\n\t\t\tactiveViewOverrides.filters.map( ( f ) => f.field )\n\t\t);\n\t\tconst preserved = ( view.filters ?? [] ).filter(\n\t\t\t( f: Filter ) => ! activeFields.has( f.field )\n\t\t);\n\t\tresult = {\n\t\t\t...result,\n\t\t\tfilters: [ ...preserved, ...activeViewOverrides.filters ],\n\t\t};\n\t}\n\n\t// Merge sort - only apply if the current sort matches the default\n\tif ( activeViewOverrides.sort ) {\n\t\tconst isDefaultSort =\n\t\t\tdefaultView &&\n\t\t\tview.sort?.field === defaultView.sort?.field &&\n\t\t\tview.sort?.direction === defaultView.sort?.direction;\n\n\t\tif ( isDefaultSort ) {\n\t\t\tresult = {\n\t\t\t\t...result,\n\t\t\t\tsort: activeViewOverrides.sort,\n\t\t\t};\n\t\t}\n\t}\n\n\t// Merge layout — shallow merge, override keys always win\n\tif ( activeViewOverrides.layout ) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tlayout: {\n\t\t\t\t...( result as any ).layout,\n\t\t\t\t...activeViewOverrides.layout,\n\t\t\t},\n\t\t} as View;\n\t}\n\n\t// Merge groupBy — full replacement, override always wins\n\tif ( activeViewOverrides.groupBy ) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tgroupBy: activeViewOverrides.groupBy,\n\t\t};\n\t}\n\n\treturn result;\n}\n\n/**\n * Strips overrides before persisting.\n * Filters: Removes filters on fields managed by activeViewOverrides.\n * Sort: If sort matches the override, restores the default sort.\n *\n * @param view The view to strip overrides from.\n * @param activeViewOverrides The tab-specific override definitions.\n * @param defaultView The default view configuration.\n * @return A new view with overrides stripped, or the original view if no overrides.\n */\nexport function stripActiveViewOverrides(\n\tview: View,\n\tactiveViewOverrides?: ActiveViewOverrides,\n\tdefaultView?: View\n): View {\n\tif ( ! activeViewOverrides ) {\n\t\treturn view;\n\t}\n\n\tlet result = view;\n\n\t// Strip scalar keys managed by overrides\n\tfor ( const key of SCALAR_VALUES ) {\n\t\tif ( key in activeViewOverrides ) {\n\t\t\tconst { [ key ]: _, ...rest } = result;\n\t\t\tresult = rest as View;\n\t\t}\n\t}\n\n\t// Strip managed filters\n\tif (\n\t\tactiveViewOverrides.filters &&\n\t\tactiveViewOverrides.filters.length > 0\n\t) {\n\t\tconst activeFields = new Set(\n\t\t\tactiveViewOverrides.filters.map( ( f ) => f.field )\n\t\t);\n\t\tresult = {\n\t\t\t...result,\n\t\t\tfilters: ( view.filters ?? [] ).filter(\n\t\t\t\t( f: Filter ) => ! activeFields.has( f.field )\n\t\t\t),\n\t\t};\n\t}\n\n\t// Strip sort if it matches the override (restore to default)\n\tif (\n\t\tactiveViewOverrides.sort &&\n\t\tview.sort?.field === activeViewOverrides.sort.field &&\n\t\tview.sort?.direction === activeViewOverrides.sort.direction\n\t) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tsort: defaultView?.sort,\n\t\t};\n\t}\n\n\t// Strip layout keys managed by overrides\n\tif ( activeViewOverrides.layout && 'layout' in result && result.layout ) {\n\t\tconst layout = { ...result.layout } as Record< string, unknown >;\n\t\tfor ( const key of Object.keys( activeViewOverrides.layout ) ) {\n\t\t\tdelete layout[ key ];\n\t\t}\n\t\tresult = {\n\t\t\t...result,\n\t\t\tlayout: Object.keys( layout ).length > 0 ? layout : undefined,\n\t\t} as View;\n\t}\n\n\t// Strip groupBy managed by overrides\n\tif ( activeViewOverrides.groupBy && 'groupBy' in result ) {\n\t\tconst { groupBy: _, ...rest } = result;\n\t\tresult = rest as View;\n\t}\n\n\treturn result;\n}\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,IAAM,gBAAgB;AAAA,EACrB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AAYO,SAAS,yBACf,MACA,qBACA,aACO;AACP,MAAK,CAAE,qBAAsB;AAC5B,WAAO;AAAA,EACR;AAEA,MAAI,SAAS;AAGb,aAAY,OAAO,eAAgB;AAClC,QAAK,OAAO,qBAAsB;AACjC,eAAS,EAAE,GAAG,QAAQ,CAAE,GAAI,GAAG,oBAAqB,G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xB,oBAAoB,QAAQ,IAAK,CAAE,MAAO,EAAE,KAAM;AAAA,IACnD;AACA,aAAS;AAAA,MACR,GAAG;AAAA,MACH,UAAW,KAAK,WAAW,CAAC,GAAI;AAAA,QAC/B,CAAE,MAAe,CAAE,aAAa,IAAK,EAAE,KAAM;AAAA,MAC9C;AAAA,IACD;AAAA,EACD;AAGA,MACC,oBAAoB,QACpB,KAAK,MAAM,UAAU,oBAAoB,KAAK,SAC9C,KAAK,MAAM,cAAc,oBAAoB,KAAK,WACjD;AACD,aAAS;AAAA,MACR,GAAG;AAAA,MACH,MAAM,aAAa;AAAA,IACpB;AAAA,EACD;AAGA,MAAK,oBAAoB,UAAU,YAAY,UAAU,OAAO,QAAS;AACxE,UAAM,SAAS,EAAE,GAAG,OAAO,OAAO;AAClC,eAAY,OAAO,OAAO,KAAM,oBAAoB,MAAO,GAAI;AAC9D,aAAO,OAAQ,GAAI;AAAA,IACpB;AACA,aAAS;AAAA,MACR,GAAG;AAAA,MACH,QAAQ,OAAO,KAAM,MAAO,EAAE,SAAS,IAAI,SAAS;AAAA,IACrD;AAAA,EACD;AAGA,MAAK,oBAAoB,WAAW,aAAa,QAAS;AACzD,UAAM,EAAE,SAAS,GAAG,GAAG,KAAK,IAAI;AAChC,aAAS;AAAA,EACV;AAEA,SAAO;AACR;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -28,13 +28,18 @@ var import_core_data = require("@wordpress/core-data");
|
|
|
28
28
|
var import_lock_unlock = require("./lock-unlock.cjs");
|
|
29
29
|
function useViewConfig({
|
|
30
30
|
kind,
|
|
31
|
-
name
|
|
31
|
+
name,
|
|
32
|
+
fields
|
|
32
33
|
}) {
|
|
34
|
+
const fieldList = Array.isArray(fields) ? fields : fields?.split(",");
|
|
35
|
+
const fieldsKey = fieldList ? [...fieldList].sort().join(",") : void 0;
|
|
33
36
|
return (0, import_data.useSelect)(
|
|
34
37
|
(select) => {
|
|
35
|
-
return (0, import_lock_unlock.unlock)(select(import_core_data.store)).getViewConfig(kind, name
|
|
38
|
+
return (0, import_lock_unlock.unlock)(select(import_core_data.store)).getViewConfig(kind, name, {
|
|
39
|
+
fields: fieldsKey
|
|
40
|
+
});
|
|
36
41
|
},
|
|
37
|
-
[kind, name]
|
|
42
|
+
[kind, name, fieldsKey]
|
|
38
43
|
);
|
|
39
44
|
}
|
|
40
45
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/use-view-config.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { View, SupportedLayouts, Form } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from './lock-unlock';\n\n/**\n * A hook that retrieves the view configuration for a given entity\n * from the core data store.\n *\n * @param {Object}
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA0B;AAC1B,uBAAmC;AAMnC,yBAAuB;
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { View, SupportedLayouts, Form } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from './lock-unlock';\n\n/**\n * A hook that retrieves the view configuration for a given entity\n * from the core data store.\n *\n * @param {Object} params\n * @param {string} params.kind The kind of the entity.\n * @param {string} params.name The name of the entity.\n * @param {?(string|string[])} [params.fields] Subset of top-level config\n * properties to request, as an array\n * or a comma-separated string\n * (mapped to the REST API `_fields`\n * parameter). When omitted, the full\n * config is requested.\n * @return {Object} An object containing the `default_view`, `default_layouts`, `view_list`, and `form` configuration for the entity.\n */\nexport function useViewConfig( {\n\tkind,\n\tname,\n\tfields,\n}: {\n\tkind: string;\n\tname: string;\n\tfields?: string | string[];\n} ): {\n\tdefault_view: View | undefined;\n\tdefault_layouts: SupportedLayouts | undefined;\n\tview_list: Array< any > | undefined;\n\tform: Form | undefined;\n} {\n\t// Sort fields so the cache key is independent of the order callers list\n\t// them; `['title','author']` and `['author','title']` request the same data.\n\tconst fieldList = Array.isArray( fields ) ? fields : fields?.split( ',' );\n\tconst fieldsKey = fieldList\n\t\t? [ ...fieldList ].sort().join( ',' )\n\t\t: undefined;\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\treturn unlock( select( coreStore ) ).getViewConfig( kind, name, {\n\t\t\t\tfields: fieldsKey,\n\t\t\t} );\n\t\t},\n\t\t[ kind, name, fieldsKey ]\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA0B;AAC1B,uBAAmC;AAMnC,yBAAuB;AAiBhB,SAAS,cAAe;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AACD,GASE;AAGD,QAAM,YAAY,MAAM,QAAS,MAAO,IAAI,SAAS,QAAQ,MAAO,GAAI;AACxE,QAAM,YAAY,YACf,CAAE,GAAG,SAAU,EAAE,KAAK,EAAE,KAAM,GAAI,IAClC;AACH,aAAO;AAAA,IACN,CAAE,WAAY;AACb,iBAAO,2BAAQ,OAAQ,iBAAAA,KAAU,CAAE,EAAE,cAAe,MAAM,MAAM;AAAA,QAC/D,QAAQ;AAAA,MACT,CAAE;AAAA,IACH;AAAA,IACA,CAAE,MAAM,MAAM,SAAU;AAAA,EACzB;AACD;",
|
|
6
6
|
"names": ["coreStore"]
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/filter-utils.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport type { View, Filter } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport type { ActiveViewOverrides } from './types';\n\nconst SCALAR_VALUES = [\n\t'titleField',\n\t'mediaField',\n\t'descriptionField',\n\t'showTitle',\n\t'showMedia',\n\t'showDescription',\n\t'showLevels',\n\t'infiniteScrollEnabled',\n] as const;\n\n/**\n * Merges activeViewOverrides into a view.\n * Filters: Active filters take precedence; same-field filters are replaced.\n * Sort: Active sort is applied only if current sort matches the default.\n *\n * @param view The view to merge overrides into.\n * @param activeViewOverrides The tab-specific overrides to apply.\n * @param defaultView The default view configuration.\n * @return A new view with merged overrides, or the original view if no overrides.\n */\nexport function mergeActiveViewOverrides(\n\tview: View,\n\tactiveViewOverrides?: ActiveViewOverrides,\n\tdefaultView?: View\n): View {\n\tif ( ! activeViewOverrides ) {\n\t\treturn view;\n\t}\n\n\tlet result = view;\n\n\t// Merge scalar overrides
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport type { View, Filter } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport type { ActiveViewOverrides } from './types';\n\nconst SCALAR_VALUES = [\n\t'titleField',\n\t'mediaField',\n\t'descriptionField',\n\t'showTitle',\n\t'showMedia',\n\t'showDescription',\n\t'showLevels',\n\t'infiniteScrollEnabled',\n] as const;\n\n/**\n * Merges activeViewOverrides into a view.\n * Filters: Active filters take precedence; same-field filters are replaced.\n * Sort: Active sort is applied only if current sort matches the default.\n *\n * @param view The view to merge overrides into.\n * @param activeViewOverrides The tab-specific overrides to apply.\n * @param defaultView The default view configuration.\n * @return A new view with merged overrides, or the original view if no overrides.\n */\nexport function mergeActiveViewOverrides(\n\tview: View,\n\tactiveViewOverrides?: ActiveViewOverrides,\n\tdefaultView?: View\n): View {\n\tif ( ! activeViewOverrides ) {\n\t\treturn view;\n\t}\n\n\tlet result = view;\n\n\t// Merge scalar overrides — always win over persisted values\n\tfor ( const key of SCALAR_VALUES ) {\n\t\tif ( key in activeViewOverrides ) {\n\t\t\tresult = { ...result, [ key ]: activeViewOverrides[ key ] };\n\t\t}\n\t}\n\n\t// Merge filters\n\tif (\n\t\tactiveViewOverrides.filters &&\n\t\tactiveViewOverrides.filters.length > 0\n\t) {\n\t\tconst activeFields = new Set(\n\t\t\tactiveViewOverrides.filters.map( ( f ) => f.field )\n\t\t);\n\t\tconst preserved = ( view.filters ?? [] ).filter(\n\t\t\t( f: Filter ) => ! activeFields.has( f.field )\n\t\t);\n\t\tresult = {\n\t\t\t...result,\n\t\t\tfilters: [ ...preserved, ...activeViewOverrides.filters ],\n\t\t};\n\t}\n\n\t// Merge sort - only apply if the current sort matches the default\n\tif ( activeViewOverrides.sort ) {\n\t\tconst isDefaultSort =\n\t\t\tdefaultView &&\n\t\t\tview.sort?.field === defaultView.sort?.field &&\n\t\t\tview.sort?.direction === defaultView.sort?.direction;\n\n\t\tif ( isDefaultSort ) {\n\t\t\tresult = {\n\t\t\t\t...result,\n\t\t\t\tsort: activeViewOverrides.sort,\n\t\t\t};\n\t\t}\n\t}\n\n\t// Merge layout — shallow merge, override keys always win\n\tif ( activeViewOverrides.layout ) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tlayout: {\n\t\t\t\t...( result as any ).layout,\n\t\t\t\t...activeViewOverrides.layout,\n\t\t\t},\n\t\t} as View;\n\t}\n\n\t// Merge groupBy — full replacement, override always wins\n\tif ( activeViewOverrides.groupBy ) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tgroupBy: activeViewOverrides.groupBy,\n\t\t};\n\t}\n\n\treturn result;\n}\n\n/**\n * Strips overrides before persisting.\n * Filters: Removes filters on fields managed by activeViewOverrides.\n * Sort: If sort matches the override, restores the default sort.\n *\n * @param view The view to strip overrides from.\n * @param activeViewOverrides The tab-specific override definitions.\n * @param defaultView The default view configuration.\n * @return A new view with overrides stripped, or the original view if no overrides.\n */\nexport function stripActiveViewOverrides(\n\tview: View,\n\tactiveViewOverrides?: ActiveViewOverrides,\n\tdefaultView?: View\n): View {\n\tif ( ! activeViewOverrides ) {\n\t\treturn view;\n\t}\n\n\tlet result = view;\n\n\t// Strip scalar keys managed by overrides\n\tfor ( const key of SCALAR_VALUES ) {\n\t\tif ( key in activeViewOverrides ) {\n\t\t\tconst { [ key ]: _, ...rest } = result;\n\t\t\tresult = rest as View;\n\t\t}\n\t}\n\n\t// Strip managed filters\n\tif (\n\t\tactiveViewOverrides.filters &&\n\t\tactiveViewOverrides.filters.length > 0\n\t) {\n\t\tconst activeFields = new Set(\n\t\t\tactiveViewOverrides.filters.map( ( f ) => f.field )\n\t\t);\n\t\tresult = {\n\t\t\t...result,\n\t\t\tfilters: ( view.filters ?? [] ).filter(\n\t\t\t\t( f: Filter ) => ! activeFields.has( f.field )\n\t\t\t),\n\t\t};\n\t}\n\n\t// Strip sort if it matches the override (restore to default)\n\tif (\n\t\tactiveViewOverrides.sort &&\n\t\tview.sort?.field === activeViewOverrides.sort.field &&\n\t\tview.sort?.direction === activeViewOverrides.sort.direction\n\t) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tsort: defaultView?.sort,\n\t\t};\n\t}\n\n\t// Strip layout keys managed by overrides\n\tif ( activeViewOverrides.layout && 'layout' in result && result.layout ) {\n\t\tconst layout = { ...result.layout } as Record< string, unknown >;\n\t\tfor ( const key of Object.keys( activeViewOverrides.layout ) ) {\n\t\t\tdelete layout[ key ];\n\t\t}\n\t\tresult = {\n\t\t\t...result,\n\t\t\tlayout: Object.keys( layout ).length > 0 ? layout : undefined,\n\t\t} as View;\n\t}\n\n\t// Strip groupBy managed by overrides\n\tif ( activeViewOverrides.groupBy && 'groupBy' in result ) {\n\t\tconst { groupBy: _, ...rest } = result;\n\t\tresult = rest as View;\n\t}\n\n\treturn result;\n}\n"],
|
|
5
5
|
"mappings": ";AAUA,IAAM,gBAAgB;AAAA,EACrB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AAYO,SAAS,yBACf,MACA,qBACA,aACO;AACP,MAAK,CAAE,qBAAsB;AAC5B,WAAO;AAAA,EACR;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AAK,IAAI;AAChC,eAAS;AAAA,IACV;AAAA,EACD;AAGA,MACC,oBAAoB,WACpB,oBAAoB,QAAQ,SAAS,GACpC;AACD,UAAM,eAAe,IAAI;AAAA,MACxB,oBAAoB,QAAQ,IAAK,CAAE,MAAO,EAAE,KAAM;AAAA,IACnD;AACA,aAAS;AAAA,MACR,GAAG;AAAA,MACH,UAAW,KAAK,WAAW,CAAC,GAAI;AAAA,QAC/B,CAAE,MAAe,CAAE,aAAa,IAAK,EAAE,KAAM;AAAA,MAC9C;AAAA,IACD;AAAA,EACD;AAGA,MACC,oBAAoB,QACpB,KAAK,MAAM,UAAU,oBAAoB,KAAK,SAC9C,KAAK,MAAM,cAAc,oBAAoB,KAAK,WACjD;AACD,aAAS;AAAA,MACR,GAAG;AAAA,MACH,MAAM,aAAa;AAAA,IACpB;AAAA,EACD;AAGA,MAAK,oBAAoB,UAAU,YAAY,UAAU,OAAO,QAAS;AACxE,UAAM,SAAS,EAAE,GAAG,OAAO,OAAO;AAClC,eAAY,OAAO,OAAO,KAAM,oBAAoB,MAAO,GAAI;AAC9D,aAAO,OAAQ,GAAI;AAAA,IACpB;AACA,aAAS;AAAA,MACR,GAAG;AAAA,MACH,QAAQ,OAAO,KAAM,MAAO,EAAE,SAAS,IAAI,SAAS;AAAA,IACrD;AAAA,EACD;AAGA,MAAK,oBAAoB,WAAW,aAAa,QAAS;AACzD,UAAM,EAAE,SAAS,GAAG,GAAG,KAAK,IAAI;AAChC,aAAS;AAAA,EACV;AAEA,SAAO;AACR;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -4,13 +4,18 @@ import { store as coreStore } from "@wordpress/core-data";
|
|
|
4
4
|
import { unlock } from "./lock-unlock.mjs";
|
|
5
5
|
function useViewConfig({
|
|
6
6
|
kind,
|
|
7
|
-
name
|
|
7
|
+
name,
|
|
8
|
+
fields
|
|
8
9
|
}) {
|
|
10
|
+
const fieldList = Array.isArray(fields) ? fields : fields?.split(",");
|
|
11
|
+
const fieldsKey = fieldList ? [...fieldList].sort().join(",") : void 0;
|
|
9
12
|
return useSelect(
|
|
10
13
|
(select) => {
|
|
11
|
-
return unlock(select(coreStore)).getViewConfig(kind, name
|
|
14
|
+
return unlock(select(coreStore)).getViewConfig(kind, name, {
|
|
15
|
+
fields: fieldsKey
|
|
16
|
+
});
|
|
12
17
|
},
|
|
13
|
-
[kind, name]
|
|
18
|
+
[kind, name, fieldsKey]
|
|
14
19
|
);
|
|
15
20
|
}
|
|
16
21
|
export {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/use-view-config.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { View, SupportedLayouts, Form } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from './lock-unlock';\n\n/**\n * A hook that retrieves the view configuration for a given entity\n * from the core data store.\n *\n * @param {Object}
|
|
5
|
-
"mappings": ";AAGA,SAAS,iBAAiB;AAC1B,SAAS,SAAS,iBAAiB;AAMnC,SAAS,cAAc;
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { View, SupportedLayouts, Form } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from './lock-unlock';\n\n/**\n * A hook that retrieves the view configuration for a given entity\n * from the core data store.\n *\n * @param {Object} params\n * @param {string} params.kind The kind of the entity.\n * @param {string} params.name The name of the entity.\n * @param {?(string|string[])} [params.fields] Subset of top-level config\n * properties to request, as an array\n * or a comma-separated string\n * (mapped to the REST API `_fields`\n * parameter). When omitted, the full\n * config is requested.\n * @return {Object} An object containing the `default_view`, `default_layouts`, `view_list`, and `form` configuration for the entity.\n */\nexport function useViewConfig( {\n\tkind,\n\tname,\n\tfields,\n}: {\n\tkind: string;\n\tname: string;\n\tfields?: string | string[];\n} ): {\n\tdefault_view: View | undefined;\n\tdefault_layouts: SupportedLayouts | undefined;\n\tview_list: Array< any > | undefined;\n\tform: Form | undefined;\n} {\n\t// Sort fields so the cache key is independent of the order callers list\n\t// them; `['title','author']` and `['author','title']` request the same data.\n\tconst fieldList = Array.isArray( fields ) ? fields : fields?.split( ',' );\n\tconst fieldsKey = fieldList\n\t\t? [ ...fieldList ].sort().join( ',' )\n\t\t: undefined;\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\treturn unlock( select( coreStore ) ).getViewConfig( kind, name, {\n\t\t\t\tfields: fieldsKey,\n\t\t\t} );\n\t\t},\n\t\t[ kind, name, fieldsKey ]\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,iBAAiB;AAC1B,SAAS,SAAS,iBAAiB;AAMnC,SAAS,cAAc;AAiBhB,SAAS,cAAe;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AACD,GASE;AAGD,QAAM,YAAY,MAAM,QAAS,MAAO,IAAI,SAAS,QAAQ,MAAO,GAAI;AACxE,QAAM,YAAY,YACf,CAAE,GAAG,SAAU,EAAE,KAAK,EAAE,KAAM,GAAI,IAClC;AACH,SAAO;AAAA,IACN,CAAE,WAAY;AACb,aAAO,OAAQ,OAAQ,SAAU,CAAE,EAAE,cAAe,MAAM,MAAM;AAAA,QAC/D,QAAQ;AAAA,MACT,CAAE;AAAA,IACH;AAAA,IACA,CAAE,MAAM,MAAM,SAAU;AAAA,EACzB;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -3,18 +3,25 @@ import type { View, SupportedLayouts, Form } from '@wordpress/dataviews';
|
|
|
3
3
|
* A hook that retrieves the view configuration for a given entity
|
|
4
4
|
* from the core data store.
|
|
5
5
|
*
|
|
6
|
-
* @param {Object}
|
|
7
|
-
* @param {string}
|
|
8
|
-
* @param {string}
|
|
9
|
-
* @
|
|
6
|
+
* @param {Object} params
|
|
7
|
+
* @param {string} params.kind The kind of the entity.
|
|
8
|
+
* @param {string} params.name The name of the entity.
|
|
9
|
+
* @param {?(string|string[])} [params.fields] Subset of top-level config
|
|
10
|
+
* properties to request, as an array
|
|
11
|
+
* or a comma-separated string
|
|
12
|
+
* (mapped to the REST API `_fields`
|
|
13
|
+
* parameter). When omitted, the full
|
|
14
|
+
* config is requested.
|
|
15
|
+
* @return {Object} An object containing the `default_view`, `default_layouts`, `view_list`, and `form` configuration for the entity.
|
|
10
16
|
*/
|
|
11
|
-
export declare function useViewConfig({ kind, name }: {
|
|
17
|
+
export declare function useViewConfig({ kind, name, fields }: {
|
|
12
18
|
kind: string;
|
|
13
19
|
name: string;
|
|
20
|
+
fields?: string | string[];
|
|
14
21
|
}): {
|
|
15
|
-
default_view: View;
|
|
16
|
-
default_layouts: SupportedLayouts;
|
|
17
|
-
view_list: Array<any
|
|
22
|
+
default_view: View | undefined;
|
|
23
|
+
default_layouts: SupportedLayouts | undefined;
|
|
24
|
+
view_list: Array<any> | undefined;
|
|
18
25
|
form: Form | undefined;
|
|
19
26
|
};
|
|
20
27
|
//# sourceMappingURL=use-view-config.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-view-config.d.ts","sourceRoot":"","sources":["../src/use-view-config.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,IAAI,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAOzE
|
|
1
|
+
{"version":3,"file":"use-view-config.d.ts","sourceRoot":"","sources":["../src/use-view-config.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,IAAI,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAOzE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAAE,EAC9B,IAAI,EACJ,IAAI,EACJ,MAAM,EACN,EAAE;IACF,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC3B,GAAI;IACJ,YAAY,EAAE,IAAI,GAAG,SAAS,CAAC;IAC/B,eAAe,EAAE,gBAAgB,GAAG,SAAS,CAAC;IAC9C,SAAS,EAAE,KAAK,CAAE,GAAG,CAAE,GAAG,SAAS,CAAC;IACpC,IAAI,EAAE,IAAI,GAAG,SAAS,CAAC;CACvB,CAeA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/views",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.0",
|
|
4
4
|
"description": "View persistence and management for WordPress DataViews.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -41,16 +41,16 @@
|
|
|
41
41
|
},
|
|
42
42
|
"types": "build-types",
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@wordpress/core-data": "^7.
|
|
45
|
-
"@wordpress/data": "^10.
|
|
46
|
-
"@wordpress/dataviews": "^
|
|
47
|
-
"@wordpress/element": "^8.0
|
|
48
|
-
"@wordpress/preferences": "^4.
|
|
49
|
-
"@wordpress/private-apis": "^1.
|
|
44
|
+
"@wordpress/core-data": "^7.49.0",
|
|
45
|
+
"@wordpress/data": "^10.49.0",
|
|
46
|
+
"@wordpress/dataviews": "^17.0.0",
|
|
47
|
+
"@wordpress/element": "^8.1.0",
|
|
48
|
+
"@wordpress/preferences": "^4.49.0",
|
|
49
|
+
"@wordpress/private-apis": "^1.49.0",
|
|
50
50
|
"dequal": "^2.0.3"
|
|
51
51
|
},
|
|
52
52
|
"publishConfig": {
|
|
53
53
|
"access": "public"
|
|
54
54
|
},
|
|
55
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "0e7112a4f4fde4ea15bd9060489b8f6fe11eb6ca"
|
|
56
56
|
}
|
package/src/use-view-config.ts
CHANGED
|
@@ -14,27 +14,43 @@ import { unlock } from './lock-unlock';
|
|
|
14
14
|
* A hook that retrieves the view configuration for a given entity
|
|
15
15
|
* from the core data store.
|
|
16
16
|
*
|
|
17
|
-
* @param {Object}
|
|
18
|
-
* @param {string}
|
|
19
|
-
* @param {string}
|
|
20
|
-
* @
|
|
17
|
+
* @param {Object} params
|
|
18
|
+
* @param {string} params.kind The kind of the entity.
|
|
19
|
+
* @param {string} params.name The name of the entity.
|
|
20
|
+
* @param {?(string|string[])} [params.fields] Subset of top-level config
|
|
21
|
+
* properties to request, as an array
|
|
22
|
+
* or a comma-separated string
|
|
23
|
+
* (mapped to the REST API `_fields`
|
|
24
|
+
* parameter). When omitted, the full
|
|
25
|
+
* config is requested.
|
|
26
|
+
* @return {Object} An object containing the `default_view`, `default_layouts`, `view_list`, and `form` configuration for the entity.
|
|
21
27
|
*/
|
|
22
28
|
export function useViewConfig( {
|
|
23
29
|
kind,
|
|
24
30
|
name,
|
|
31
|
+
fields,
|
|
25
32
|
}: {
|
|
26
33
|
kind: string;
|
|
27
34
|
name: string;
|
|
35
|
+
fields?: string | string[];
|
|
28
36
|
} ): {
|
|
29
|
-
default_view: View;
|
|
30
|
-
default_layouts: SupportedLayouts;
|
|
31
|
-
view_list: Array< any
|
|
37
|
+
default_view: View | undefined;
|
|
38
|
+
default_layouts: SupportedLayouts | undefined;
|
|
39
|
+
view_list: Array< any > | undefined;
|
|
32
40
|
form: Form | undefined;
|
|
33
41
|
} {
|
|
42
|
+
// Sort fields so the cache key is independent of the order callers list
|
|
43
|
+
// them; `['title','author']` and `['author','title']` request the same data.
|
|
44
|
+
const fieldList = Array.isArray( fields ) ? fields : fields?.split( ',' );
|
|
45
|
+
const fieldsKey = fieldList
|
|
46
|
+
? [ ...fieldList ].sort().join( ',' )
|
|
47
|
+
: undefined;
|
|
34
48
|
return useSelect(
|
|
35
49
|
( select ) => {
|
|
36
|
-
return unlock( select( coreStore ) ).getViewConfig( kind, name
|
|
50
|
+
return unlock( select( coreStore ) ).getViewConfig( kind, name, {
|
|
51
|
+
fields: fieldsKey,
|
|
52
|
+
} );
|
|
37
53
|
},
|
|
38
|
-
[ kind, name ]
|
|
54
|
+
[ kind, name, fieldsKey ]
|
|
39
55
|
);
|
|
40
56
|
}
|