|
| 1 | +import { FormTokenField } from '@wordpress/components'; |
| 2 | +import { useDebounce } from '@wordpress/compose'; |
| 3 | +import { store as coreStore } from '@wordpress/core-data'; |
| 4 | +import { useSelect } from '@wordpress/data'; |
| 5 | +import { useState, useEffect } from '@wordpress/element'; |
| 6 | +import { decodeEntities } from '@wordpress/html-entities'; |
| 7 | +import { __, sprintf } from '@wordpress/i18n'; |
| 8 | + |
| 9 | +const EMPTY_ARRAY = []; |
| 10 | +const BASE_QUERY = { |
| 11 | + order: 'asc', |
| 12 | + _fields: 'id,name', |
| 13 | + context: 'view', |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * Helper function to get the term id based on user input in terms `FormTokenField`. |
| 18 | + * |
| 19 | + * @param {Array} terms Array of terms from the search results. |
| 20 | + * @param {string|object} termValue Single term name or object. |
| 21 | + * |
| 22 | + * @returns {number} The term ID. |
| 23 | + */ |
| 24 | +const getTermIdByTermValue = ( terms, termValue ) => { |
| 25 | + // First we check for exact match by `term.id` or case sensitive `term.name` match. |
| 26 | + const termId = |
| 27 | + termValue?.id || terms?.find( ( term ) => term.name === termValue )?.id; |
| 28 | + if ( termId ) { |
| 29 | + return termId; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Here we make an extra check for entered terms in a non case sensitive way, |
| 34 | + * to match user expectations, due to `FormTokenField` behaviour that shows |
| 35 | + * suggestions which are case insensitive. |
| 36 | + * |
| 37 | + * Although WP tries to discourage users to add terms with the same name (case insensitive), |
| 38 | + * it's still possible if you manually change the name, as long as the terms have different slugs. |
| 39 | + * In this edge case we always apply the first match from the terms list. |
| 40 | + */ |
| 41 | + const termValueLower = termValue.toLocaleLowerCase(); |
| 42 | + return terms?.find( ( term ) => term.name.toLocaleLowerCase() === termValueLower ) |
| 43 | + ?.id; |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * Renders a `FormTokenField` for a given taxonomy. Based on the Query Loop block taxonomy controls. |
| 48 | + * https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/query/edit/inspector-controls/taxonomy-controls.js |
| 49 | + * |
| 50 | + * @param {object} props The props for the component. |
| 51 | + * @param {string} props.label The label text for the search field. |
| 52 | + * @param {object} props.taxonomy The taxonomy object. |
| 53 | + * @param {number[]} props.termIds An array with the block's term ids for the given taxonomy. |
| 54 | + * @param {Function} props.onChange Callback `onChange` function. |
| 55 | + * |
| 56 | + * @returns {Element} The rendered component. |
| 57 | + */ |
| 58 | +function TermSearchControl( { label, taxonomy, termIds, onChange } ) { |
| 59 | + const [ search, setSearch ] = useState( '' ); |
| 60 | + const [ value, setValue ] = useState( EMPTY_ARRAY ); |
| 61 | + const [ suggestions, setSuggestions ] = useState( EMPTY_ARRAY ); |
| 62 | + const debouncedSearch = useDebounce( setSearch, 250 ); |
| 63 | + const taxObject = useSelect( |
| 64 | + ( select ) => { |
| 65 | + return select( 'core' ).getTaxonomy( taxonomy ); |
| 66 | + }, |
| 67 | + [ taxonomy ] |
| 68 | + ); |
| 69 | + const { searchResults, searchHasResolved } = useSelect( |
| 70 | + ( select ) => { |
| 71 | + if ( ! search ) { |
| 72 | + return { |
| 73 | + searchResults: EMPTY_ARRAY, |
| 74 | + searchHasResolved: true, |
| 75 | + }; |
| 76 | + } |
| 77 | + const { getEntityRecords, hasFinishedResolution } = select( coreStore ); |
| 78 | + const selectorArgs = [ |
| 79 | + 'taxonomy', |
| 80 | + taxonomy, |
| 81 | + { |
| 82 | + ...BASE_QUERY, |
| 83 | + search, |
| 84 | + orderby: 'name', |
| 85 | + exclude: termIds, |
| 86 | + per_page: 20, |
| 87 | + }, |
| 88 | + ]; |
| 89 | + return { |
| 90 | + searchResults: getEntityRecords( ...selectorArgs ), |
| 91 | + searchHasResolved: hasFinishedResolution( |
| 92 | + 'getEntityRecords', |
| 93 | + selectorArgs |
| 94 | + ), |
| 95 | + }; |
| 96 | + }, |
| 97 | + [ search, termIds ] |
| 98 | + ); |
| 99 | + |
| 100 | + // `existingTerms` are the ones fetched from the API and their type is `{ id: number; name: string }`. |
| 101 | + // They are used to extract the terms' names to populate the `FormTokenField` properly |
| 102 | + // and to sanitize the provided `termIds`, by setting only the ones that exist. |
| 103 | + const existingTerms = useSelect( |
| 104 | + ( select ) => { |
| 105 | + if ( ! termIds?.length ) { |
| 106 | + return EMPTY_ARRAY; |
| 107 | + } |
| 108 | + const { getEntityRecords } = select( coreStore ); |
| 109 | + return getEntityRecords( 'taxonomy', taxonomy, { |
| 110 | + ...BASE_QUERY, |
| 111 | + include: termIds, |
| 112 | + per_page: termIds.length, |
| 113 | + } ); |
| 114 | + }, |
| 115 | + [ termIds ] |
| 116 | + ); |
| 117 | + |
| 118 | + // Update the `value` state only after the selectors are resolved |
| 119 | + // to avoid emptying the input when we're changing terms. |
| 120 | + useEffect( () => { |
| 121 | + if ( ! termIds?.length ) { |
| 122 | + setValue( EMPTY_ARRAY ); |
| 123 | + } |
| 124 | + if ( ! existingTerms?.length ) { |
| 125 | + return; |
| 126 | + } |
| 127 | + // Returns only the existing entity ids. This prevents the component |
| 128 | + // from crashing in the editor, when non existing ids are provided. |
| 129 | + const sanitizedValue = termIds.reduce( ( accumulator, id ) => { |
| 130 | + const entity = existingTerms.find( ( term ) => term.id === id ); |
| 131 | + if ( entity ) { |
| 132 | + accumulator.push( { |
| 133 | + id, |
| 134 | + value: entity.name, |
| 135 | + } ); |
| 136 | + } |
| 137 | + return accumulator; |
| 138 | + }, [] ); |
| 139 | + setValue( sanitizedValue ); |
| 140 | + }, [ termIds, existingTerms ] ); |
| 141 | + |
| 142 | + // Update suggestions only when the query has resolved. |
| 143 | + useEffect( () => { |
| 144 | + if ( ! searchHasResolved ) { |
| 145 | + return; |
| 146 | + } |
| 147 | + setSuggestions( searchResults.map( ( result ) => result.name ) ); |
| 148 | + }, [ searchResults, searchHasResolved ] ); |
| 149 | + |
| 150 | + /** |
| 151 | + * Function to handle change of selected terms. |
| 152 | + * |
| 153 | + * @param {Array} newTermValues Array of new term values. |
| 154 | + */ |
| 155 | + const onTermsChange = ( newTermValues ) => { |
| 156 | + const newTermIds = new Set(); |
| 157 | + for ( const termValue of newTermValues ) { |
| 158 | + const termId = getTermIdByTermValue( searchResults, termValue ); |
| 159 | + if ( termId ) { |
| 160 | + newTermIds.add( termId ); |
| 161 | + } |
| 162 | + } |
| 163 | + setSuggestions( EMPTY_ARRAY ); |
| 164 | + onChange( Array.from( newTermIds ) ); |
| 165 | + }; |
| 166 | + |
| 167 | + return ( |
| 168 | + <FormTokenField |
| 169 | + displayTransform={ decodeEntities } |
| 170 | + label={ |
| 171 | + label || |
| 172 | + sprintf( |
| 173 | + __( 'Filter by %s', 'block-editor-components' ), |
| 174 | + taxObject |
| 175 | + ? taxObject?.labels?.singular_name |
| 176 | + : __( 'term', 'block-editor-components' ) |
| 177 | + ) |
| 178 | + } |
| 179 | + suggestions={ suggestions } |
| 180 | + value={ value } |
| 181 | + onChange={ onTermsChange } |
| 182 | + onInputChange={ debouncedSearch } |
| 183 | + /> |
| 184 | + ); |
| 185 | +} |
| 186 | + |
| 187 | +export default TermSearchControl; |
0 commit comments