arithmeticInput.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. import {createRef, Fragment, PureComponent} from 'react';
  2. import styled from '@emotion/styled';
  3. import isEqual from 'lodash/isEqual';
  4. import type {InputProps} from 'sentry/components/input';
  5. import Input from 'sentry/components/input';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import type {Column} from 'sentry/utils/discover/fields';
  9. import {generateFieldAsString, isLegalEquationColumn} from 'sentry/utils/discover/fields';
  10. const NONE_SELECTED = -1;
  11. type DropdownOption = {
  12. active: boolean;
  13. kind: 'field' | 'operator';
  14. value: string;
  15. };
  16. type DropdownOptionGroup = {
  17. options: DropdownOption[];
  18. title: string;
  19. };
  20. type DefaultProps = {
  21. options: Column[];
  22. };
  23. type Props = DefaultProps &
  24. InputProps & {
  25. onUpdate: (value: string) => void;
  26. value: string;
  27. hideFieldOptions?: boolean;
  28. };
  29. type State = {
  30. activeSelection: number;
  31. dropdownOptionGroups: DropdownOptionGroup[];
  32. dropdownVisible: boolean;
  33. partialTerm: string | null;
  34. query: string;
  35. rawOptions: Column[];
  36. };
  37. export default class ArithmeticInput extends PureComponent<Props, State> {
  38. static defaultProps: DefaultProps = {
  39. options: [],
  40. };
  41. static getDerivedStateFromProps(props: Readonly<Props>, state: State): State {
  42. const changed = !isEqual(state.rawOptions, props.options);
  43. if (changed) {
  44. return {
  45. ...state,
  46. rawOptions: props.options,
  47. dropdownOptionGroups: makeOptions(
  48. props.options,
  49. state.partialTerm,
  50. props.hideFieldOptions
  51. ),
  52. activeSelection: NONE_SELECTED,
  53. };
  54. }
  55. return {...state};
  56. }
  57. state: State = {
  58. query: this.props.value,
  59. partialTerm: null,
  60. rawOptions: this.props.options,
  61. dropdownVisible: false,
  62. dropdownOptionGroups: makeOptions(
  63. this.props.options,
  64. null,
  65. this.props.hideFieldOptions
  66. ),
  67. activeSelection: NONE_SELECTED,
  68. };
  69. input = createRef<HTMLInputElement>();
  70. blur = () => {
  71. this.input.current?.blur();
  72. };
  73. focus = (position: number) => {
  74. this.input.current?.focus();
  75. this.input.current?.setSelectionRange(position, position);
  76. };
  77. getCursorPosition(): number {
  78. return this.input.current?.selectionStart ?? -1;
  79. }
  80. splitQuery() {
  81. const {query} = this.state;
  82. const currentPosition = this.getCursorPosition();
  83. // The current term is delimited by whitespaces. So if no spaces are found,
  84. // the entire string is taken to be 1 term.
  85. //
  86. // TODO: add support for when there are no spaces
  87. const matches = [...query.substring(0, currentPosition).matchAll(/\s|^/g)];
  88. const match = matches[matches.length - 1];
  89. const startOfTerm = match[0] === '' ? 0 : (match.index || 0) + 1;
  90. const cursorOffset = query.slice(currentPosition).search(/\s|$/);
  91. const endOfTerm = currentPosition + (cursorOffset === -1 ? 0 : cursorOffset);
  92. return {
  93. startOfTerm,
  94. endOfTerm,
  95. prefix: query.substring(0, startOfTerm),
  96. term: query.substring(startOfTerm, endOfTerm),
  97. suffix: query.substring(endOfTerm),
  98. };
  99. }
  100. handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  101. const query = event.target.value.replace('\n', '');
  102. this.setState({query}, this.updateAutocompleteOptions);
  103. };
  104. handleClick = () => {
  105. this.updateAutocompleteOptions();
  106. };
  107. handleFocus = () => {
  108. this.setState({dropdownVisible: true});
  109. };
  110. handleBlur = () => {
  111. this.props.onUpdate(this.state.query);
  112. this.setState({dropdownVisible: false});
  113. };
  114. getSelection(selection: number): DropdownOption | null {
  115. const {dropdownOptionGroups} = this.state;
  116. for (const group of dropdownOptionGroups) {
  117. if (selection >= group.options.length) {
  118. selection -= group.options.length;
  119. continue;
  120. }
  121. return group.options[selection];
  122. }
  123. return null;
  124. }
  125. handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
  126. const {key} = event;
  127. const {options, hideFieldOptions} = this.props;
  128. const {activeSelection, partialTerm} = this.state;
  129. const startedSelection = activeSelection >= 0;
  130. // handle arrow navigation
  131. if (key === 'ArrowDown' || key === 'ArrowUp') {
  132. event.preventDefault();
  133. const newOptionGroups = makeOptions(options, partialTerm, hideFieldOptions);
  134. const flattenedOptions = newOptionGroups.map(group => group.options).flat();
  135. if (flattenedOptions.length === 0) {
  136. return;
  137. }
  138. let newSelection;
  139. if (!startedSelection) {
  140. newSelection = key === 'ArrowUp' ? flattenedOptions.length - 1 : 0;
  141. } else {
  142. newSelection =
  143. key === 'ArrowUp'
  144. ? (activeSelection - 1 + flattenedOptions.length) % flattenedOptions.length
  145. : (activeSelection + 1) % flattenedOptions.length;
  146. }
  147. // This is modifying the `active` value of the references so make sure to
  148. // use `newOptionGroups` at the end.
  149. flattenedOptions[newSelection].active = true;
  150. this.setState({
  151. activeSelection: newSelection,
  152. dropdownOptionGroups: newOptionGroups,
  153. });
  154. return;
  155. }
  156. // handle selection
  157. if (startedSelection && (key === 'Tab' || key === 'Enter')) {
  158. event.preventDefault();
  159. const selection = this.getSelection(activeSelection);
  160. if (selection) {
  161. this.handleSelect(selection);
  162. }
  163. return;
  164. }
  165. if (key === 'Enter') {
  166. this.blur();
  167. return;
  168. }
  169. if (key === 'ArrowLeft' || key === 'ArrowRight') {
  170. this.updateAutocompleteOptions();
  171. }
  172. };
  173. handleKeyUp = (event: React.KeyboardEvent<HTMLInputElement>) => {
  174. // Other keys are managed at handleKeyDown function
  175. if (event.key !== 'Escape') {
  176. return;
  177. }
  178. event.preventDefault();
  179. const {activeSelection} = this.state;
  180. const startedSelection = activeSelection >= 0;
  181. if (!startedSelection) {
  182. this.blur();
  183. return;
  184. }
  185. };
  186. handleSelect = (option: DropdownOption) => {
  187. const {prefix, suffix} = this.splitQuery();
  188. this.setState(
  189. {
  190. // make sure to insert a space after the autocompleted term
  191. query: `${prefix}${option.value} ${suffix}`,
  192. activeSelection: NONE_SELECTED,
  193. },
  194. () => {
  195. // updating the query will cause the input to lose focus
  196. // and make sure to move the cursor behind the space after
  197. // the end of the autocompleted term
  198. this.focus(prefix.length + option.value.length + 1);
  199. this.updateAutocompleteOptions();
  200. }
  201. );
  202. };
  203. updateAutocompleteOptions() {
  204. const {options, hideFieldOptions} = this.props;
  205. const {term} = this.splitQuery();
  206. const partialTerm = term || null;
  207. this.setState({
  208. dropdownOptionGroups: makeOptions(options, partialTerm, hideFieldOptions),
  209. partialTerm,
  210. });
  211. }
  212. render() {
  213. const {onUpdate: _onUpdate, options: _options, ...props} = this.props;
  214. const {dropdownVisible, dropdownOptionGroups} = this.state;
  215. return (
  216. <Container isOpen={dropdownVisible}>
  217. <Input
  218. {...props}
  219. ref={this.input}
  220. autoComplete="off"
  221. className="form-control"
  222. value={this.state.query}
  223. onClick={this.handleClick}
  224. onChange={this.handleChange}
  225. onBlur={this.handleBlur}
  226. onFocus={this.handleFocus}
  227. onKeyDown={this.handleKeyDown}
  228. spellCheck={false}
  229. />
  230. <TermDropdown
  231. isOpen={dropdownVisible}
  232. optionGroups={dropdownOptionGroups}
  233. handleSelect={this.handleSelect}
  234. />
  235. </Container>
  236. );
  237. }
  238. }
  239. const Container = styled('div')<{isOpen: boolean}>`
  240. background: ${p => p.theme.background};
  241. position: relative;
  242. border-radius: ${p =>
  243. p.isOpen
  244. ? `${p.theme.borderRadius} ${p.theme.borderRadius} 0 0`
  245. : p.theme.borderRadius};
  246. .show-sidebar & {
  247. background: ${p => p.theme.backgroundSecondary};
  248. }
  249. `;
  250. type TermDropdownProps = {
  251. handleSelect: (option: DropdownOption) => void;
  252. isOpen: boolean;
  253. optionGroups: DropdownOptionGroup[];
  254. };
  255. function TermDropdown({isOpen, optionGroups, handleSelect}: TermDropdownProps) {
  256. return (
  257. <DropdownContainer isOpen={isOpen}>
  258. {isOpen && (
  259. <DropdownItemsList>
  260. {optionGroups.map(group => {
  261. const {title, options} = group;
  262. return (
  263. <Fragment key={title}>
  264. <ListItem>
  265. <DropdownTitle>{title}</DropdownTitle>
  266. </ListItem>
  267. {options.map(option => {
  268. return (
  269. <DropdownListItem
  270. key={option.value}
  271. className={option.active ? 'active' : undefined}
  272. onClick={() => handleSelect(option)}
  273. // prevent the blur event on the input from firing
  274. onMouseDown={event => event.preventDefault()}
  275. // scroll into view if it is the active element
  276. ref={element =>
  277. option.active && element?.scrollIntoView?.({block: 'nearest'})
  278. }
  279. aria-label={option.value}
  280. >
  281. <DropdownItemTitleWrapper>{option.value}</DropdownItemTitleWrapper>
  282. </DropdownListItem>
  283. );
  284. })}
  285. {options.length === 0 && <Info>{t('No items found')}</Info>}
  286. </Fragment>
  287. );
  288. })}
  289. </DropdownItemsList>
  290. )}
  291. </DropdownContainer>
  292. );
  293. }
  294. function makeFieldOptions(
  295. columns: Column[],
  296. partialTerm: string | null
  297. ): DropdownOptionGroup {
  298. const fieldValues = new Set<string>();
  299. const options = columns
  300. .filter(({kind}) => kind !== 'equation')
  301. .filter(isLegalEquationColumn)
  302. .map(option => ({
  303. kind: 'field' as const,
  304. active: false,
  305. value: generateFieldAsString(option),
  306. }))
  307. .filter(({value}) => {
  308. if (fieldValues.has(value)) {
  309. return false;
  310. }
  311. fieldValues.add(value);
  312. return true;
  313. })
  314. .filter(({value}) => (partialTerm ? value.includes(partialTerm) : true));
  315. return {
  316. title: 'Fields',
  317. options,
  318. };
  319. }
  320. function makeOperatorOptions(partialTerm: string | null): DropdownOptionGroup {
  321. const options = ['+', '-', '*', '/', '(', ')']
  322. .filter(operator => (partialTerm ? operator.includes(partialTerm) : true))
  323. .map(operator => ({
  324. kind: 'operator' as const,
  325. active: false,
  326. value: operator,
  327. }));
  328. return {
  329. title: 'Operators',
  330. options,
  331. };
  332. }
  333. function makeOptions(
  334. columns: Column[],
  335. partialTerm: string | null,
  336. hideFieldOptions?: boolean
  337. ): DropdownOptionGroup[] {
  338. if (hideFieldOptions) {
  339. return [makeOperatorOptions(partialTerm)];
  340. }
  341. return [makeFieldOptions(columns, partialTerm), makeOperatorOptions(partialTerm)];
  342. }
  343. const DropdownContainer = styled('div')<{isOpen: boolean}>`
  344. /* Container has a border that we need to account for */
  345. display: ${p => (p.isOpen ? 'block' : 'none')};
  346. position: absolute;
  347. top: 100%;
  348. left: -1px;
  349. right: -1px;
  350. z-index: ${p => p.theme.zIndex.dropdown};
  351. background: ${p => p.theme.backgroundElevated};
  352. box-shadow: ${p => p.theme.dropShadowHeavy};
  353. border: 1px solid ${p => p.theme.border};
  354. border-radius: ${p => p.theme.borderRadius};
  355. margin-top: ${space(1)};
  356. max-height: 300px;
  357. overflow-y: auto;
  358. `;
  359. const DropdownItemsList = styled('ul')`
  360. padding-left: 0;
  361. list-style: none;
  362. margin-bottom: 0;
  363. `;
  364. const ListItem = styled('li')`
  365. &:not(:last-child) {
  366. border-bottom: 1px solid ${p => p.theme.innerBorder};
  367. }
  368. `;
  369. const DropdownTitle = styled('header')`
  370. display: flex;
  371. align-items: center;
  372. background-color: ${p => p.theme.backgroundSecondary};
  373. color: ${p => p.theme.gray300};
  374. font-weight: normal;
  375. font-size: ${p => p.theme.fontSizeMedium};
  376. margin: 0;
  377. padding: ${space(1)} ${space(2)};
  378. & > svg {
  379. margin-right: ${space(1)};
  380. }
  381. `;
  382. const DropdownListItem = styled(ListItem)`
  383. scroll-margin: 40px 0;
  384. font-size: ${p => p.theme.fontSizeLarge};
  385. padding: ${space(1)} ${space(2)};
  386. cursor: pointer;
  387. &:hover,
  388. &.active {
  389. background: ${p => p.theme.hover};
  390. }
  391. `;
  392. const DropdownItemTitleWrapper = styled('div')`
  393. color: ${p => p.theme.textColor};
  394. font-weight: normal;
  395. font-size: ${p => p.theme.fontSizeMedium};
  396. margin: 0;
  397. line-height: ${p => p.theme.text.lineHeightHeading};
  398. ${p => p.theme.overflowEllipsis};
  399. `;
  400. const Info = styled('div')`
  401. display: flex;
  402. padding: ${space(1)} ${space(2)};
  403. font-size: ${p => p.theme.fontSizeLarge};
  404. color: ${p => p.theme.gray300};
  405. &:not(:last-child) {
  406. border-bottom: 1px solid ${p => p.theme.innerBorder};
  407. }
  408. `;