Add support for basic tab navigation when selecting template

MAILPOET-6331
This commit is contained in:
Oluwaseun Olorunsola
2024-12-03 17:08:37 +01:00
committed by Oluwaseun Olorunsola
parent 5743ccbc5f
commit cb091e0dc6
4 changed files with 143 additions and 97 deletions

View File

@ -1,4 +1,4 @@
import { useState } from '@wordpress/element';
import { store as editorStore } from '@wordpress/editor'; import { store as editorStore } from '@wordpress/editor';
import { dispatch } from '@wordpress/data'; import { dispatch } from '@wordpress/data';
import { import {
@ -15,6 +15,33 @@ import { TemplateCategoriesListSidebar } from './template-categories-list-sideba
const BLANK_TEMPLATE = 'email-general'; const BLANK_TEMPLATE = 'email-general';
function SelectTemplateBody( {
initialCategory,
templateCategories,
templates,
handleTemplateSelection,
} ) {
const [ selectedCategory, setSelectedCategory ] = useState(
initialCategory?.name
);
return (
<div className="block-editor-block-patterns-explorer">
<TemplateCategoriesListSidebar
templateCategories={ templateCategories }
selectedCategory={ selectedCategory }
onClickCategory={ setSelectedCategory }
/>
<TemplateList
templates={ templates }
onTemplateSelection={ handleTemplateSelection }
selectedCategory={ selectedCategory }
/>
</div>
);
}
export function SelectTemplateModal( { export function SelectTemplateModal( {
onSelectCallback, onSelectCallback,
closeCallback = null, closeCallback = null,
@ -52,28 +79,28 @@ export function SelectTemplateModal( {
const dummyTemplateCategories = [ const dummyTemplateCategories = [
{ {
name: 'recent', name: 'recent',
label: 'Recent' label: 'Recent',
}, },
{ {
name: 'basic', name: 'basic',
label: 'Basic' label: 'Basic',
} },
]; ];
const onClickCategory = () => {}
return ( return (
<Modal <Modal
title="Select a template" title={ __( 'Select a template', 'mailpoet' ) }
onRequestClose={ () => onRequestClose={ () =>
closeCallback ? closeCallback() : handleCloseWithoutSelection() closeCallback ? closeCallback() : handleCloseWithoutSelection()
} }
isFullScreen isFullScreen
> >
<div className="block-editor-block-patterns-explorer"> <SelectTemplateBody
<TemplateCategoriesListSidebar templateCategories={dummyTemplateCategories} selectedCategory={dummyTemplateCategories[0].name} onClickCategory={onClickCategory} /> initialCategory={ dummyTemplateCategories[ 0 ] }
templateCategories={ dummyTemplateCategories }
<TemplateList templates={templates} onTemplateSelection={ handleTemplateSelection} /> templates={ templates }
handleTemplateSelection={ handleTemplateSelection }
/>
<Flex justify="flex-end"> <Flex justify="flex-end">
<FlexItem> <FlexItem>
@ -86,7 +113,6 @@ export function SelectTemplateModal( {
</Button> </Button>
</FlexItem> </FlexItem>
</Flex> </Flex>
</div>
</Modal> </Modal>
); );
} }

View File

@ -1,18 +1,15 @@
import { useMemo } from '@wordpress/element';
// @ts-expect-error No types available for this component // @ts-expect-error No types available for this component
import { BlockPreview } from '@wordpress/block-editor'; import { BlockPreview } from '@wordpress/block-editor';
import { import {
__experimentalHStack as HStack, // eslint-disable-line __experimentalHStack as HStack, // eslint-disable-line
} from '@wordpress/components'; } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { Async } from './async'; import { Async } from './async';
export function TemplateList({templates, onTemplateSelection}) { function TemplateListBox( { templates, onTemplateSelection } ) {
return ( return (
<div className="block-editor-block-patterns-explorer__list"> <div className="block-editor-block-patterns-list" role="listbox">
<div
className="block-editor-block-patterns-list"
role="listbox"
>
{ templates.map( ( template ) => ( { templates.map( ( template ) => (
<div <div
key={ template.slug } key={ template.slug }
@ -26,17 +23,16 @@ export function TemplateList({templates, onTemplateSelection}) {
onTemplateSelection( template ); onTemplateSelection( template );
} } } }
onKeyPress={ ( event ) => { onKeyPress={ ( event ) => {
if ( if ( event.key === 'Enter' || event.key === ' ' ) {
event.key === 'Enter' ||
event.key === ' '
) {
onTemplateSelection( template ); onTemplateSelection( template );
} }
} } } }
> >
<Async <Async
placeholder={ placeholder={
<p>rendering template</p> <p>
{ __( 'rendering template', 'mailpoet' ) }
</p>
} }
> >
<BlockPreview <BlockPreview
@ -52,9 +48,7 @@ export function TemplateList({templates, onTemplateSelection}) {
<HStack className="block-editor-patterns__pattern-details"> <HStack className="block-editor-patterns__pattern-details">
<div className="block-editor-block-patterns-list__item-title"> <div className="block-editor-block-patterns-list__item-title">
{ { template.template.title.rendered }
template.template.title.rendered
}
</div> </div>
</HStack> </HStack>
</Async> </Async>
@ -62,6 +56,28 @@ export function TemplateList({templates, onTemplateSelection}) {
</div> </div>
) ) } ) ) }
</div> </div>
);
}
export function TemplateList( {
templates,
onTemplateSelection,
selectedCategory,
} ) {
const filteredTemplates = useMemo(
() =>
templates.filter(
( template ) => template.category === selectedCategory
),
[ selectedCategory, templates ]
);
return (
<div className="block-editor-block-patterns-explorer__list">
<TemplateListBox
templates={ filteredTemplates }
onTemplateSelection={ onTemplateSelection }
/>
</div> </div>
); );
} }

View File

@ -103,6 +103,7 @@ export function usePreviewTemplates(
? contentPatternBlocksGeneral ? contentPatternBlocksGeneral
: contentPatternBlocks, : contentPatternBlocks,
template, template,
category: 'basic', // TODO: This will be updated once template category is implemented
}; };
} ), } ),
]; ];

View File

@ -228,8 +228,11 @@ export type TemplatePreview = {
previewContentParsed: BlockInstance[]; previewContentParsed: BlockInstance[];
emailParsed: BlockInstance[]; emailParsed: BlockInstance[];
template: EmailTemplatePreview; template: EmailTemplatePreview;
category?: TemplateCategory;
}; };
export type TemplateCategory = 'recent' | 'basic';
export type Feature = export type Feature =
| 'fullscreenMode' | 'fullscreenMode'
| 'showIconLabels' | 'showIconLabels'