index.js
44 lines
| 1 | import AdvancedSelect from '../advanced-select'; |
| 2 | import { useMemo } from '@wordpress/element'; |
| 3 | import { __ } from '@wordpress/i18n'; |
| 4 | import useAuthors from '../../hooks/useAuthors'; |
| 5 | import { applyFilters } from '@wordpress/hooks'; |
| 6 | |
| 7 | export default function AuthorsSelect( props ) { |
| 8 | const { |
| 9 | label, |
| 10 | onChange, |
| 11 | value, |
| 12 | help, |
| 13 | filterName = 'generateblocks.editor.authors-select', |
| 14 | } = props; |
| 15 | const authors = useAuthors(); |
| 16 | |
| 17 | const authorOptions = useMemo( () => { |
| 18 | const options = authors |
| 19 | .reduce( ( result, author ) => { |
| 20 | result.push( { value: author.id, label: author.name } ); |
| 21 | return result; |
| 22 | }, [] ); |
| 23 | |
| 24 | return applyFilters( filterName, options ); |
| 25 | }, [ authors ] ); |
| 26 | |
| 27 | const selectedValues = authorOptions.filter( ( option ) => ( value.includes( option.value ) ) ); |
| 28 | |
| 29 | return ( |
| 30 | <AdvancedSelect |
| 31 | id={ 'gblocks-select-author' } |
| 32 | label={ label || __( 'Select authors', 'generateblocks' ) } |
| 33 | help={ help } |
| 34 | placeholder={ label || __( 'Select authors', 'generateblocks' ) } |
| 35 | options={ authorOptions } |
| 36 | isMulti |
| 37 | isSearchable |
| 38 | value={ selectedValues } |
| 39 | onChange={ onChange } |
| 40 | isLoading={ authorOptions.length === 0 } |
| 41 | /> |
| 42 | ); |
| 43 | } |
| 44 |