Selector.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. import { Context } from "shared/Context";
  4. export type SelectorPropsType<T> = {
  5. activeValue: T;
  6. refreshOptions?: () => void;
  7. options: { value: T; label: string; icon?: any }[];
  8. addButton?: boolean;
  9. setActiveValue: (x: T) => void;
  10. width: string;
  11. height?: string;
  12. disabled?: boolean;
  13. dropdownLabel?: string;
  14. dropdownWidth?: string;
  15. dropdownMaxHeight?: string;
  16. closeOverlay?: boolean;
  17. placeholder?: string;
  18. scrollBuffer?: boolean;
  19. disableTooltip?: boolean;
  20. };
  21. type StateType = {};
  22. export default class Selector<T> extends Component<SelectorPropsType<T>, StateType> {
  23. state = {
  24. expanded: false,
  25. showTooltip: false,
  26. };
  27. wrapperRef: any = React.createRef();
  28. parentRef: any = React.createRef();
  29. componentDidMount() {
  30. document.addEventListener("mousedown", this.handleClickOutside.bind(this));
  31. }
  32. componentWillUnmount() {
  33. document.removeEventListener(
  34. "mousedown",
  35. this.handleClickOutside.bind(this)
  36. );
  37. }
  38. handleClickOutside = (event: any) => {
  39. if (
  40. this.wrapperRef &&
  41. this.wrapperRef.current &&
  42. !this.wrapperRef.current.contains(event.target) &&
  43. this.parentRef &&
  44. this.parentRef.current &&
  45. !this.parentRef.current.contains(event.target)
  46. ) {
  47. this.setState({ expanded: false });
  48. }
  49. };
  50. handleOptionClick = (option: { value: string; label: string }) => {
  51. this.props.setActiveValue(option.value);
  52. this.props.closeOverlay ? null : this.setState({ expanded: false });
  53. };
  54. renderOptionList = () => {
  55. let { options, activeValue } = this.props;
  56. return options.map(
  57. (option: { value: string; label: string; icon?: any }, i: number) => {
  58. return (
  59. <Option
  60. key={i}
  61. height={this.props.height}
  62. selected={option.value === activeValue}
  63. onClick={() => this.handleOptionClick(option)}
  64. lastItem={i === options.length - 1}
  65. >
  66. {option.icon && (
  67. <Icon>
  68. <img src={option.icon} />
  69. </Icon>
  70. )}
  71. {option.label}
  72. </Option>
  73. );
  74. }
  75. );
  76. };
  77. renderDropdownLabel = () => {
  78. if (this.props.dropdownLabel && this.props.dropdownLabel !== "") {
  79. return <DropdownLabel>{this.props.dropdownLabel}</DropdownLabel>;
  80. }
  81. };
  82. renderAddButton = () => {
  83. if (this.props.addButton) {
  84. return (
  85. <NewOption
  86. onClick={() => {
  87. this.context.setCurrentModal("NamespaceModal", this.props.options);
  88. }}
  89. >
  90. <Plus>+</Plus>
  91. Add namespace
  92. </NewOption>
  93. );
  94. }
  95. };
  96. renderDropdown = () => {
  97. if (this.state.expanded) {
  98. return (
  99. <DropdownWrapper>
  100. <Dropdown
  101. ref={this.wrapperRef}
  102. dropdownWidth={
  103. this.props.dropdownWidth
  104. ? this.props.dropdownWidth
  105. : this.props.width
  106. }
  107. dropdownMaxHeight={this.props.dropdownMaxHeight}
  108. onClick={() => this.setState({ expanded: false })}
  109. >
  110. {this.renderDropdownLabel()}
  111. {this.renderOptionList()}
  112. {this.renderAddButton()}
  113. </Dropdown>
  114. {this.props.scrollBuffer && <ScrollBuffer />}
  115. </DropdownWrapper>
  116. );
  117. }
  118. };
  119. getLabel = (value: string): any => {
  120. let tgt = this.props.options.find(
  121. (element: { value: string; label: string }) => element.value === value
  122. );
  123. if (tgt) {
  124. return tgt.label;
  125. }
  126. };
  127. renderIcon = () => {
  128. var icon;
  129. this.props.options.forEach((option: any) => {
  130. if (option.icon && option.value === this.props.activeValue) {
  131. icon = option.icon;
  132. }
  133. });
  134. return (
  135. <>
  136. {icon && (
  137. <Icon>
  138. <img src={icon} />
  139. </Icon>
  140. )}
  141. </>
  142. );
  143. };
  144. render() {
  145. let { activeValue } = this.props;
  146. return (
  147. <StyledSelector width={this.props.width}>
  148. <MainSelector
  149. ref={this.parentRef}
  150. disabled={this.props.disabled}
  151. onClick={() => {
  152. if (!this.props.disabled) {
  153. if (this.props.refreshOptions) {
  154. this.props.refreshOptions();
  155. }
  156. this.setState({ expanded: !this.state.expanded });
  157. }
  158. }}
  159. expanded={this.state.expanded}
  160. width={this.props.width}
  161. height={this.props.height}
  162. onMouseEnter={() => this.setState({ showTooltip: true })}
  163. onMouseLeave={() => this.setState({ showTooltip: false })}
  164. >
  165. <Flex>
  166. {this.renderIcon()}
  167. <TextWrap>
  168. {activeValue
  169. ? activeValue === ""
  170. ? "All"
  171. : this.getLabel(activeValue)
  172. : this.props.placeholder}
  173. </TextWrap>
  174. </Flex>
  175. <i className="material-icons">arrow_drop_down</i>
  176. </MainSelector>
  177. {!this.props.disableTooltip && this.state.showTooltip && (
  178. <Tooltip>
  179. {activeValue
  180. ? activeValue === ""
  181. ? "All"
  182. : this.getLabel(activeValue)
  183. : this.props.placeholder}
  184. </Tooltip>
  185. )}
  186. {this.renderDropdown()}
  187. </StyledSelector>
  188. );
  189. }
  190. }
  191. Selector.contextType = Context;
  192. const DropdownWrapper = styled.div`
  193. position: absolute;
  194. width: 100%;
  195. right: 0;
  196. z-index: 1;
  197. top: calc(100% + 5px);
  198. `;
  199. const ScrollBuffer = styled.div`
  200. width: 100%;
  201. height: 50px;
  202. `;
  203. const Flex = styled.div`
  204. display: flex;
  205. align-items: center;
  206. width: 85%;
  207. `;
  208. const Icon = styled.div`
  209. height: 20px;
  210. width: 30px;
  211. margin-left: -5px;
  212. margin-right: 10px;
  213. display: flex;
  214. align-items: center;
  215. justify-content: center;
  216. overflow: visible;
  217. > img {
  218. height: 18px;
  219. width: auto;
  220. }
  221. `;
  222. const Plus = styled.div`
  223. margin-right: 10px;
  224. font-size: 15px;
  225. `;
  226. const TextWrap = styled.div`
  227. white-space: nowrap;
  228. overflow: hidden;
  229. text-overflow: ellipsis;
  230. z-index: 0;
  231. `;
  232. const DropdownLabel = styled.div`
  233. font-size: 13px;
  234. color: #ffffff44;
  235. font-weight: 500;
  236. margin: 10px 13px;
  237. `;
  238. const NewOption = styled.div`
  239. display: flex;
  240. width: 100%;
  241. border-top: 1px solid #00000000;
  242. border-bottom: 1px solid #ffffff00;
  243. height: 37px;
  244. font-size: 13px;
  245. align-items: center;
  246. padding-left: 15px;
  247. cursor: pointer;
  248. padding-right: 10px;
  249. white-space: nowrap;
  250. overflow: hidden;
  251. text-overflow: ellipsis;
  252. :hover {
  253. background: #ffffff22;
  254. }
  255. `;
  256. type OptionProps = {
  257. selected: boolean;
  258. lastItem: boolean;
  259. height: string;
  260. };
  261. const Option = styled.div`
  262. width: 100%;
  263. border-top: 1px solid #00000000;
  264. border-bottom: 1px solid
  265. ${(props: OptionProps) => (props.lastItem ? "#ffffff00" : "#ffffff15")};
  266. height: ${(props: OptionProps) => props.height || "37px"};
  267. font-size: 13px;
  268. align-items: center;
  269. display: flex;
  270. align-items: center;
  271. padding-left: 15px;
  272. cursor: pointer;
  273. padding-right: 10px;
  274. white-space: nowrap;
  275. overflow: hidden;
  276. text-overflow: ellipsis;
  277. background: ${(props: OptionProps) => (props.selected ? "#ffffff11" : "")};
  278. :hover {
  279. background: #ffffff22;
  280. }
  281. `;
  282. const Dropdown = styled.div`
  283. background: #26282f;
  284. width: ${(props: { dropdownWidth: string; dropdownMaxHeight: string }) =>
  285. props.dropdownWidth};
  286. max-height: ${(props: { dropdownWidth: string; dropdownMaxHeight: string }) =>
  287. props.dropdownMaxHeight || "300px"};
  288. border-radius: 3px;
  289. z-index: 999;
  290. overflow-y: auto;
  291. margin-bottom: 20px;
  292. box-shadow: 0 8px 20px 0px #00000088;
  293. `;
  294. const StyledSelector = styled.div<{ width: string }>`
  295. position: relative;
  296. width: ${(props) => props.width};
  297. `;
  298. const MainSelector = styled.div<{
  299. disabled?: boolean;
  300. expanded: boolean;
  301. width: string;
  302. height?: string;
  303. }>`
  304. width: ${props => props.width};
  305. height: ${props => props.height ? props.height : "35px"};
  306. border: 1px solid #ffffff55;
  307. font-size: 13px;
  308. padding: 5px 10px;
  309. padding-left: 15px;
  310. border-radius: 3px;
  311. display: flex;
  312. color: ${props => props.disabled ? "#ffffff44" : "#ffffff"};
  313. justify-content: space-between;
  314. align-items: center;
  315. cursor: ${props => props.disabled ? "not-allowed" : "pointer"};
  316. background: ${props => props.expanded ? "#ffffff33" : props.theme.fg};
  317. :hover {
  318. background: ${props => props.expanded ? "#ffffff33" : (
  319. props.disabled ? "#ffffff11" : "#ffffff22"
  320. )};
  321. }
  322. > i {
  323. font-size: 20px;
  324. transform: ${props => props.expanded ? "rotate(180deg)" : ""};
  325. }
  326. `;
  327. const Tooltip = styled.div`
  328. position: absolute;
  329. left: 5px;
  330. word-wrap: break-word;
  331. top: 40px;
  332. min-height: 18px;
  333. width: fit-content;
  334. padding: 5px 7px;
  335. background: #272731;
  336. z-index: 999;
  337. display: flex;
  338. flex-direction: column;
  339. justify-content: center;
  340. flex: 1;
  341. color: white;
  342. text-transform: none;
  343. font-size: 12px;
  344. font-family: "Work Sans", sans-serif;
  345. outline: 1px solid #ffffff55;
  346. opacity: 0;
  347. animation: faded-in 0.2s 0.15s;
  348. animation-fill-mode: forwards;
  349. @keyframes faded-in {
  350. from {
  351. opacity: 0;
  352. }
  353. to {
  354. opacity: 1;
  355. }
  356. }
  357. `;