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":770,"date":"2024-10-25T00:03:39","date_gmt":"2024-10-25T03:03:39","guid":{"rendered":"https:\/\/automax.net.br\/?p=770"},"modified":"2024-10-25T00:03:39","modified_gmt":"2024-10-25T03:03:39","slug":"%d0%b2%d0%be%d0%b9%d0%b4%d0%b8%d1%82%d0%b5-%d0%b2-%d0%be%d0%bd%d0%bb%d0%b0%d0%b9%d0%bd-%d0%ba%d0%b0%d0%b7%d0%b8%d0%bd%d0%be-1win-%d0%b8-%d0%bf%d0%be%d0%bb%d1%83%d1%87%d0%b8%d1%82%d0%b5-%d0%bf%d1%80","status":"publish","type":"post","link":"https:\/\/automax.net.br\/%d0%b2%d0%be%d0%b9%d0%b4%d0%b8%d1%82%d0%b5-%d0%b2-%d0%be%d0%bd%d0%bb%d0%b0%d0%b9%d0%bd-%d0%ba%d0%b0%d0%b7%d0%b8%d0%bd%d0%be-1win-%d0%b8-%d0%bf%d0%be%d0%bb%d1%83%d1%87%d0%b8%d1%82%d0%b5-%d0%bf%d1%80\/","title":{"rendered":"\u0412\u043e\u0439\u0434\u0438\u0442\u0435 \u0432 \u041e\u043d\u043b\u0430\u0439\u043d-\u043a\u0430\u0437\u0438\u043d\u043e 1win \u0438 \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u041f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0411\u043e\u043d\u0443"},"content":{"rendered":"

    \u0412\u043e\u0439\u0434\u0438\u0442\u0435 \u0432 \u041e\u043d\u043b\u0430\u0439\u043d-\u043a\u0430\u0437\u0438\u043d\u043e 1win \u0438 \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u041f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0411\u043e\u043d\u0443\u0441<\/p>\n

    \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f 1win \u0412\u043e\u0439\u0434\u0438\u0442\u0435 \u0412 \u0421\u0438\u0441\u0442\u0435\u043c\u0443 1win \u0418 \u041e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 \u0421\u0447\u0435\u0442 \u0412 \u042d\u0442\u043e\u0439 \u043c\u0438\u0440\u043e\u0432\u044b\u0445 \u0411\u0443\u043a\u043c\u0435\u043a\u0435\u0440\u0441\u043a\u043e\u0439 \u041a\u043e\u043d\u0442\u043e\u0440\u0435<\/h1>\n
    \n

    Content<\/p>\n