import React, {useEffect, useState} from 'react'; const { __ } = wp.i18n; export function SearchDropdown( props ) { const [searchResultsFieldStatus, setSearchResultsFieldStatus] = useState( 'closed' ); const [textareaValue, setTextareaValue] = useState( '' ); const [dropdownMarkup, setDropdownMarkup] = useState( '' ); const [reset, setReset] = useState( false ); let searchResultsItems = []; /** * Sets the dropdown content when there's a change on any of props.searchResults, props.showDefaultMessage, props.showNoResultsMessage */ useEffect(() => { if ( props.searchResults ) { searchResultsItems = props.searchResults; } else { searchResultsItems = []; } if ( ! props.showDefaultMessage && ! props.showNoResultsMessage ) { setDropdownMarkup( printItems() ); } else { setDropdownMarkup( dropdownContent() ); } }, [props.searchResults, props.showDefaultMessage, props.showNoResultsMessage], reset); /** * Initially sets the dropdown content to the default message. */ useEffect( () => { setDropdownMarkup( getDefaultMessage() ); }, [] ); /** * On dropdown container focus, the textarea gets focused so user can start typing immediately. * @param el */ const handleSearchDropdownFocus = ( el ) => { if ( 'closed' === searchResultsFieldStatus ) { const textarea = el.currentTarget.querySelector( 'textarea' ); if ( 'undefined' !== typeof textarea && textarea !== null ) { textarea.tabIndex = '-1'; textarea.focus(); } } } const handleSearchDropdownClick = ( el ) => { } /** * On keydown sets the value of textarea. * @param el */ const handleSearchDropdownKeyDown = ( el ) => { setTextareaValue( el.currentTarget.value ) setSearchResultsFieldStatus( 'open' ); } /** * Expands results area upon textarea focus. * @param el */ const handleTextareaFocus = ( el ) => { setSearchResultsFieldStatus( 'open' ); } const handleSearchDropdownBlur = ( el ) => { const blurTimer = setInterval(function() { const resultsWrapperId = `sui-select-dropdown-wrap-${props.id}`, textareaId = `search-dropdown-textbox-${props.id}`, rendererWrapId = `blc-search-dropdown__renderer_wrap-${props.id}`; if ( resultsWrapperId !== document.activeElement.id && textareaId !== document.activeElement.id && rendererWrapId !== document.activeElement.id ) { setSearchResultsFieldStatus( 'closed' ); setTextareaValue( '' ); } clearInterval( blurTimer ); }, 160); } const handleResultsContainerClick = ( el ) => { el.currentTarget.tabIndex = '-1'; el.currentTarget.focus(); } const clearDropdown = () => { setSearchResultsFieldStatus( 'closed' ); setTextareaValue( '' ); //props.searchResults = []; setDropdownMarkup( ''); //props.showDefaultMessage = true; //props.showNoResultsMessage = false; setDropdownMarkup( getDefaultMessage() ); setReset( true ) } const printItems = () => { if ( typeof searchResultsItems !== 'undefined' && searchResultsItems.length > 0 ) { return ( searchResultsItems.map( (listItem) => ( ( typeof listItem !== 'undefined' && listItem.hasOwnProperty( 'key' ) ) &&
  • { listItem.onClick(); clearDropdown(); } } > {listItem.display}
  • ) ) ) } } const getDefaultMessage = () => { return
  • {props.defaultMessage}
  • ; } const dropdownContent = () => { if ( props.showDefaultMessage && props.defaultMessage ) { return getDefaultMessage(); } else if ( props.showNoResultsMessage && props.noResultsMessage ) { return
  • {props.noResultsMessage}
  • ; } return printItems(); } return(
    { handleSearchDropdownFocus(e) }} onClick={(e)=> { handleSearchDropdownClick(e) }} onKeyDown={(e)=> { handleSearchDropdownKeyDown(e) }} onBlur={(e)=> { handleSearchDropdownBlur(e) }} >
    { handleResultsContainerClick(e) }} >
      {dropdownMarkup}
    ) }
    {"id":532,"date":"2024-10-20T11:45:49","date_gmt":"2024-10-20T14:45:49","guid":{"rendered":"https:\/\/automax.net.br\/?p=532"},"modified":"2024-10-21T07:57:11","modified_gmt":"2024-10-21T10:57:11","slug":"leonbet-ta-kalytera-stoichimata-kai-bonoys-os-300-eur","status":"publish","type":"post","link":"https:\/\/automax.net.br\/leonbet-ta-kalytera-stoichimata-kai-bonoys-os-300-eur\/","title":{"rendered":"Leonbet: \u03a4\u03b1 \u039a\u03b1\u03bb\u03cd\u03c4\u03b5\u03c1\u03b1 \u03a3\u03c4\u03bf\u03b9\u03c7\u03ae\u03bc\u03b1\u03c4\u03b1 \u039a\u03b1\u03b9 \u039c\u03c0\u03cc\u03bd\u03bf\u03c5\u03c2 \u0388\u03c9\u03c2 300 Eur"},"content":{"rendered":"

    Leonbet: \u03a4\u03b1 \u039a\u03b1\u03bb\u03cd\u03c4\u03b5\u03c1\u03b1 \u03a3\u03c4\u03bf\u03b9\u03c7\u03ae\u03bc\u03b1\u03c4\u03b1 \u039a\u03b1\u03b9 \u039c\u03c0\u03cc\u03bd\u03bf\u03c5\u03c2 \u0388\u03c9\u03c2 300 Eur”<\/p>\n

    Leon Bet \u0395\u03bb\u03bb\u03b1\u03b4\u03b1 \u039a\u03c1\u03b9\u03c4\u03b9\u03ba\u03ae: \u0391\u03be\u03b9\u03bf\u03bb\u03cc\u03b3\u03b7\u03c3\u03b7 \u039a\u03b1\u03b6\u03af\u03bd\u03bf \u039a\u03b1\u03b9 \u03a3\u03c4\u03bf\u03b9\u03c7\u03ae\u03bc\u03b1\u03c4\u03bf\u03c2<\/h1>\n
    \n

    Content<\/p>\n