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