EntitySelector.tsx
96 lines
| 1 | import { useState } from 'react'; |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import ReactSelect from 'react-select'; |
| 4 | import logo from './givewp-logo.svg'; |
| 5 | import usePostState from "../../hooks/usePostState"; |
| 6 | import {reactSelectStyles, reactSelectThemeStyles} from "./styles/reactSelectStyles"; |
| 7 | import './styles/index.scss'; |
| 8 | import { EmotionStylesProvider } from '@givewp/admin/providers'; |
| 9 | import {useDispatch} from '@wordpress/data'; |
| 10 | |
| 11 | /** |
| 12 | * @since 4.3.0 |
| 13 | */ |
| 14 | export type EntityOption = { |
| 15 | label: string; |
| 16 | value: number | string; |
| 17 | }; |
| 18 | |
| 19 | /** |
| 20 | * @since 4.3.0 |
| 21 | */ |
| 22 | type EntitySelectorProps = { |
| 23 | id: string; |
| 24 | label: string; |
| 25 | options: EntityOption[]; |
| 26 | isLoading: boolean; |
| 27 | emptyMessage: string; |
| 28 | loadingMessage: string; |
| 29 | onConfirm: (id: number | string) => void; |
| 30 | buttonText?: string; |
| 31 | disabled?: boolean; |
| 32 | }; |
| 33 | |
| 34 | /** |
| 35 | * @since 4.3.0 |
| 36 | */ |
| 37 | export default function EntitySelector({ |
| 38 | id, |
| 39 | label, |
| 40 | options, |
| 41 | isLoading, |
| 42 | emptyMessage, |
| 43 | loadingMessage, |
| 44 | onConfirm, |
| 45 | buttonText = __('Confirm', 'give'), |
| 46 | disabled = false, |
| 47 | }: EntitySelectorProps) { |
| 48 | const [selected, setSelected] = useState<number | string | null>(null); |
| 49 | const selectedOption = options.find((opt) => opt.value === selected); |
| 50 | const {isSaving, isDisabled} = usePostState(); |
| 51 | |
| 52 | const {savePost} = useDispatch('core/editor'); |
| 53 | |
| 54 | return ( |
| 55 | <div className="givewp-entity-selector"> |
| 56 | <img className="givewp-entity-selector__logo" src={logo} alt="givewp-logo" /> |
| 57 | <div className="givewp-entity-selector__select"> |
| 58 | <label htmlFor={id} className="givewp-entity-selector__label"> |
| 59 | {label} |
| 60 | </label> |
| 61 | |
| 62 | <EmotionStylesProvider cacheKey="givewp-entity-selector"> |
| 63 | <ReactSelect |
| 64 | name={id} |
| 65 | inputId={id} |
| 66 | value={selectedOption} |
| 67 | //@ts-ignore |
| 68 | onChange={(option) => setSelected(option?.value)} |
| 69 | options={options} |
| 70 | noOptionsMessage={() => <p>{emptyMessage}</p>} |
| 71 | loadingMessage={() => <>{loadingMessage}</>} |
| 72 | isLoading={isLoading} |
| 73 | theme={reactSelectThemeStyles} |
| 74 | styles={reactSelectStyles} |
| 75 | placeholder={isLoading ? loadingMessage : __('Select...', 'give')} |
| 76 | /> |
| 77 | </EmotionStylesProvider> |
| 78 | </div> |
| 79 | |
| 80 | <button |
| 81 | className="givewp-entity-selector__submit" |
| 82 | type="button" |
| 83 | disabled={isSaving || isDisabled || !selected} |
| 84 | onClick={() => { |
| 85 | if (selected) { |
| 86 | onConfirm(selected); |
| 87 | } |
| 88 | savePost(); |
| 89 | }} |
| 90 | > |
| 91 | {buttonText} |
| 92 | </button> |
| 93 | </div> |
| 94 | ); |
| 95 | } |
| 96 |