ColorPopup.js
2 years ago
EntitySearchDropdown.js
2 years ago
LoadSelect.js
5 years ago
ProBadge.js
5 years ago
SelectMediaDropdown.js
2 years ago
UrlSelect.js
2 years ago
VideoIcon.js
2 years ago
EntitySearchDropdown.js
164 lines
| 1 | import { |
| 2 | Spinner, |
| 3 | Button, |
| 4 | MenuGroup, |
| 5 | MenuItem, |
| 6 | Dropdown, |
| 7 | Disabled, |
| 8 | } from "@wordpress/components"; |
| 9 | import { __ } from "@wordpress/i18n"; |
| 10 | import { SearchControl } from "@wordpress/components"; |
| 11 | import { css } from "@emotion/core"; |
| 12 | |
| 13 | const EntitySearchDropdown = ({ |
| 14 | options, |
| 15 | isLoading, |
| 16 | hasMore, |
| 17 | search, |
| 18 | onSearch, |
| 19 | onSelect, |
| 20 | onNextPage, |
| 21 | onCreate, |
| 22 | onOpen, |
| 23 | renderItem = null, |
| 24 | ...dropdownProps |
| 25 | }) => { |
| 26 | const renderContent = () => { |
| 27 | if (isLoading && !options.length) return <Spinner />; |
| 28 | if (!options.length) { |
| 29 | return ( |
| 30 | <> |
| 31 | <span |
| 32 | css={css` |
| 33 | margin: 8px; |
| 34 | `} |
| 35 | > |
| 36 | {__("None found.", "presto-player")} |
| 37 | </span> |
| 38 | </> |
| 39 | ); |
| 40 | } |
| 41 | return ( |
| 42 | <> |
| 43 | {!!onCreate && ( |
| 44 | <MenuGroup> |
| 45 | <MenuItem |
| 46 | icon={ |
| 47 | <svg |
| 48 | xmlns="http://www.w3.org/2000/svg" |
| 49 | viewBox="0 0 20 20" |
| 50 | fill="currentColor" |
| 51 | css={css` |
| 52 | width: 18px; |
| 53 | `} |
| 54 | > |
| 55 | <path d="M10.75 4.75a.75.75 0 0 0-1.5 0v4.5h-4.5a.75.75 0 0 0 0 1.5h4.5v4.5a.75.75 0 0 0 1.5 0v-4.5h4.5a.75.75 0 0 0 0-1.5h-4.5v-4.5Z" /> |
| 56 | </svg> |
| 57 | } |
| 58 | iconPosition="left" |
| 59 | onClick={onCreate} |
| 60 | > |
| 61 | {__("Add New", "presto-player")} |
| 62 | </MenuItem> |
| 63 | </MenuGroup> |
| 64 | )} |
| 65 | |
| 66 | {!options.length && search ? ( |
| 67 | <Disabled> |
| 68 | <MenuItem>{__("None found.", "presto-player")}</MenuItem> |
| 69 | </Disabled> |
| 70 | ) : ( |
| 71 | <MenuGroup> |
| 72 | {(options || []).map((item) => { |
| 73 | if (renderItem) return renderItem({ item, onSelect }); |
| 74 | return ( |
| 75 | <MenuItem |
| 76 | icon={item?.icon} |
| 77 | iconPosition="left" |
| 78 | onClick={() => onSelect(item)} |
| 79 | {...item} |
| 80 | > |
| 81 | {item?.title || "Untitled"} |
| 82 | </MenuItem> |
| 83 | ); |
| 84 | })} |
| 85 | </MenuGroup> |
| 86 | )} |
| 87 | {hasMore && options.length && ( |
| 88 | <div |
| 89 | css={css` |
| 90 | margin-top: 20px; |
| 91 | text-align: center; |
| 92 | display: flex; |
| 93 | justify-content: center; |
| 94 | width: 100%; |
| 95 | `} |
| 96 | > |
| 97 | <Button |
| 98 | variant="secondary" |
| 99 | size={"small"} |
| 100 | onClick={onNextPage} |
| 101 | isBusy={isLoading} |
| 102 | > |
| 103 | {__("Load More", "presto-player")} |
| 104 | </Button> |
| 105 | </div> |
| 106 | )} |
| 107 | </> |
| 108 | ); |
| 109 | }; |
| 110 | |
| 111 | return ( |
| 112 | <div |
| 113 | class="pp_search_dropdown_container" |
| 114 | css={css` |
| 115 | width: 100%; |
| 116 | position: relative; |
| 117 | `} |
| 118 | > |
| 119 | <Dropdown |
| 120 | onToggle={(open) => onOpen(open)} |
| 121 | renderContent={() => ( |
| 122 | <div |
| 123 | css={css` |
| 124 | width: 500px; |
| 125 | max-width: 100vw; |
| 126 | .components-menu-group { |
| 127 | padding: 8px; |
| 128 | margin-top: 0; |
| 129 | margin-bottom: 0; |
| 130 | margin-left: -8px; |
| 131 | margin-right: -8px; |
| 132 | } |
| 133 | .components-menu-group + .components-menu-group { |
| 134 | margin-top: 0; |
| 135 | border-top: 1px solid #ccc; |
| 136 | padding: 8px; |
| 137 | } |
| 138 | .components-menu-group:last-child { |
| 139 | margin-bottom: -8px; |
| 140 | } |
| 141 | .components-menu-group:first-child { |
| 142 | margin-top: -8px; |
| 143 | } |
| 144 | `} |
| 145 | > |
| 146 | <SearchControl |
| 147 | placeholder={__("Search...", "presto-player")} |
| 148 | value={search} |
| 149 | onChange={onSearch} |
| 150 | css={css` |
| 151 | padding: 0.5em 0.5em 0em 0.5em; |
| 152 | `} |
| 153 | /> |
| 154 | {renderContent()} |
| 155 | </div> |
| 156 | )} |
| 157 | {...dropdownProps} |
| 158 | /> |
| 159 | </div> |
| 160 | ); |
| 161 | }; |
| 162 | |
| 163 | export default EntitySearchDropdown; |
| 164 |