@wordpress/fields 0.33.1-next.v.202603102151.0 → 0.34.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.
Files changed (29) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/fields/parent/parent-edit.cjs +4 -10
  3. package/build/fields/parent/parent-edit.cjs.map +2 -2
  4. package/build/fields/template/hooks.cjs +35 -2
  5. package/build/fields/template/hooks.cjs.map +2 -2
  6. package/build/fields/template/template-edit.cjs +64 -12
  7. package/build/fields/template/template-edit.cjs.map +2 -2
  8. package/build/fields/template/template-view.cjs +23 -2
  9. package/build/fields/template/template-view.cjs.map +2 -2
  10. package/build-module/fields/parent/parent-edit.mjs +4 -10
  11. package/build-module/fields/parent/parent-edit.mjs.map +2 -2
  12. package/build-module/fields/template/hooks.mjs +33 -1
  13. package/build-module/fields/template/hooks.mjs.map +2 -2
  14. package/build-module/fields/template/template-edit.mjs +65 -13
  15. package/build-module/fields/template/template-edit.mjs.map +2 -2
  16. package/build-module/fields/template/template-view.mjs +24 -3
  17. package/build-module/fields/template/template-view.mjs.map +2 -2
  18. package/build-types/fields/parent/parent-edit.d.ts.map +1 -1
  19. package/build-types/fields/template/hooks.d.ts +8 -0
  20. package/build-types/fields/template/hooks.d.ts.map +1 -1
  21. package/build-types/fields/template/template-edit.d.ts +1 -1
  22. package/build-types/fields/template/template-edit.d.ts.map +1 -1
  23. package/build-types/fields/template/template-view.d.ts +1 -1
  24. package/build-types/fields/template/template-view.d.ts.map +1 -1
  25. package/package.json +26 -26
  26. package/src/fields/parent/parent-edit.tsx +4 -10
  27. package/src/fields/template/hooks.ts +48 -0
  28. package/src/fields/template/template-edit.tsx +80 -23
  29. package/src/fields/template/template-view.tsx +33 -3
@@ -5,16 +5,56 @@ import { SelectControl } from "@wordpress/components";
5
5
  import { useSelect } from "@wordpress/data";
6
6
  import { __ } from "@wordpress/i18n";
7
7
  import { getItemTitle } from "../../actions/utils.mjs";
8
- import { useDefaultTemplateLabel } from "./hooks.mjs";
8
+ import { useDefaultTemplateLabel, useTemplateFieldMode } from "./hooks.mjs";
9
9
  import { unlock } from "../../lock-unlock.mjs";
10
10
  import { jsx } from "react/jsx-runtime";
11
11
  var EMPTY_ARRAY = [];
12
- var TemplateEdit = ({
12
+ function ClassicTemplateEdit({
13
13
  data,
14
14
  field,
15
15
  onChange
16
- }) => {
17
- const { id } = field;
16
+ }) {
17
+ const postId = typeof data.id === "number" ? data.id : parseInt(data.id, 10);
18
+ const value = field.getValue({ item: data });
19
+ const options = useMemo(
20
+ () => Object.entries(
21
+ data?.available_templates ?? {}
22
+ ).map(([templateSlug, title]) => ({
23
+ label: title,
24
+ value: templateSlug
25
+ })),
26
+ [data]
27
+ );
28
+ const canSwitchTemplate = useSelect(
29
+ (select) => {
30
+ const { getHomePage, getPostsPageId } = unlock(
31
+ select(coreStore)
32
+ );
33
+ const singlePostId = String(postId);
34
+ const isPostsPage = getPostsPageId() === singlePostId;
35
+ const isFrontPage = data.type === "page" && getHomePage()?.postId === singlePostId;
36
+ return !isPostsPage && !isFrontPage;
37
+ },
38
+ [postId, data.type]
39
+ );
40
+ return /* @__PURE__ */ jsx(
41
+ SelectControl,
42
+ {
43
+ __next40pxDefaultSize: true,
44
+ label: __("Template"),
45
+ hideLabelFromVision: true,
46
+ value,
47
+ options,
48
+ onChange,
49
+ disabled: !canSwitchTemplate
50
+ }
51
+ );
52
+ }
53
+ function BlockThemeTemplateEdit({
54
+ data,
55
+ field,
56
+ onChange
57
+ }) {
18
58
  const postType = data.type;
19
59
  const postId = typeof data.id === "number" ? data.id : parseInt(data.id, 10);
20
60
  const slug = data.slug;
@@ -32,8 +72,8 @@ var TemplateEdit = ({
32
72
  select(coreStore)
33
73
  );
34
74
  const singlePostId = String(postId);
35
- const isPostsPage = singlePostId !== void 0 && getPostsPageId() === singlePostId;
36
- const isFrontPage = singlePostId !== void 0 && postType === "page" && getHomePage()?.postId === singlePostId;
75
+ const isPostsPage = getPostsPageId() === singlePostId;
76
+ const isFrontPage = postType === "page" && getHomePage()?.postId === singlePostId;
37
77
  return {
38
78
  templates: allTemplates,
39
79
  canSwitchTemplate: !isPostsPage && !isFrontPage
@@ -47,12 +87,6 @@ var TemplateEdit = ({
47
87
  slug
48
88
  );
49
89
  const value = field.getValue({ item: data });
50
- const onChangeControl = useCallback(
51
- (newValue) => onChange({
52
- [id]: newValue
53
- }),
54
- [id, onChange]
55
- );
56
90
  const options = useMemo(() => {
57
91
  const templateOptions = templates.map((template) => ({
58
92
  label: getItemTitle(template),
@@ -71,10 +105,28 @@ var TemplateEdit = ({
71
105
  hideLabelFromVision: true,
72
106
  value,
73
107
  options,
74
- onChange: onChangeControl,
108
+ onChange,
75
109
  disabled: !canSwitchTemplate
76
110
  }
77
111
  );
112
+ }
113
+ var TemplateEdit = ({
114
+ data,
115
+ field,
116
+ onChange
117
+ }) => {
118
+ const onChangeControl = useCallback(
119
+ (newValue) => onChange({
120
+ [field.id]: newValue
121
+ }),
122
+ [field.id, onChange]
123
+ );
124
+ const mode = useTemplateFieldMode(data);
125
+ if (!mode || !["block-theme", "classic"].includes(mode)) {
126
+ return null;
127
+ }
128
+ const Edit = mode === "classic" ? ClassicTemplateEdit : BlockThemeTemplateEdit;
129
+ return /* @__PURE__ */ jsx(Edit, { data, field, onChange: onChangeControl });
78
130
  };
79
131
  export {
80
132
  TemplateEdit
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/fields/template/template-edit.tsx"],
4
- "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo } from '@wordpress/element';\nimport type { WpTemplate } from '@wordpress/core-data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { DataFormControlProps } from '@wordpress/dataviews';\nimport { SelectControl } from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { getItemTitle } from '../../actions/utils';\nimport type { BasePost } from '../../types';\nimport { useDefaultTemplateLabel } from './hooks';\nimport { unlock } from '../../lock-unlock';\n\nconst EMPTY_ARRAY: [] = [];\n\nexport const TemplateEdit = ( {\n\tdata,\n\tfield,\n\tonChange,\n}: DataFormControlProps< BasePost > ) => {\n\tconst { id } = field;\n\tconst postType = data.type;\n\tconst postId =\n\t\ttypeof data.id === 'number' ? data.id : parseInt( data.id, 10 );\n\tconst slug = data.slug;\n\n\tconst { templates, canSwitchTemplate } = useSelect(\n\t\t( select ) => {\n\t\t\tconst allTemplates =\n\t\t\t\tselect( coreStore ).getEntityRecords< WpTemplate >(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\t{\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t\t\tpost_type: postType,\n\t\t\t\t\t}\n\t\t\t\t) ?? EMPTY_ARRAY;\n\n\t\t\tconst { getHomePage, getPostsPageId } = unlock(\n\t\t\t\tselect( coreStore )\n\t\t\t);\n\t\t\tconst singlePostId = String( postId );\n\t\t\tconst isPostsPage =\n\t\t\t\tsinglePostId !== undefined && getPostsPageId() === singlePostId;\n\t\t\tconst isFrontPage =\n\t\t\t\tsinglePostId !== undefined &&\n\t\t\t\tpostType === 'page' &&\n\t\t\t\tgetHomePage()?.postId === singlePostId;\n\n\t\t\treturn {\n\t\t\t\ttemplates: allTemplates,\n\t\t\t\tcanSwitchTemplate: ! isPostsPage && ! isFrontPage,\n\t\t\t};\n\t\t},\n\t\t[ postId, postType ]\n\t);\n\n\tconst defaultTemplateLabel = useDefaultTemplateLabel(\n\t\tpostType,\n\t\tpostId,\n\t\tslug\n\t);\n\n\tconst value = field.getValue( { item: data } );\n\n\tconst onChangeControl = useCallback(\n\t\t( newValue: string ) =>\n\t\t\tonChange( {\n\t\t\t\t[ id ]: newValue,\n\t\t\t} ),\n\t\t[ id, onChange ]\n\t);\n\n\tconst options = useMemo( () => {\n\t\tconst templateOptions = templates.map( ( template ) => ( {\n\t\t\tlabel: getItemTitle( template ),\n\t\t\tvalue: template.slug,\n\t\t} ) );\n\t\treturn [\n\t\t\t{ label: defaultTemplateLabel, value: '' },\n\t\t\t...templateOptions,\n\t\t];\n\t}, [ templates, defaultTemplateLabel ] );\n\n\treturn (\n\t\t<SelectControl\n\t\t\t__next40pxDefaultSize\n\t\t\tlabel={ __( 'Template' ) }\n\t\t\thideLabelFromVision\n\t\t\tvalue={ value }\n\t\t\toptions={ options }\n\t\t\tonChange={ onChangeControl }\n\t\t\tdisabled={ ! canSwitchTemplate }\n\t\t/>\n\t);\n};\n"],
5
- "mappings": ";AAGA,SAAS,aAAa,eAAe;AAErC,SAAS,SAAS,iBAAiB;AAEnC,SAAS,qBAAqB;AAC9B,SAAS,iBAAiB;AAC1B,SAAS,UAAU;AAKnB,SAAS,oBAAoB;AAE7B,SAAS,+BAA+B;AACxC,SAAS,cAAc;AA0ErB;AAxEF,IAAM,cAAkB,CAAC;AAElB,IAAM,eAAe,CAAE;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AACD,MAAyC;AACxC,QAAM,EAAE,GAAG,IAAI;AACf,QAAM,WAAW,KAAK;AACtB,QAAM,SACL,OAAO,KAAK,OAAO,WAAW,KAAK,KAAK,SAAU,KAAK,IAAI,EAAG;AAC/D,QAAM,OAAO,KAAK;AAElB,QAAM,EAAE,WAAW,kBAAkB,IAAI;AAAA,IACxC,CAAE,WAAY;AACb,YAAM,eACL,OAAQ,SAAU,EAAE;AAAA,QACnB;AAAA,QACA;AAAA,QACA;AAAA,UACC,UAAU;AAAA,UACV,WAAW;AAAA,QACZ;AAAA,MACD,KAAK;AAEN,YAAM,EAAE,aAAa,eAAe,IAAI;AAAA,QACvC,OAAQ,SAAU;AAAA,MACnB;AACA,YAAM,eAAe,OAAQ,MAAO;AACpC,YAAM,cACL,iBAAiB,UAAa,eAAe,MAAM;AACpD,YAAM,cACL,iBAAiB,UACjB,aAAa,UACb,YAAY,GAAG,WAAW;AAE3B,aAAO;AAAA,QACN,WAAW;AAAA,QACX,mBAAmB,CAAE,eAAe,CAAE;AAAA,MACvC;AAAA,IACD;AAAA,IACA,CAAE,QAAQ,QAAS;AAAA,EACpB;AAEA,QAAM,uBAAuB;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAEA,QAAM,QAAQ,MAAM,SAAU,EAAE,MAAM,KAAK,CAAE;AAE7C,QAAM,kBAAkB;AAAA,IACvB,CAAE,aACD,SAAU;AAAA,MACT,CAAE,EAAG,GAAG;AAAA,IACT,CAAE;AAAA,IACH,CAAE,IAAI,QAAS;AAAA,EAChB;AAEA,QAAM,UAAU,QAAS,MAAM;AAC9B,UAAM,kBAAkB,UAAU,IAAK,CAAE,cAAgB;AAAA,MACxD,OAAO,aAAc,QAAS;AAAA,MAC9B,OAAO,SAAS;AAAA,IACjB,EAAI;AACJ,WAAO;AAAA,MACN,EAAE,OAAO,sBAAsB,OAAO,GAAG;AAAA,MACzC,GAAG;AAAA,IACJ;AAAA,EACD,GAAG,CAAE,WAAW,oBAAqB,CAAE;AAEvC,SACC;AAAA,IAAC;AAAA;AAAA,MACA,uBAAqB;AAAA,MACrB,OAAQ,GAAI,UAAW;AAAA,MACvB,qBAAmB;AAAA,MACnB;AAAA,MACA;AAAA,MACA,UAAW;AAAA,MACX,UAAW,CAAE;AAAA;AAAA,EACd;AAEF;",
4
+ "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo } from '@wordpress/element';\nimport type { WpTemplate } from '@wordpress/core-data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { DataFormControlProps } from '@wordpress/dataviews';\nimport { SelectControl } from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { getItemTitle } from '../../actions/utils';\nimport type { BasePost } from '../../types';\nimport { useDefaultTemplateLabel, useTemplateFieldMode } from './hooks';\nimport { unlock } from '../../lock-unlock';\n\ntype TemplateEditComponentProps = Omit<\n\tDataFormControlProps< BasePost >,\n\t'onChange'\n> & {\n\tonChange: ( value: string ) => void;\n};\n\nconst EMPTY_ARRAY: [] = [];\n\nfunction ClassicTemplateEdit( {\n\tdata,\n\tfield,\n\tonChange,\n}: TemplateEditComponentProps ) {\n\tconst postId =\n\t\ttypeof data.id === 'number' ? data.id : parseInt( data.id, 10 );\n\tconst value = field.getValue( { item: data } );\n\tconst options = useMemo(\n\t\t() =>\n\t\t\tObject.entries(\n\t\t\t\t( ( data as Record< string, any > )?.available_templates ??\n\t\t\t\t\t{} ) as Record< string, string >\n\t\t\t).map( ( [ templateSlug, title ] ) => ( {\n\t\t\t\tlabel: title,\n\t\t\t\tvalue: templateSlug,\n\t\t\t} ) ),\n\t\t[ data ]\n\t);\n\tconst canSwitchTemplate = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getHomePage, getPostsPageId } = unlock(\n\t\t\t\tselect( coreStore )\n\t\t\t);\n\t\t\tconst singlePostId = String( postId );\n\t\t\tconst isPostsPage = getPostsPageId() === singlePostId;\n\t\t\tconst isFrontPage =\n\t\t\t\tdata.type === 'page' && getHomePage()?.postId === singlePostId;\n\n\t\t\treturn ! isPostsPage && ! isFrontPage;\n\t\t},\n\t\t[ postId, data.type ]\n\t);\n\treturn (\n\t\t<SelectControl\n\t\t\t__next40pxDefaultSize\n\t\t\tlabel={ __( 'Template' ) }\n\t\t\thideLabelFromVision\n\t\t\tvalue={ value }\n\t\t\toptions={ options }\n\t\t\tonChange={ onChange }\n\t\t\tdisabled={ ! canSwitchTemplate }\n\t\t/>\n\t);\n}\n\nfunction BlockThemeTemplateEdit( {\n\tdata,\n\tfield,\n\tonChange,\n}: TemplateEditComponentProps ) {\n\tconst postType = data.type;\n\tconst postId =\n\t\ttypeof data.id === 'number' ? data.id : parseInt( data.id, 10 );\n\tconst slug = data.slug;\n\tconst { templates, canSwitchTemplate } = useSelect(\n\t\t( select ) => {\n\t\t\tconst allTemplates =\n\t\t\t\tselect( coreStore ).getEntityRecords< WpTemplate >(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\t{\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t\t\tpost_type: postType,\n\t\t\t\t\t}\n\t\t\t\t) ?? EMPTY_ARRAY;\n\n\t\t\tconst { getHomePage, getPostsPageId } = unlock(\n\t\t\t\tselect( coreStore )\n\t\t\t);\n\t\t\tconst singlePostId = String( postId );\n\t\t\tconst isPostsPage = getPostsPageId() === singlePostId;\n\t\t\tconst isFrontPage =\n\t\t\t\tpostType === 'page' && getHomePage()?.postId === singlePostId;\n\n\t\t\treturn {\n\t\t\t\ttemplates: allTemplates,\n\t\t\t\tcanSwitchTemplate: ! isPostsPage && ! isFrontPage,\n\t\t\t};\n\t\t},\n\t\t[ postId, postType ]\n\t);\n\tconst defaultTemplateLabel = useDefaultTemplateLabel(\n\t\tpostType,\n\t\tpostId,\n\t\tslug\n\t);\n\tconst value = field.getValue( { item: data } );\n\tconst options = useMemo( () => {\n\t\tconst templateOptions = templates.map( ( template ) => ( {\n\t\t\tlabel: getItemTitle( template ),\n\t\t\tvalue: template.slug,\n\t\t} ) );\n\t\treturn [\n\t\t\t{ label: defaultTemplateLabel, value: '' },\n\t\t\t...templateOptions,\n\t\t];\n\t}, [ templates, defaultTemplateLabel ] );\n\treturn (\n\t\t<SelectControl\n\t\t\t__next40pxDefaultSize\n\t\t\tlabel={ __( 'Template' ) }\n\t\t\thideLabelFromVision\n\t\t\tvalue={ value }\n\t\t\toptions={ options }\n\t\t\tonChange={ onChange }\n\t\t\tdisabled={ ! canSwitchTemplate }\n\t\t/>\n\t);\n}\n\nexport const TemplateEdit = ( {\n\tdata,\n\tfield,\n\tonChange,\n}: DataFormControlProps< BasePost > ) => {\n\tconst onChangeControl = useCallback(\n\t\t( newValue: string ) =>\n\t\t\tonChange( {\n\t\t\t\t[ field.id ]: newValue,\n\t\t\t} ),\n\t\t[ field.id, onChange ]\n\t);\n\tconst mode = useTemplateFieldMode( data );\n\tif ( ! mode || ! [ 'block-theme', 'classic' ].includes( mode ) ) {\n\t\treturn null;\n\t}\n\tconst Edit =\n\t\tmode === 'classic' ? ClassicTemplateEdit : BlockThemeTemplateEdit;\n\treturn <Edit data={ data } field={ field } onChange={ onChangeControl } />;\n};\n"],
5
+ "mappings": "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",
6
6
  "names": []
7
7
  }
@@ -1,13 +1,23 @@
1
1
  // packages/fields/src/fields/template/template-view.tsx
2
2
  import { useSelect } from "@wordpress/data";
3
3
  import { store as coreStore } from "@wordpress/core-data";
4
+ import { __ } from "@wordpress/i18n";
4
5
  import { getItemTitle } from "../../actions/utils.mjs";
5
- import { useDefaultTemplateLabel } from "./hooks.mjs";
6
+ import { useDefaultTemplateLabel, useTemplateFieldMode } from "./hooks.mjs";
6
7
  import { Fragment, jsx } from "react/jsx-runtime";
7
- var TemplateView = ({
8
+ function ClassicTemplateView({
8
9
  item,
9
10
  field
10
- }) => {
11
+ }) {
12
+ const templateSlug = field.getValue({ item });
13
+ const availableTemplates = item?.available_templates ?? {};
14
+ const classicLabel = templateSlug && availableTemplates[templateSlug] ? availableTemplates[templateSlug] : __("Default template");
15
+ return /* @__PURE__ */ jsx(Fragment, { children: classicLabel });
16
+ }
17
+ function BlockThemeTemplateView({
18
+ item,
19
+ field
20
+ }) {
11
21
  const postType = item.type;
12
22
  const slug = item.slug;
13
23
  const postId = item.id;
@@ -36,6 +46,17 @@ var TemplateView = ({
36
46
  [postType, templateSlug]
37
47
  );
38
48
  return /* @__PURE__ */ jsx(Fragment, { children: templateLabel ?? defaultTemplateLabel });
49
+ }
50
+ var TemplateView = ({
51
+ item,
52
+ field
53
+ }) => {
54
+ const mode = useTemplateFieldMode(item);
55
+ if (!mode || !["block-theme", "classic"].includes(mode)) {
56
+ return null;
57
+ }
58
+ const View = mode === "classic" ? ClassicTemplateView : BlockThemeTemplateView;
59
+ return /* @__PURE__ */ jsx(View, { item, field });
39
60
  };
40
61
  export {
41
62
  TemplateView
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/fields/template/template-view.tsx"],
4
- "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport type { WpTemplate } from '@wordpress/core-data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { DataViewRenderFieldProps } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { getItemTitle } from '../../actions/utils';\nimport type { BasePost } from '../../types';\nimport { useDefaultTemplateLabel } from './hooks';\n\nexport const TemplateView = ( {\n\titem,\n\tfield,\n}: DataViewRenderFieldProps< BasePost > ) => {\n\tconst postType = item.type;\n\tconst slug = item.slug;\n\tconst postId = item.id;\n\tconst templateSlug = field.getValue( { item } );\n\n\tconst defaultTemplateLabel = useDefaultTemplateLabel(\n\t\tpostType,\n\t\tpostId,\n\t\tslug\n\t);\n\n\tconst templateLabel = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! templateSlug ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst allTemplates = select(\n\t\t\t\tcoreStore\n\t\t\t).getEntityRecords< WpTemplate >( 'postType', 'wp_template', {\n\t\t\t\tper_page: -1,\n\t\t\t\tpost_type: postType,\n\t\t\t} );\n\t\t\tconst match = allTemplates?.find(\n\t\t\t\t( t ) => t.slug === templateSlug\n\t\t\t);\n\t\t\treturn match ? getItemTitle( match ) : undefined;\n\t\t},\n\t\t[ postType, templateSlug ]\n\t);\n\n\treturn <>{ templateLabel ?? defaultTemplateLabel }</>;\n};\n"],
5
- "mappings": ";AAGA,SAAS,iBAAiB;AAE1B,SAAS,SAAS,iBAAiB;AAMnC,SAAS,oBAAoB;AAE7B,SAAS,+BAA+B;AAqChC;AAnCD,IAAM,eAAe,CAAE;AAAA,EAC7B;AAAA,EACA;AACD,MAA6C;AAC5C,QAAM,WAAW,KAAK;AACtB,QAAM,OAAO,KAAK;AAClB,QAAM,SAAS,KAAK;AACpB,QAAM,eAAe,MAAM,SAAU,EAAE,KAAK,CAAE;AAE9C,QAAM,uBAAuB;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAEA,QAAM,gBAAgB;AAAA,IACrB,CAAE,WAAY;AACb,UAAK,CAAE,cAAe;AACrB;AAAA,MACD;AAEA,YAAM,eAAe;AAAA,QACpB;AAAA,MACD,EAAE,iBAAgC,YAAY,eAAe;AAAA,QAC5D,UAAU;AAAA,QACV,WAAW;AAAA,MACZ,CAAE;AACF,YAAM,QAAQ,cAAc;AAAA,QAC3B,CAAE,MAAO,EAAE,SAAS;AAAA,MACrB;AACA,aAAO,QAAQ,aAAc,KAAM,IAAI;AAAA,IACxC;AAAA,IACA,CAAE,UAAU,YAAa;AAAA,EAC1B;AAEA,SAAO,gCAAI,2BAAiB,sBAAsB;AACnD;",
4
+ "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport type { WpTemplate } from '@wordpress/core-data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { DataViewRenderFieldProps } from '@wordpress/dataviews';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { getItemTitle } from '../../actions/utils';\nimport type { BasePost } from '../../types';\nimport { useDefaultTemplateLabel, useTemplateFieldMode } from './hooks';\n\nfunction ClassicTemplateView( {\n\titem,\n\tfield,\n}: DataViewRenderFieldProps< BasePost > ) {\n\tconst templateSlug = field.getValue( { item } );\n\tconst availableTemplates = ( ( item as Record< string, any > )\n\t\t?.available_templates ?? {} ) as Record< string, string >;\n\n\tconst classicLabel =\n\t\ttemplateSlug && availableTemplates[ templateSlug ]\n\t\t\t? availableTemplates[ templateSlug ]\n\t\t\t: __( 'Default template' );\n\n\treturn <>{ classicLabel }</>;\n}\n\nfunction BlockThemeTemplateView( {\n\titem,\n\tfield,\n}: DataViewRenderFieldProps< BasePost > ) {\n\tconst postType = item.type;\n\tconst slug = item.slug;\n\tconst postId = item.id;\n\tconst templateSlug = field.getValue( { item } );\n\n\tconst defaultTemplateLabel = useDefaultTemplateLabel(\n\t\tpostType,\n\t\tpostId,\n\t\tslug\n\t);\n\n\tconst templateLabel = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! templateSlug ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst allTemplates = select(\n\t\t\t\tcoreStore\n\t\t\t).getEntityRecords< WpTemplate >( 'postType', 'wp_template', {\n\t\t\t\tper_page: -1,\n\t\t\t\tpost_type: postType,\n\t\t\t} );\n\t\t\tconst match = allTemplates?.find(\n\t\t\t\t( t ) => t.slug === templateSlug\n\t\t\t);\n\t\t\treturn match ? getItemTitle( match ) : undefined;\n\t\t},\n\t\t[ postType, templateSlug ]\n\t);\n\n\treturn <>{ templateLabel ?? defaultTemplateLabel }</>;\n}\n\nexport const TemplateView = ( {\n\titem,\n\tfield,\n}: DataViewRenderFieldProps< BasePost > ) => {\n\tconst mode = useTemplateFieldMode( item );\n\tif ( ! mode || ! [ 'block-theme', 'classic' ].includes( mode ) ) {\n\t\treturn null;\n\t}\n\tconst View =\n\t\tmode === 'classic' ? ClassicTemplateView : BlockThemeTemplateView;\n\treturn <View item={ item } field={ field } />;\n};\n"],
5
+ "mappings": ";AAGA,SAAS,iBAAiB;AAE1B,SAAS,SAAS,iBAAiB;AAEnC,SAAS,UAAU;AAKnB,SAAS,oBAAoB;AAE7B,SAAS,yBAAyB,4BAA4B;AAetD;AAbR,SAAS,oBAAqB;AAAA,EAC7B;AAAA,EACA;AACD,GAA0C;AACzC,QAAM,eAAe,MAAM,SAAU,EAAE,KAAK,CAAE;AAC9C,QAAM,qBAAyB,MAC5B,uBAAuB,CAAC;AAE3B,QAAM,eACL,gBAAgB,mBAAoB,YAAa,IAC9C,mBAAoB,YAAa,IACjC,GAAI,kBAAmB;AAE3B,SAAO,gCAAI,wBAAc;AAC1B;AAEA,SAAS,uBAAwB;AAAA,EAChC;AAAA,EACA;AACD,GAA0C;AACzC,QAAM,WAAW,KAAK;AACtB,QAAM,OAAO,KAAK;AAClB,QAAM,SAAS,KAAK;AACpB,QAAM,eAAe,MAAM,SAAU,EAAE,KAAK,CAAE;AAE9C,QAAM,uBAAuB;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAEA,QAAM,gBAAgB;AAAA,IACrB,CAAE,WAAY;AACb,UAAK,CAAE,cAAe;AACrB;AAAA,MACD;AAEA,YAAM,eAAe;AAAA,QACpB;AAAA,MACD,EAAE,iBAAgC,YAAY,eAAe;AAAA,QAC5D,UAAU;AAAA,QACV,WAAW;AAAA,MACZ,CAAE;AACF,YAAM,QAAQ,cAAc;AAAA,QAC3B,CAAE,MAAO,EAAE,SAAS;AAAA,MACrB;AACA,aAAO,QAAQ,aAAc,KAAM,IAAI;AAAA,IACxC;AAAA,IACA,CAAE,UAAU,YAAa;AAAA,EAC1B;AAEA,SAAO,gCAAI,2BAAiB,sBAAsB;AACnD;AAEO,IAAM,eAAe,CAAE;AAAA,EAC7B;AAAA,EACA;AACD,MAA6C;AAC5C,QAAM,OAAO,qBAAsB,IAAK;AACxC,MAAK,CAAE,QAAQ,CAAE,CAAE,eAAe,SAAU,EAAE,SAAU,IAAK,GAAI;AAChE,WAAO;AAAA,EACR;AACA,QAAM,OACL,SAAS,YAAY,sBAAsB;AAC5C,SAAO,oBAAC,QAAK,MAAc,OAAgB;AAC5C;",
6
6
  "names": []
7
7
  }
@@ -1 +1 @@
1
- {"version":3,"file":"parent-edit.d.ts","sourceRoot":"","sources":["../../../src/fields/parent/parent-edit.tsx"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAMjE;;GAEG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAoE5C,eAAO,MAAM,eAAe,GAAK,MAAM,MAAM,EAAE,aAAa,MAAM,WAYjE,CAAC;AAEF,wBAAgB,oBAAoB,CAAE,EACrC,IAAI,EACJ,eAAe,GACf,EAAE;IACF,IAAI,EAAE,QAAQ,CAAC;IACf,eAAe,EAAE,CAAE,QAAQ,EAAE,MAAM,KAAM,IAAI,CAAC;CAC9C,sCAsKA;AAED,eAAO,MAAM,UAAU,GAAK,4BAIzB,oBAAoB,CAAE,QAAQ,CAAE,gCA2DlC,CAAC"}
1
+ {"version":3,"file":"parent-edit.d.ts","sourceRoot":"","sources":["../../../src/fields/parent/parent-edit.tsx"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAMjE;;GAEG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAoE5C,eAAO,MAAM,eAAe,GAAK,MAAM,MAAM,EAAE,aAAa,MAAM,WAYjE,CAAC;AAEF,wBAAgB,oBAAoB,CAAE,EACrC,IAAI,EACJ,eAAe,GACf,EAAE;IACF,IAAI,EAAE,QAAQ,CAAC;IACf,eAAe,EAAE,CAAE,QAAQ,EAAE,MAAM,KAAM,IAAI,CAAC;CAC9C,sCAgKA;AAED,eAAO,MAAM,UAAU,GAAK,4BAIzB,oBAAoB,CAAE,QAAQ,CAAE,gCA2DlC,CAAC"}
@@ -1,3 +1,11 @@
1
+ import type { BasePost } from '../../types';
2
+ /**
3
+ * Hook that determines the template field rendering mode for a post.
4
+ *
5
+ * @param record The post record.
6
+ * @return 'block-theme' | 'classic' | null
7
+ */
8
+ export declare function useTemplateFieldMode(record: BasePost): 'block-theme' | 'classic' | null;
1
9
  /**
2
10
  * Hook that resolves the human-readable label for the default template
3
11
  * that would apply to a post, given its type, ID and slug.
@@ -1 +1 @@
1
- {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../../src/fields/template/hooks.ts"],"names":[],"mappings":"AAsCA;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACtC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,EACnC,IAAI,EAAE,MAAM,GAAG,SAAS,GACtB,MAAM,CAsER"}
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../../src/fields/template/hooks.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5C;;;;;GAKG;AACH,wBAAgB,oBAAoB,CACnC,MAAM,EAAE,QAAQ,GACd,aAAa,GAAG,SAAS,GAAG,IAAI,CAqClC;AA2BD;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACtC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,EACnC,IAAI,EAAE,MAAM,GAAG,SAAS,GACtB,MAAM,CAsER"}
@@ -1,4 +1,4 @@
1
1
  import type { DataFormControlProps } from '@wordpress/dataviews';
2
2
  import type { BasePost } from '../../types';
3
- export declare const TemplateEdit: ({ data, field, onChange, }: DataFormControlProps<BasePost>) => import("react").JSX.Element;
3
+ export declare const TemplateEdit: ({ data, field, onChange, }: DataFormControlProps<BasePost>) => import("react").JSX.Element | null;
4
4
  //# sourceMappingURL=template-edit.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"template-edit.d.ts","sourceRoot":"","sources":["../../../src/fields/template/template-edit.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AASjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAM5C,eAAO,MAAM,YAAY,GAAK,4BAI3B,oBAAoB,CAAE,QAAQ,CAAE,gCA4ElC,CAAC"}
1
+ {"version":3,"file":"template-edit.d.ts","sourceRoot":"","sources":["../../../src/fields/template/template-edit.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AASjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AA4H5C,eAAO,MAAM,YAAY,GAAK,4BAI3B,oBAAoB,CAAE,QAAQ,CAAE,uCAelC,CAAC"}
@@ -1,4 +1,4 @@
1
1
  import type { DataViewRenderFieldProps } from '@wordpress/dataviews';
2
2
  import type { BasePost } from '../../types';
3
- export declare const TemplateView: ({ item, field, }: DataViewRenderFieldProps<BasePost>) => import("react").JSX.Element;
3
+ export declare const TemplateView: ({ item, field, }: DataViewRenderFieldProps<BasePost>) => import("react").JSX.Element | null;
4
4
  //# sourceMappingURL=template-view.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"template-view.d.ts","sourceRoot":"","sources":["../../../src/fields/template/template-view.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AAMrE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAG5C,eAAO,MAAM,YAAY,GAAK,kBAG3B,wBAAwB,CAAE,QAAQ,CAAE,gCAiCtC,CAAC"}
1
+ {"version":3,"file":"template-view.d.ts","sourceRoot":"","sources":["../../../src/fields/template/template-view.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AAOrE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAyD5C,eAAO,MAAM,YAAY,GAAK,kBAG3B,wBAAwB,CAAE,QAAQ,CAAE,uCAQtC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/fields",
3
- "version": "0.33.1-next.v.202603102151.0+59e17f9ec",
3
+ "version": "0.34.0",
4
4
  "description": "DataViews is a component that provides an API to render datasets using different types of layouts (table, grid, list, etc.).",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -48,30 +48,30 @@
48
48
  ],
49
49
  "dependencies": {
50
50
  "@react-spring/web": "^9.4.5",
51
- "@wordpress/api-fetch": "^7.41.1-next.v.202603102151.0+59e17f9ec",
52
- "@wordpress/base-styles": "^6.17.1-next.v.202603102151.0+59e17f9ec",
53
- "@wordpress/blob": "^4.41.1-next.v.202603102151.0+59e17f9ec",
54
- "@wordpress/blocks": "^15.14.1-next.v.202603102151.0+59e17f9ec",
55
- "@wordpress/components": "^32.4.1-next.v.202603102151.0+59e17f9ec",
56
- "@wordpress/compose": "^7.41.1-next.v.202603102151.0+59e17f9ec",
57
- "@wordpress/core-data": "^7.41.2-next.v.202603102151.0+59e17f9ec",
58
- "@wordpress/data": "^10.41.1-next.v.202603102151.0+59e17f9ec",
59
- "@wordpress/dataviews": "^13.1.1-next.v.202603102151.0+59e17f9ec",
60
- "@wordpress/date": "^5.41.1-next.v.202603102151.0+59e17f9ec",
61
- "@wordpress/element": "^6.41.1-next.v.202603102151.0+59e17f9ec",
62
- "@wordpress/hooks": "^4.41.1-next.v.202603102151.0+59e17f9ec",
63
- "@wordpress/html-entities": "^4.41.1-next.v.202603102151.0+59e17f9ec",
64
- "@wordpress/i18n": "^6.14.1-next.v.202603102151.0+59e17f9ec",
65
- "@wordpress/icons": "^12.0.1-next.v.202603102151.0+59e17f9ec",
66
- "@wordpress/media-utils": "^5.41.1-next.v.202603102151.0+59e17f9ec",
67
- "@wordpress/notices": "^5.41.1-next.v.202603102151.0+59e17f9ec",
68
- "@wordpress/patterns": "^2.41.1-next.v.202603102151.0+59e17f9ec",
69
- "@wordpress/primitives": "^4.41.1-next.v.202603102151.0+59e17f9ec",
70
- "@wordpress/private-apis": "^1.41.1-next.v.202603102151.0+59e17f9ec",
71
- "@wordpress/router": "^1.41.1-next.v.202603102151.0+59e17f9ec",
72
- "@wordpress/url": "^4.41.1-next.v.202603102151.0+59e17f9ec",
73
- "@wordpress/warning": "^3.41.1-next.v.202603102151.0+59e17f9ec",
74
- "@wordpress/wordcount": "^4.41.1-next.v.202603102151.0+59e17f9ec",
51
+ "@wordpress/api-fetch": "^7.42.0",
52
+ "@wordpress/base-styles": "^6.18.0",
53
+ "@wordpress/blob": "^4.42.0",
54
+ "@wordpress/blocks": "^15.15.0",
55
+ "@wordpress/components": "^32.4.0",
56
+ "@wordpress/compose": "^7.42.0",
57
+ "@wordpress/core-data": "^7.42.0",
58
+ "@wordpress/data": "^10.42.0",
59
+ "@wordpress/dataviews": "^13.1.0",
60
+ "@wordpress/date": "^5.42.0",
61
+ "@wordpress/element": "^6.42.0",
62
+ "@wordpress/hooks": "^4.42.0",
63
+ "@wordpress/html-entities": "^4.42.0",
64
+ "@wordpress/i18n": "^6.15.0",
65
+ "@wordpress/icons": "^12.0.0",
66
+ "@wordpress/media-utils": "^5.42.0",
67
+ "@wordpress/notices": "^5.42.0",
68
+ "@wordpress/patterns": "^2.42.0",
69
+ "@wordpress/primitives": "^4.42.0",
70
+ "@wordpress/private-apis": "^1.42.0",
71
+ "@wordpress/router": "^1.42.0",
72
+ "@wordpress/url": "^4.42.0",
73
+ "@wordpress/warning": "^3.42.0",
74
+ "@wordpress/wordcount": "^4.42.0",
75
75
  "change-case": "4.1.2",
76
76
  "client-zip": "^2.4.5",
77
77
  "clsx": "2.1.1",
@@ -83,5 +83,5 @@
83
83
  "publishConfig": {
84
84
  "access": "public"
85
85
  },
86
- "gitHead": "86db21e727d89e8f0dbba9300d2f97fd22b08693"
86
+ "gitHead": "c20787b1778ae64c2db65643b1c236309d68e6ba"
87
87
  }
@@ -114,7 +114,7 @@ export function PageAttributesParent( {
114
114
  data: BasePost;
115
115
  onChangeControl: ( newValue: number ) => void;
116
116
  } ) {
117
- const [ fieldValue, setFieldValue ] = useState< null | string >( null );
117
+ const [ fieldValue, setFieldValue ] = useState< string >( '' );
118
118
 
119
119
  const pageId = data.parent;
120
120
  const postId = data.id;
@@ -145,7 +145,7 @@ export function PageAttributesParent( {
145
145
  orderby: 'menu_order',
146
146
  order: 'asc',
147
147
  _fields: 'id,title,parent',
148
- ...( fieldValue !== null && {
148
+ ...( !! fieldValue && {
149
149
  // Perform a search by relevance when the field is changed.
150
150
  search: fieldValue,
151
151
  orderby: 'relevance',
@@ -192,14 +192,8 @@ export function PageAttributesParent( {
192
192
  ] );
193
193
 
194
194
  const sortedNodes = mappedNodes.sort( ( [ a ], [ b ] ) => {
195
- const priorityA = getItemPriority(
196
- a.rawName,
197
- fieldValue ?? ''
198
- );
199
- const priorityB = getItemPriority(
200
- b.rawName,
201
- fieldValue ?? ''
202
- );
195
+ const priorityA = getItemPriority( a.rawName, fieldValue );
196
+ const priorityB = getItemPriority( b.rawName, fieldValue );
203
197
  return priorityA >= priorityB ? 1 : -1;
204
198
  } );
205
199
 
@@ -10,6 +10,54 @@ import type { WpTemplate } from '@wordpress/core-data';
10
10
  */
11
11
  import { getItemTitle } from '../../actions/utils';
12
12
  import { unlock } from '../../lock-unlock';
13
+ import type { BasePost } from '../../types';
14
+
15
+ /**
16
+ * Hook that determines the template field rendering mode for a post.
17
+ *
18
+ * @param record The post record.
19
+ * @return 'block-theme' | 'classic' | null
20
+ */
21
+ export function useTemplateFieldMode(
22
+ record: BasePost
23
+ ): 'block-theme' | 'classic' | null {
24
+ const postType = record.type;
25
+ const availableTemplates = ( ( record as Record< string, any > )
26
+ ?.available_templates ?? {} ) as Record< string, string >;
27
+ const hasAvailableTemplates = Object.keys( availableTemplates ).length > 0;
28
+ return useSelect(
29
+ ( select ) => {
30
+ const isBlockTheme =
31
+ !! select( coreStore ).getCurrentTheme()?.is_block_theme;
32
+ const postTypeObj = select( coreStore ).getPostType( postType );
33
+ if ( ! postTypeObj?.viewable ) {
34
+ return null;
35
+ }
36
+ const canCreateTemplates =
37
+ isBlockTheme &&
38
+ ( select( coreStore ).canUser( 'create', {
39
+ kind: 'postType',
40
+ name: 'wp_template',
41
+ } ) ??
42
+ false );
43
+ const isVisible = hasAvailableTemplates || canCreateTemplates;
44
+ const canViewTemplates = isVisible
45
+ ? !! select( coreStore ).canUser( 'read', {
46
+ kind: 'postType',
47
+ name: 'wp_template',
48
+ } )
49
+ : false;
50
+ if ( ( ! isBlockTheme || ! canViewTemplates ) && isVisible ) {
51
+ return 'classic';
52
+ }
53
+ if ( isBlockTheme && canViewTemplates ) {
54
+ return 'block-theme';
55
+ }
56
+ return null;
57
+ },
58
+ [ postType, hasAvailableTemplates ]
59
+ );
60
+ }
13
61
 
14
62
  /**
15
63
  * Compute the template slug to look up in the template hierarchy.
@@ -14,22 +14,73 @@ import { __ } from '@wordpress/i18n';
14
14
  */
15
15
  import { getItemTitle } from '../../actions/utils';
16
16
  import type { BasePost } from '../../types';
17
- import { useDefaultTemplateLabel } from './hooks';
17
+ import { useDefaultTemplateLabel, useTemplateFieldMode } from './hooks';
18
18
  import { unlock } from '../../lock-unlock';
19
19
 
20
+ type TemplateEditComponentProps = Omit<
21
+ DataFormControlProps< BasePost >,
22
+ 'onChange'
23
+ > & {
24
+ onChange: ( value: string ) => void;
25
+ };
26
+
20
27
  const EMPTY_ARRAY: [] = [];
21
28
 
22
- export const TemplateEdit = ( {
29
+ function ClassicTemplateEdit( {
23
30
  data,
24
31
  field,
25
32
  onChange,
26
- }: DataFormControlProps< BasePost > ) => {
27
- const { id } = field;
33
+ }: TemplateEditComponentProps ) {
34
+ const postId =
35
+ typeof data.id === 'number' ? data.id : parseInt( data.id, 10 );
36
+ const value = field.getValue( { item: data } );
37
+ const options = useMemo(
38
+ () =>
39
+ Object.entries(
40
+ ( ( data as Record< string, any > )?.available_templates ??
41
+ {} ) as Record< string, string >
42
+ ).map( ( [ templateSlug, title ] ) => ( {
43
+ label: title,
44
+ value: templateSlug,
45
+ } ) ),
46
+ [ data ]
47
+ );
48
+ const canSwitchTemplate = useSelect(
49
+ ( select ) => {
50
+ const { getHomePage, getPostsPageId } = unlock(
51
+ select( coreStore )
52
+ );
53
+ const singlePostId = String( postId );
54
+ const isPostsPage = getPostsPageId() === singlePostId;
55
+ const isFrontPage =
56
+ data.type === 'page' && getHomePage()?.postId === singlePostId;
57
+
58
+ return ! isPostsPage && ! isFrontPage;
59
+ },
60
+ [ postId, data.type ]
61
+ );
62
+ return (
63
+ <SelectControl
64
+ __next40pxDefaultSize
65
+ label={ __( 'Template' ) }
66
+ hideLabelFromVision
67
+ value={ value }
68
+ options={ options }
69
+ onChange={ onChange }
70
+ disabled={ ! canSwitchTemplate }
71
+ />
72
+ );
73
+ }
74
+
75
+ function BlockThemeTemplateEdit( {
76
+ data,
77
+ field,
78
+ onChange,
79
+ }: TemplateEditComponentProps ) {
28
80
  const postType = data.type;
29
81
  const postId =
30
82
  typeof data.id === 'number' ? data.id : parseInt( data.id, 10 );
31
83
  const slug = data.slug;
32
-
33
84
  const { templates, canSwitchTemplate } = useSelect(
34
85
  ( select ) => {
35
86
  const allTemplates =
@@ -46,12 +97,9 @@ export const TemplateEdit = ( {
46
97
  select( coreStore )
47
98
  );
48
99
  const singlePostId = String( postId );
49
- const isPostsPage =
50
- singlePostId !== undefined && getPostsPageId() === singlePostId;
100
+ const isPostsPage = getPostsPageId() === singlePostId;
51
101
  const isFrontPage =
52
- singlePostId !== undefined &&
53
- postType === 'page' &&
54
- getHomePage()?.postId === singlePostId;
102
+ postType === 'page' && getHomePage()?.postId === singlePostId;
55
103
 
56
104
  return {
57
105
  templates: allTemplates,
@@ -60,23 +108,12 @@ export const TemplateEdit = ( {
60
108
  },
61
109
  [ postId, postType ]
62
110
  );
63
-
64
111
  const defaultTemplateLabel = useDefaultTemplateLabel(
65
112
  postType,
66
113
  postId,
67
114
  slug
68
115
  );
69
-
70
116
  const value = field.getValue( { item: data } );
71
-
72
- const onChangeControl = useCallback(
73
- ( newValue: string ) =>
74
- onChange( {
75
- [ id ]: newValue,
76
- } ),
77
- [ id, onChange ]
78
- );
79
-
80
117
  const options = useMemo( () => {
81
118
  const templateOptions = templates.map( ( template ) => ( {
82
119
  label: getItemTitle( template ),
@@ -87,7 +124,6 @@ export const TemplateEdit = ( {
87
124
  ...templateOptions,
88
125
  ];
89
126
  }, [ templates, defaultTemplateLabel ] );
90
-
91
127
  return (
92
128
  <SelectControl
93
129
  __next40pxDefaultSize
@@ -95,8 +131,29 @@ export const TemplateEdit = ( {
95
131
  hideLabelFromVision
96
132
  value={ value }
97
133
  options={ options }
98
- onChange={ onChangeControl }
134
+ onChange={ onChange }
99
135
  disabled={ ! canSwitchTemplate }
100
136
  />
101
137
  );
138
+ }
139
+
140
+ export const TemplateEdit = ( {
141
+ data,
142
+ field,
143
+ onChange,
144
+ }: DataFormControlProps< BasePost > ) => {
145
+ const onChangeControl = useCallback(
146
+ ( newValue: string ) =>
147
+ onChange( {
148
+ [ field.id ]: newValue,
149
+ } ),
150
+ [ field.id, onChange ]
151
+ );
152
+ const mode = useTemplateFieldMode( data );
153
+ if ( ! mode || ! [ 'block-theme', 'classic' ].includes( mode ) ) {
154
+ return null;
155
+ }
156
+ const Edit =
157
+ mode === 'classic' ? ClassicTemplateEdit : BlockThemeTemplateEdit;
158
+ return <Edit data={ data } field={ field } onChange={ onChangeControl } />;
102
159
  };
@@ -5,18 +5,35 @@ import { useSelect } from '@wordpress/data';
5
5
  import type { WpTemplate } from '@wordpress/core-data';
6
6
  import { store as coreStore } from '@wordpress/core-data';
7
7
  import type { DataViewRenderFieldProps } from '@wordpress/dataviews';
8
+ import { __ } from '@wordpress/i18n';
8
9
 
9
10
  /**
10
11
  * Internal dependencies
11
12
  */
12
13
  import { getItemTitle } from '../../actions/utils';
13
14
  import type { BasePost } from '../../types';
14
- import { useDefaultTemplateLabel } from './hooks';
15
+ import { useDefaultTemplateLabel, useTemplateFieldMode } from './hooks';
15
16
 
16
- export const TemplateView = ( {
17
+ function ClassicTemplateView( {
17
18
  item,
18
19
  field,
19
- }: DataViewRenderFieldProps< BasePost > ) => {
20
+ }: DataViewRenderFieldProps< BasePost > ) {
21
+ const templateSlug = field.getValue( { item } );
22
+ const availableTemplates = ( ( item as Record< string, any > )
23
+ ?.available_templates ?? {} ) as Record< string, string >;
24
+
25
+ const classicLabel =
26
+ templateSlug && availableTemplates[ templateSlug ]
27
+ ? availableTemplates[ templateSlug ]
28
+ : __( 'Default template' );
29
+
30
+ return <>{ classicLabel }</>;
31
+ }
32
+
33
+ function BlockThemeTemplateView( {
34
+ item,
35
+ field,
36
+ }: DataViewRenderFieldProps< BasePost > ) {
20
37
  const postType = item.type;
21
38
  const slug = item.slug;
22
39
  const postId = item.id;
@@ -49,4 +66,17 @@ export const TemplateView = ( {
49
66
  );
50
67
 
51
68
  return <>{ templateLabel ?? defaultTemplateLabel }</>;
69
+ }
70
+
71
+ export const TemplateView = ( {
72
+ item,
73
+ field,
74
+ }: DataViewRenderFieldProps< BasePost > ) => {
75
+ const mode = useTemplateFieldMode( item );
76
+ if ( ! mode || ! [ 'block-theme', 'classic' ].includes( mode ) ) {
77
+ return null;
78
+ }
79
+ const View =
80
+ mode === 'classic' ? ClassicTemplateView : BlockThemeTemplateView;
81
+ return <View item={ item } field={ field } />;
52
82
  };