From 9267c868d72de3b9ced27b6d90857be4e61561ec Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 20 Oct 2026 10:08:02 +0300 Subject: [PATCH 1/2] Template field: Show the default template instead of the `Select` placeholder --- package-lock.json | 1 + packages/core-fields/package.json | 1 + .../src/post_type_supports/template/edit.tsx | 29 ++++-- .../src/test/template-edit.jsdom.test.tsx | 94 +++++++++++++++++++ 4 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 packages/core-fields/src/test/template-edit.jsdom.test.tsx diff --git a/package-lock.json b/package-lock.json index a4b37787b12382..2029e1019650ec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -45374,6 +45374,7 @@ "remove-accents": "^0.5.0" }, "devDependencies": { + "@testing-library/react": "^16.3.3", "vitest": "^5.0.3" }, "engines": { diff --git a/packages/core-fields/package.json b/packages/core-fields/package.json index b1d1994d2f8004..8c71d84d8f4bbc 100644 --- a/packages/core-fields/package.json +++ b/packages/core-fields/package.json @@ -106,6 +106,7 @@ "remove-accents": "^0.5.0" }, "devDependencies": { + "@testing-library/react": "^16.3.3", "vitest": "^5.0.3" }, "peerDependencies": { diff --git a/packages/core-fields/src/post_type_supports/template/edit.tsx b/packages/core-fields/src/post_type_supports/template/edit.tsx index 1c1914fe67a840..c2bba8786db8a7 100644 --- a/packages/core-fields/src/post_type_supports/template/edit.tsx +++ b/packages/core-fields/src/post_type_supports/template/edit.tsx @@ -21,6 +21,19 @@ type TemplateEditComponentProps = Pick< const EMPTY_ARRAY: [] = []; +// The value of the default template option. The post stores the default +// template as an empty string, but the select treats an empty value as no +// selection and shows its placeholder. +const DEFAULT_TEMPLATE_VALUE = '__default'; + +function toItemValue( template: string | undefined ) { + return template || DEFAULT_TEMPLATE_VALUE; +} + +function fromItemValue( itemValue: string | null | undefined ) { + return ! itemValue || itemValue === DEFAULT_TEMPLATE_VALUE ? '' : itemValue; +} + /** * Whether the post is the front page or the posts page, which are assigned * a template by the reading settings rather than by this field. @@ -60,7 +73,7 @@ function ClassicTemplateEdit( { Object.entries( data?.available_templates ?? {} ).map( ( [ templateSlug, title ] ) => ( { label: title, - value: templateSlug, + value: toItemValue( templateSlug ), } ) ), [ data ] @@ -71,12 +84,14 @@ function ClassicTemplateEdit( { label={ __( 'Template' ) } hideLabelFromVision value={ - items.find( ( item ) => item.value === value ) ?? + items.find( ( item ) => item.value === toItemValue( value ) ) ?? items[ 0 ] ?? null } items={ items } - onValueChange={ ( item ) => onChange( item?.value ?? '' ) } + onValueChange={ ( item ) => + onChange( fromItemValue( item?.value ) ) + } disabled={ ! canSwitchTemplate } /> ); @@ -111,7 +126,7 @@ function BlockThemeTemplateEdit( { const value = field.getValue( { item: data } ); const items = useMemo( () => [ - { label: defaultTemplateLabel, value: '' }, + { label: defaultTemplateLabel, value: DEFAULT_TEMPLATE_VALUE }, ...templates.map( ( template ) => ( { label: getItemTitle( template ), value: template.slug, @@ -124,12 +139,14 @@ function BlockThemeTemplateEdit( { label={ __( 'Template' ) } hideLabelFromVision value={ - items.find( ( item ) => item.value === value ) ?? + items.find( ( item ) => item.value === toItemValue( value ) ) ?? items[ 0 ] ?? null } items={ items } - onValueChange={ ( item ) => onChange( item?.value ?? '' ) } + onValueChange={ ( item ) => + onChange( fromItemValue( item?.value ) ) + } disabled={ ! canSwitchTemplate } /> ); diff --git a/packages/core-fields/src/test/template-edit.jsdom.test.tsx b/packages/core-fields/src/test/template-edit.jsdom.test.tsx new file mode 100644 index 00000000000000..269ceb36ade7ea --- /dev/null +++ b/packages/core-fields/src/test/template-edit.jsdom.test.tsx @@ -0,0 +1,94 @@ +import { render, screen } from '@testing-library/react'; +import { describe, expect, it } from 'vitest'; +import { + createReduxStore, + createRegistry, + RegistryProvider, +} from '@wordpress/data'; +import { unlock } from '../lock-unlock'; +import TemplateEdit from '../post_type_supports/template/edit'; + +const field = { + id: 'template', + getValue: ( { item }: { item: { template?: string } } ) => item.template, +} as any; + +const CUSTOM_TEMPLATES = [ { slug: 'custom', title: { rendered: 'Custom' } } ]; +const DEFAULT_TEMPLATE = { + slug: 'single', + title: { rendered: 'Single Posts' }, +}; + +// The field reads the post type, the theme, and the templates from the +// `core` store; a stand-in avoids the resolvers' requests. +function createTestRegistry( { isBlockTheme }: { isBlockTheme: boolean } ) { + const store = createReduxStore( 'core', { + reducer: ( state = {} ) => state, + selectors: { + getPostType: () => ( { viewable: true } ), + getCurrentTheme: () => ( { is_block_theme: isBlockTheme } ), + getEntityRecords: () => CUSTOM_TEMPLATES, + getDefaultTemplateId: () => 'theme//single', + getEntityRecord: () => DEFAULT_TEMPLATE, + }, + } ); + unlock( store ).registerPrivateSelectors( { + getTemplateId: () => 'theme//single', + getHomePage: () => null, + getPostsPageId: () => null, + } ); + const registry = createRegistry(); + registry.register( store ); + return registry; +} + +describe( 'TemplateEdit', () => { + it( 'shows the default template of a block theme as the selected one', () => { + render( + + {} } + /> + + ); + + const select = screen.getByRole( 'combobox', { name: 'Template' } ); + expect( select ).toHaveTextContent( 'Single Posts' ); + expect( select ).not.toHaveAttribute( 'data-placeholder' ); + } ); + + it( 'shows the default template of a classic theme as the selected one', () => { + render( + + {} } + /> + + ); + + const select = screen.getByRole( 'combobox', { name: 'Template' } ); + expect( select ).toHaveTextContent( 'Default template' ); + expect( select ).not.toHaveAttribute( 'data-placeholder' ); + } ); +} ); From 19273275a21c61e8effd3f4b7c93354d2dcd2ea8 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 20 Oct 2026 11:19:03 +0300 Subject: [PATCH 2/2] rename DEFAULT_TEMPLATE_VALUE --- .../core-fields/src/post_type_supports/template/edit.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/core-fields/src/post_type_supports/template/edit.tsx b/packages/core-fields/src/post_type_supports/template/edit.tsx index c2bba8786db8a7..372745a90754ad 100644 --- a/packages/core-fields/src/post_type_supports/template/edit.tsx +++ b/packages/core-fields/src/post_type_supports/template/edit.tsx @@ -23,8 +23,9 @@ const EMPTY_ARRAY: [] = []; // The value of the default template option. The post stores the default // template as an empty string, but the select treats an empty value as no -// selection and shows its placeholder. -const DEFAULT_TEMPLATE_VALUE = '__default'; +// selection and shows its placeholder. Template slugs can't start with a +// slash, so this can't clash with a real template. +const DEFAULT_TEMPLATE_VALUE = '//default'; function toItemValue( template: string | undefined ) { return template || DEFAULT_TEMPLATE_VALUE;