11/* eslint-disable complexity */
22import { Combobox } from "@headlessui/react" ;
33import { SearchIcon } from "@heroicons/react/outline" ;
4- import { useCallback , useRef , useState } from "@wordpress/element" ;
4+ import { useCallback , useRef , useState , useMemo } from "@wordpress/element" ;
55import { __ } from "@wordpress/i18n" ;
6- import { Modal , Title , useSvgAria , useToggleState } from "@yoast/ui-library" ;
6+ import { Modal , Title , useSvgAria , useToggleState , Code } from "@yoast/ui-library" ;
77import classNames from "classnames" ;
88import { debounce , first , groupBy , includes , isEmpty , map , max , reduce , split , trim , values } from "lodash" ;
99import PropTypes from "prop-types" ;
@@ -13,6 +13,42 @@ import { safeToLocaleLower } from "../helpers";
1313import { useParsedUserAgent , useSelectSettings } from "../hooks" ;
1414
1515const QUERY_MIN_CHARS = 3 ;
16+ const POST_TYPE_OR_TAXONOMY_BREADCRUMB_SETTING_REGEXP = new RegExp ( / ^ i n p u t - w p s e o _ t i t l e s - ( p o s t _ t y p e s | t a x o n o m y ) - (?< name > \S + ) - ( m a i n t a x | p t p a r e n t ) $ / is ) ;
17+
18+ /**
19+ * @param {string } fieldId The item field ID.
20+ * @param {string } fieldLabel The item label.
21+ * @returns {JSX.Element } The SearchResultLabel element.
22+ */
23+ const SearchResultLabel = ( { fieldId, fieldLabel } ) => {
24+ // Deduce wether field is a breadcrumb option for post type or taxonomy.
25+ const { isPostTypeOrTaxonomyBreadcrumbSetting, postTypeOrTaxonomyName } = useMemo ( ( ) => {
26+ const matches = POST_TYPE_OR_TAXONOMY_BREADCRUMB_SETTING_REGEXP . exec ( fieldId ) ;
27+ return {
28+ isPostTypeOrTaxonomyBreadcrumbSetting : Boolean ( matches ) ,
29+ postTypeOrTaxonomyName : matches ?. groups ?. name ,
30+ } ;
31+ } , [ fieldId , POST_TYPE_OR_TAXONOMY_BREADCRUMB_SETTING_REGEXP ] ) ;
32+
33+ // Render additional code block with post type or taxonomy name if applicable.
34+ if ( isPostTypeOrTaxonomyBreadcrumbSetting ) {
35+ return (
36+ < >
37+ { fieldLabel }
38+ { postTypeOrTaxonomyName && (
39+ < Code className = "yst-ml-2 group-hover:yst-bg-primary-200 group-hover:yst-text-primary-800" > { postTypeOrTaxonomyName } </ Code >
40+ ) }
41+ </ >
42+ ) ;
43+ }
44+
45+ return fieldLabel ;
46+ } ;
47+
48+ SearchResultLabel . propTypes = {
49+ fieldId : PropTypes . string . isRequired ,
50+ fieldLabel : PropTypes . string . isRequired ,
51+ } ;
1652
1753/**
1854 * @param {string } title The title.
@@ -176,13 +212,13 @@ const Search = () => {
176212 < li key = { groupedItems ?. [ 0 ] ?. route || `group-${ index } ` } >
177213 < Title as = "h4" size = "3" className = "yst-bg-slate-100 yst-py-3 yst-px-4" > { first ( groupedItems ) . routeLabel } </ Title >
178214 < ul >
179- { map ( groupedItems , ( item , name ) => (
215+ { map ( groupedItems , ( item ) => (
180216 < Combobox . Option
181- key = { name }
217+ key = { item . fieldId }
182218 value = { item }
183219 className = { handleOptionActiveState }
184220 >
185- { item . fieldLabel }
221+ < SearchResultLabel { ... item } />
186222 </ Combobox . Option >
187223 ) ) }
188224 </ ul >
0 commit comments