| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import { useCallback, WPElement } from '@wordpress/element'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Internal dependencies. |
| 8 |
*/ |
| 9 |
import { useApiSearch } from '../../../../api-search'; |
| 10 |
import { useDebounce } from '../hooks'; |
| 11 |
import Combobox from './components/combobox'; |
| 12 |
import Suggestion from './components/suggestion'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Autosuggest app. |
| 16 |
* |
| 17 |
* @param {object} props Component props. |
| 18 |
* @param {string} props.adminUrl Admin URL. |
| 19 |
* @param {string} props.dateFormat Date format string. |
| 20 |
* @param {object} props.statusLabels Post status labels. |
| 21 |
* @param {string} props.timeFormat Time format string. |
| 22 |
* @param {string} props.value Input value. |
| 23 |
* @returns {WPElement} Rendered component. |
| 24 |
*/ |
| 25 |
export default ({ adminUrl, dateFormat, statusLabels, timeFormat, value, ...props }) => { |
| 26 |
/** |
| 27 |
* Use the API Search provider. |
| 28 |
*/ |
| 29 |
const { clearResults, isLoading, searchFor, searchResults } = useApiSearch(); |
| 30 |
|
| 31 |
/** |
| 32 |
* Update the search term, debounced. |
| 33 |
* |
| 34 |
* @param {string} searchTerm Search term. |
| 35 |
* @returns {void} |
| 36 |
*/ |
| 37 |
const dispatchSearchFor = useDebounce((searchTerm) => { |
| 38 |
searchFor(searchTerm); |
| 39 |
}, 300); |
| 40 |
|
| 41 |
/** |
| 42 |
* Input event handler. |
| 43 |
* |
| 44 |
* @param {Event} event keyupEvent |
| 45 |
*/ |
| 46 |
const onInput = useCallback( |
| 47 |
(event) => { |
| 48 |
const { value } = event.target; |
| 49 |
|
| 50 |
if (value) { |
| 51 |
dispatchSearchFor(event.target.value); |
| 52 |
} else { |
| 53 |
clearResults(); |
| 54 |
} |
| 55 |
}, |
| 56 |
[clearResults, dispatchSearchFor], |
| 57 |
); |
| 58 |
|
| 59 |
/** |
| 60 |
* Selection event handler. |
| 61 |
* |
| 62 |
* Fires when a suggestion from the combobox component is selected. The |
| 63 |
* value will be the value prop of the child that was selected. |
| 64 |
* |
| 65 |
* @param {*} value Selection value. |
| 66 |
* @param {Event} event Click or keydown event. |
| 67 |
*/ |
| 68 |
const onSelect = useCallback( |
| 69 |
(value, event) => { |
| 70 |
window.open( |
| 71 |
`${adminUrl}?post=${value}&action=edit`, |
| 72 |
event.metaKey ? '_blank' : '_self', |
| 73 |
); |
| 74 |
}, |
| 75 |
[adminUrl], |
| 76 |
); |
| 77 |
|
| 78 |
/** |
| 79 |
* Render. |
| 80 |
*/ |
| 81 |
return ( |
| 82 |
<Combobox |
| 83 |
defaultValue={value} |
| 84 |
id="ep-orders-suggestions" |
| 85 |
isBusy={isLoading} |
| 86 |
onInput={onInput} |
| 87 |
onSelect={onSelect} |
| 88 |
{...props} |
| 89 |
> |
| 90 |
{searchResults.map((hit) => { |
| 91 |
const { |
| 92 |
_id, |
| 93 |
_source: { post_id }, |
| 94 |
} = hit; |
| 95 |
|
| 96 |
return ( |
| 97 |
<Suggestion |
| 98 |
dateFormat={dateFormat} |
| 99 |
id={`ep-order-suggestion-${_id}`} |
| 100 |
hit={hit} |
| 101 |
key={_id} |
| 102 |
statusLabels={statusLabels} |
| 103 |
timeFormat={timeFormat} |
| 104 |
value={post_id} |
| 105 |
/> |
| 106 |
); |
| 107 |
})} |
| 108 |
</Combobox> |
| 109 |
); |
| 110 |
}; |
| 111 |
|