index.js
34 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 | |
| 6 | export default function AuthorsSelect( { label, onChange, value, help } ) { |
| 7 | const authors = useAuthors(); |
| 8 | |
| 9 | const authorOptions = useMemo( () => { |
| 10 | return authors |
| 11 | .reduce( ( result, author ) => { |
| 12 | result.push( { value: author.id, label: author.name } ); |
| 13 | return result; |
| 14 | }, [] ); |
| 15 | }, [ authors ] ); |
| 16 | |
| 17 | const selectedValues = authorOptions.filter( ( option ) => ( value.includes( option.value ) ) ); |
| 18 | |
| 19 | return ( |
| 20 | <AdvancedSelect |
| 21 | id={ 'gblocks-select-author' } |
| 22 | label={ label || __( 'Select authors', 'generateblocks' ) } |
| 23 | help={ help } |
| 24 | placeholder={ label || __( 'Select authors', 'generateblocks' ) } |
| 25 | options={ authorOptions } |
| 26 | isMulti |
| 27 | isSearchable |
| 28 | value={ selectedValues } |
| 29 | onChange={ onChange } |
| 30 | isLoading={ authorOptions.length === 0 } |
| 31 | /> |
| 32 | ); |
| 33 | } |
| 34 |