| | |
| | | } |
| | | if (subtype === 'taxonomy') { |
| | | // Assumes taxonomy rest_base matches the (unprefixed) taxonomy slug. |
| | | return (search) => addQueryArgs(`/wp/v2/${field.taxonomy}`, { search, per_page: 20 }); |
| | | return (search) => addQueryArgs(`/wp/v2/${field.taxonomy}`, { search, per_page: 20, hide_empty: false }); |
| | | } |
| | | return (search) => addQueryArgs('/wp/v2/search', { search, type: 'post', subtype: field.post_type, per_page: 20 }); |
| | | }; |
| | | |
| | | const createPayload = (field, subtype, name) => |
| | | subtype === 'post' ? { title: name, status: 'publish', postType: field.post_type } : { name }; |
| | | subtype === 'post' ? { title: name, status: 'publish', postType: field.post_type } : { name: name }; |
| | | |
| | | const labelOf = (item, subtype) => (subtype === 'user' ? item.name : item.title?.rendered ?? item.title ?? item.name); |
| | | |
| | |
| | | |
| | | // Resolve current IDs to labels on mount / value change |
| | | useEffect(() => { |
| | | onInputChange(''); |
| | | let cancelled = false; |
| | | if (!ids.length) { |
| | | setTokens([]); |
| | |
| | | |
| | | const onInputChange = useCallback((search) => { |
| | | setSearch(search); |
| | | if (!search) { setOptions({}); return;} |
| | | |
| | | apiFetch({ path: buildPath(search) }) |
| | | .then((results) => { |
| | |
| | | if (isSingle) { |
| | | onChange(newIds[newIds.length - 1] ?? 0); |
| | | } else { |
| | | onChange(field.max ? newIds.slice(0, field.max) : newIds); |
| | | onChange((field.max ? newIds.slice(0, field.max) : newIds).join(',')); |
| | | } |
| | | }; |
| | | |
| | |
| | | |
| | | if (pendingCreate && canCreate && !creating) { |
| | | setCreating(true); |
| | | apiFetch({ path: `/wp/v2/${field.taxonomy}`, method: 'POST', data: createPayload(subtype, search) }) |
| | | apiFetch({ path: `/wp/v2/${field.taxonomy}`, method: 'POST', data: createPayload(field, subtype, search) }) |
| | | .then((item) => { |
| | | const label = labelOf(item, subtype); |
| | | const finalTokens = tokens.filter((t) => t !== createLabel).concat(label); |