import * as React from 'react' import cx from 'classnames' import { useState, useEffect, } from '@wordpress/element' import { BaseControl, Icon, Button, TextControl, FormTokenField, } from '@wordpress/components' import { useDebounce, } from '@wordpress/compose' import { useSelect, } from '@wordpress/data' import { store as coreStore, } from '@wordpress/core-data' import { search, } from '@wordpress/icons' import { decodeEntities, } from '@wordpress/html-entities' import type { FormTokenFieldProps, } from '@wordpress/components/build-types/form-token-field/types' import type { BaseControlProps, } from '@wordpress/components/build-types/base-control/types' import './style.scss' const EMPTY_RESULTS: any[] = [] const EMPTY_SUGGESTIONS: string[] = [] const ID_SEPARATOR = ' | ' const BASE_QUERY = { order: 'asc', _fields: 'id,title', context: 'view', } interface PostFormTokenFieldProps extends Omit { label?: string placeholder?: string value: number[] onChange: (nextValue: number[]) => void postType?: string } const PostFormTokenField: React.FC = ({ label, placeholder, value, onChange, postType = 'post', ...props }) => { const [search, setSearch] = useState('') const [suggestions, setSuggestions] = useState(EMPTY_SUGGESTIONS) const debouncedSearch = useDebounce(setSearch, 250) const { searchResults, searchHasResolved, } = useSelect(select => { if(!search) { return {searchResults: EMPTY_RESULTS, searchHasResolved: true} } const { getEntityRecords, // @ts-expect-error hasFinishedResolution, } = select(coreStore) const selectorArgs = [ 'postType', postType, { ...BASE_QUERY, search, orderby: 'relevance', exclude: value, per_page: 20, }, ] // @ts-expect-error const searchResults: any = getEntityRecords(...selectorArgs) const searchHasResolved = hasFinishedResolution('getEntityRecords', selectorArgs) return {searchResults, searchHasResolved} }, [search, postType, value]) useEffect(() => { if(!searchHasResolved) { return } const nextSuggestions = searchResults.reduce((acc: string[], cur: any) => { const { id = 0, title = {}, } = cur const { rendered = '', } = title const ID = Number(id) if(!isNaN(ID) && rendered) { acc.push(`${ID}${ID_SEPARATOR}${decodeEntities(rendered)}`) } return acc }, []) setSuggestions(nextSuggestions) }, [searchResults, searchHasResolved]) return ( v.toString())} onInputChange={debouncedSearch} suggestions={suggestions} onChange={nextValue => { const nextIds = nextValue.map(v => { if(typeof v === 'string') { if(v.indexOf(ID_SEPARATOR) !== -1) { const [id] = v.split(ID_SEPARATOR) v = id } if(!isNaN(Number(v))) { return Number(v) } } return null }).filter(v => v) as any as number[] onChange(nextIds) }} // displayTransform={v => { // if(!isNaN(Number(v))) { // console.log('display ID', v) // } // return v // }} help={false} // __experimentalExpandOnFocus={true} __next40pxDefaultSize __nextHasNoMarginBottom /> ) } export interface PostIdsControlProps extends Omit { label?: string toggleLabel?: string placeholder?: string postType?: string /** Comma-separated string of IDs. */ value: string onChange: (nextValue: string) => void } const PostIdsControl: React.FC = ({ label, toggleLabel = 'Toggle edit mode', placeholder = '4, 8, 15, 16, 23, 42…', postType, value, onChange, ...props }) => { const [rawEdit, setRawEdit] = useState(true) const ids = value.split(',').map(v => v.trim()).filter(v => v).filter(v => !isNaN(Number(v))).map(v => Number(v)) return ( {rawEdit && ( )} {!rawEdit && ( { onChange(nextIds.join(', ')) }} /> )} ) } export default PostIdsControl