index.tsx
78 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { AsyncPaginate } from 'react-select-async-paginate'; |
| 3 | import { AdminSectionField } from '@givewp/components/AdminDetailsPage/AdminSection'; |
| 4 | import { SelectOption } from '@givewp/admin/types'; |
| 5 | |
| 6 | import styles from './styles.module.scss'; |
| 7 | |
| 8 | |
| 9 | /** |
| 10 | * @since 4.11.0 |
| 11 | */ |
| 12 | export default function AsyncSelectOption({ |
| 13 | name, |
| 14 | label, |
| 15 | description = '', |
| 16 | selectedOption, |
| 17 | loadOptions, |
| 18 | mapOptionsForMenu, |
| 19 | handleChange, |
| 20 | isLoadingError, |
| 21 | errorMessage, |
| 22 | searchPlaceholder, |
| 23 | loadingMessage, |
| 24 | loadingError, |
| 25 | ariaLabel, |
| 26 | noOptionsMessage, |
| 27 | children |
| 28 | }: AsyncSelectOptionProps) { |
| 29 | return ( |
| 30 | <AdminSectionField error={errorMessage}> |
| 31 | <label htmlFor={name}>{label}</label> |
| 32 | {description && <p>{description}</p>} |
| 33 | {isLoadingError ? ( |
| 34 | <div role="alert" style={{ color: 'var(--givewp-red-500)', fontSize: '0.875rem' }}> |
| 35 | {loadingError} |
| 36 | </div> |
| 37 | ) : ( |
| 38 | <AsyncPaginate |
| 39 | inputId={name} |
| 40 | className={styles.searchableSelect} |
| 41 | classNamePrefix="searchableSelect" |
| 42 | value={selectedOption} |
| 43 | loadOptions={loadOptions} |
| 44 | mapOptionsForMenu={mapOptionsForMenu} |
| 45 | onChange={handleChange} |
| 46 | debounceTimeout={600} |
| 47 | placeholder={searchPlaceholder} |
| 48 | loadingMessage={() => loadingMessage} |
| 49 | noOptionsMessage={() => noOptionsMessage} |
| 50 | aria-label={ariaLabel} |
| 51 | /> |
| 52 | )} |
| 53 | {children} |
| 54 | </AdminSectionField> |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | interface AsyncSelectOptionProps { |
| 59 | name: string; |
| 60 | label: string; |
| 61 | description?: string; |
| 62 | handleChange: (selectedOption: SelectOption) => void; |
| 63 | selectedOption: SelectOption | null; |
| 64 | loadOptions: (searchInput: string) => Promise<{ |
| 65 | options: SelectOption[]; |
| 66 | hasMore: boolean; |
| 67 | }>; |
| 68 | mapOptionsForMenu: (options: SelectOption[]) => SelectOption[]; |
| 69 | isLoadingError: Error | null; |
| 70 | errorMessage: string; |
| 71 | searchPlaceholder: string; |
| 72 | loadingMessage: string; |
| 73 | loadingError: string, |
| 74 | noOptionsMessage: string; |
| 75 | ariaLabel: string; |
| 76 | children?: JSX.Element | JSX.Element[]; |
| 77 | } |
| 78 |