queryField.tsx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. import {Component, createRef} from 'react';
  2. import type {SingleValueProps} from 'react-select';
  3. import {components} from 'react-select';
  4. import styled from '@emotion/styled';
  5. import cloneDeep from 'lodash/cloneDeep';
  6. import Tag from 'sentry/components/badge/tag';
  7. import type {ControlProps} from 'sentry/components/forms/controls/selectControl';
  8. import SelectControl from 'sentry/components/forms/controls/selectControl';
  9. import type {InputProps} from 'sentry/components/input';
  10. import Input from 'sentry/components/input';
  11. import {Tooltip} from 'sentry/components/tooltip';
  12. import {IconWarning} from 'sentry/icons';
  13. import {t} from 'sentry/locale';
  14. import {pulse} from 'sentry/styles/animations';
  15. import {space} from 'sentry/styles/space';
  16. import type {SelectValue} from 'sentry/types/core';
  17. import type {
  18. AggregateParameter,
  19. AggregationKeyWithAlias,
  20. Column,
  21. ColumnType,
  22. QueryFieldValue,
  23. ValidateColumnTypes,
  24. } from 'sentry/utils/discover/fields';
  25. import {AGGREGATIONS, DEPRECATED_FIELDS} from 'sentry/utils/discover/fields';
  26. import {SESSIONS_OPERATIONS} from 'sentry/views/dashboards/widgetBuilder/releaseWidget/fields';
  27. import ArithmeticInput from './arithmeticInput';
  28. import type {FieldValue, FieldValueColumns} from './types';
  29. import {FieldValueKind} from './types';
  30. export type FieldValueOption = SelectValue<FieldValue>;
  31. type FieldOptions = Record<string, FieldValueOption>;
  32. // Intermediate type that combines the current column
  33. // data with the AggregateParameter type.
  34. type ParameterDescription =
  35. | {
  36. dataType: ColumnType;
  37. kind: 'value';
  38. required: boolean;
  39. value: string;
  40. placeholder?: string;
  41. }
  42. | {
  43. kind: 'column';
  44. options: FieldValueOption[];
  45. required: boolean;
  46. value: FieldValue | null;
  47. }
  48. | {
  49. dataType: string;
  50. kind: 'dropdown';
  51. options: SelectValue<string>[];
  52. required: boolean;
  53. value: string;
  54. placeholder?: string;
  55. };
  56. type Props = {
  57. fieldOptions: FieldOptions;
  58. fieldValue: QueryFieldValue;
  59. onChange: (fieldValue: QueryFieldValue) => void;
  60. className?: string;
  61. disabled?: boolean;
  62. error?: string;
  63. /**
  64. * Function to filter the options that are used as parameters for function/aggregate.
  65. */
  66. filterAggregateParameters?: (
  67. option: FieldValueOption,
  68. fieldValue?: QueryFieldValue
  69. ) => boolean;
  70. /**
  71. * Filter the options in the primary selector. Useful if you only want to
  72. * show a subset of selectable items.
  73. *
  74. * NOTE: This is different from passing an already filtered fieldOptions
  75. * list, as tag items in the list may be used as parameters to functions.
  76. */
  77. filterPrimaryOptions?: (option: FieldValueOption) => boolean;
  78. /**
  79. * The number of columns to render. Columns that do not have a parameter will
  80. * render an empty parameter placeholder. Leave blank to avoid adding spacers.
  81. */
  82. gridColumns?: number;
  83. hideParameterSelector?: boolean;
  84. hidePrimarySelector?: boolean;
  85. /**
  86. * Whether or not to add labels inside of the input fields, currently only
  87. * used for the metric alert builder.
  88. */
  89. inFieldLabels?: boolean;
  90. /**
  91. * This will be displayed in the select if there are no fields
  92. */
  93. noFieldsMessage?: string;
  94. otherColumns?: Column[];
  95. placeholder?: string;
  96. /**
  97. * Whether or not to add the tag explaining the FieldValueKind of each field
  98. */
  99. shouldRenderTag?: boolean;
  100. skipParameterPlaceholder?: boolean;
  101. takeFocus?: boolean;
  102. };
  103. // Type for completing generics in react-select
  104. type OptionType = {
  105. label: string;
  106. value: FieldValue;
  107. };
  108. class QueryField extends Component<Props> {
  109. FieldSelectComponents = {
  110. SingleValue: ({data, ...props}: SingleValueProps<OptionType>) => {
  111. return (
  112. <components.SingleValue data={data} {...props}>
  113. <span data-test-id="label">{data.label}</span>
  114. {data.value && this.renderTag(data.value.kind, data.label)}
  115. </components.SingleValue>
  116. );
  117. },
  118. };
  119. FieldSelectStyles = {
  120. singleValue(provided: React.CSSProperties) {
  121. const custom = {
  122. display: 'flex',
  123. justifyContent: 'space-between',
  124. alignItems: 'center',
  125. };
  126. return {...provided, ...custom};
  127. },
  128. };
  129. handleFieldChange = (selected?: FieldValueOption | null) => {
  130. if (!selected) {
  131. return;
  132. }
  133. const {value} = selected;
  134. const current = this.props.fieldValue;
  135. let fieldValue: QueryFieldValue = cloneDeep(this.props.fieldValue);
  136. switch (value.kind) {
  137. case FieldValueKind.TAG:
  138. case FieldValueKind.MEASUREMENT:
  139. case FieldValueKind.CUSTOM_MEASUREMENT:
  140. case FieldValueKind.BREAKDOWN:
  141. case FieldValueKind.FIELD:
  142. fieldValue = {kind: 'field', field: value.meta.name};
  143. break;
  144. case FieldValueKind.NUMERIC_METRICS:
  145. fieldValue = {
  146. kind: 'calculatedField',
  147. field: value.meta.name,
  148. };
  149. break;
  150. case FieldValueKind.FUNCTION:
  151. if (current.kind === 'function') {
  152. fieldValue = {
  153. kind: 'function',
  154. function: [
  155. value.meta.name as AggregationKeyWithAlias,
  156. current.function[1],
  157. current.function[2],
  158. current.function[3],
  159. ],
  160. };
  161. } else {
  162. fieldValue = {
  163. kind: 'function',
  164. function: [
  165. value.meta.name as AggregationKeyWithAlias,
  166. '',
  167. undefined,
  168. undefined,
  169. ],
  170. };
  171. }
  172. break;
  173. case FieldValueKind.EQUATION:
  174. fieldValue = {
  175. kind: 'equation',
  176. field: value.meta.name,
  177. alias: value.meta.name,
  178. };
  179. break;
  180. default:
  181. throw new Error('Invalid field type found in column picker');
  182. }
  183. if (value.kind === FieldValueKind.FUNCTION) {
  184. value.meta.parameters.forEach((param: AggregateParameter, i: number) => {
  185. if (fieldValue.kind !== 'function') {
  186. return;
  187. }
  188. if (param.kind === 'column') {
  189. const field = this.getFieldOrTagOrMeasurementValue(fieldValue.function[i + 1]);
  190. if (field === null) {
  191. fieldValue.function[i + 1] = param.defaultValue || '';
  192. } else if (
  193. (field.kind === FieldValueKind.FIELD ||
  194. field.kind === FieldValueKind.TAG ||
  195. field.kind === FieldValueKind.MEASUREMENT ||
  196. field.kind === FieldValueKind.CUSTOM_MEASUREMENT ||
  197. field.kind === FieldValueKind.METRICS ||
  198. field.kind === FieldValueKind.BREAKDOWN) &&
  199. validateColumnTypes(param.columnTypes as ValidateColumnTypes, field)
  200. ) {
  201. // New function accepts current field.
  202. fieldValue.function[i + 1] = field.meta.name;
  203. } else {
  204. // field does not fit within new function requirements, use the default.
  205. fieldValue.function[i + 1] = param.defaultValue || '';
  206. fieldValue.function[i + 2] = undefined;
  207. fieldValue.function[i + 3] = undefined;
  208. }
  209. } else {
  210. fieldValue.function[i + 1] = param.defaultValue || '';
  211. }
  212. });
  213. if (fieldValue.kind === 'function') {
  214. if (value.meta.parameters.length === 0) {
  215. fieldValue.function = [fieldValue.function[0], '', undefined, undefined];
  216. } else if (value.meta.parameters.length === 1) {
  217. fieldValue.function[2] = undefined;
  218. fieldValue.function[3] = undefined;
  219. } else if (value.meta.parameters.length === 2) {
  220. fieldValue.function[3] = undefined;
  221. }
  222. }
  223. }
  224. this.triggerChange(fieldValue);
  225. };
  226. handleEquationChange = (value: string) => {
  227. const newColumn = cloneDeep(this.props.fieldValue);
  228. if (newColumn.kind === FieldValueKind.EQUATION) {
  229. newColumn.field = value;
  230. }
  231. this.triggerChange(newColumn);
  232. };
  233. handleFieldParameterChange = ({value}) => {
  234. const newColumn = cloneDeep(this.props.fieldValue);
  235. if (newColumn.kind === 'function') {
  236. newColumn.function[1] = value.meta.name;
  237. }
  238. this.triggerChange(newColumn);
  239. };
  240. handleDropdownParameterChange = (index: number) => {
  241. return (value: SelectValue<string>) => {
  242. const newColumn = cloneDeep(this.props.fieldValue);
  243. if (newColumn.kind === 'function') {
  244. newColumn.function[index] = value.value;
  245. }
  246. this.triggerChange(newColumn);
  247. };
  248. };
  249. handleScalarParameterChange = (index: number) => {
  250. return (value: string) => {
  251. const newColumn = cloneDeep(this.props.fieldValue);
  252. if (newColumn.kind === 'function') {
  253. newColumn.function[index] = value;
  254. }
  255. this.triggerChange(newColumn);
  256. };
  257. };
  258. triggerChange(fieldValue: QueryFieldValue) {
  259. this.props.onChange(fieldValue);
  260. }
  261. getFieldOrTagOrMeasurementValue(
  262. name: string | undefined,
  263. functions: string[] = []
  264. ): FieldValue | null {
  265. const {fieldOptions} = this.props;
  266. if (name === undefined) {
  267. return null;
  268. }
  269. const fieldName = `field:${name}`;
  270. if (fieldOptions[fieldName]) {
  271. return fieldOptions[fieldName].value;
  272. }
  273. const measurementName = `measurement:${name}`;
  274. if (fieldOptions[measurementName]) {
  275. return fieldOptions[measurementName].value;
  276. }
  277. const spanOperationBreakdownName = `span_op_breakdown:${name}`;
  278. if (fieldOptions[spanOperationBreakdownName]) {
  279. return fieldOptions[spanOperationBreakdownName].value;
  280. }
  281. const equationName = `equation:${name}`;
  282. if (fieldOptions[equationName]) {
  283. return fieldOptions[equationName].value;
  284. }
  285. const tagName =
  286. name.indexOf('tags[') === 0
  287. ? `tag:${name.replace(/tags\[(.*?)\]/, '$1')}`
  288. : `tag:${name}`;
  289. if (fieldOptions[tagName]) {
  290. return fieldOptions[tagName].value;
  291. }
  292. if (name.length > 0) {
  293. // Custom Measurement. Probably not appearing in field options because
  294. // no metrics found within selected time range
  295. if (name.startsWith('measurements.')) {
  296. return {
  297. kind: FieldValueKind.CUSTOM_MEASUREMENT,
  298. meta: {
  299. name,
  300. dataType: 'number',
  301. functions,
  302. },
  303. };
  304. }
  305. // Likely a tag that was deleted but left behind in a saved query
  306. // Cook up a tag option so select control works.
  307. return {
  308. kind: FieldValueKind.TAG,
  309. meta: {
  310. name,
  311. dataType: 'string',
  312. unknown: true,
  313. },
  314. };
  315. }
  316. return null;
  317. }
  318. getFieldData() {
  319. let field: FieldValue | null = null;
  320. const {fieldValue} = this.props;
  321. let {fieldOptions} = this.props;
  322. if (fieldValue?.kind === 'function') {
  323. const funcName = `function:${fieldValue.function[0]}`;
  324. if (fieldOptions[funcName] !== undefined) {
  325. field = fieldOptions[funcName].value;
  326. }
  327. }
  328. if (fieldValue?.kind === 'field' || fieldValue?.kind === 'calculatedField') {
  329. field = this.getFieldOrTagOrMeasurementValue(fieldValue.field);
  330. fieldOptions = appendFieldIfUnknown(fieldOptions, field);
  331. }
  332. let parameterDescriptions: ParameterDescription[] = [];
  333. // Generate options and values for each parameter.
  334. if (
  335. field &&
  336. field.kind === FieldValueKind.FUNCTION &&
  337. field.meta.parameters.length > 0 &&
  338. fieldValue?.kind === FieldValueKind.FUNCTION
  339. ) {
  340. parameterDescriptions = field.meta.parameters.map(
  341. (param, index: number): ParameterDescription => {
  342. if (param.kind === 'column') {
  343. const fieldParameter = this.getFieldOrTagOrMeasurementValue(
  344. fieldValue.function[1],
  345. [fieldValue.function[0]]
  346. );
  347. fieldOptions = appendFieldIfUnknown(fieldOptions, fieldParameter);
  348. return {
  349. kind: 'column',
  350. value: fieldParameter,
  351. required: param.required,
  352. options: Object.values(fieldOptions).filter(
  353. ({value}) =>
  354. (value.kind === FieldValueKind.FIELD ||
  355. value.kind === FieldValueKind.TAG ||
  356. value.kind === FieldValueKind.MEASUREMENT ||
  357. value.kind === FieldValueKind.CUSTOM_MEASUREMENT ||
  358. value.kind === FieldValueKind.METRICS ||
  359. value.kind === FieldValueKind.BREAKDOWN) &&
  360. validateColumnTypes(param.columnTypes as ValidateColumnTypes, value)
  361. ),
  362. };
  363. }
  364. if (param.kind === 'dropdown') {
  365. return {
  366. kind: 'dropdown',
  367. options: param.options,
  368. dataType: param.dataType,
  369. required: param.required,
  370. value:
  371. (fieldValue.kind === 'function' && fieldValue.function[index + 1]) ||
  372. param.defaultValue ||
  373. '',
  374. };
  375. }
  376. return {
  377. kind: 'value',
  378. value:
  379. (fieldValue.kind === 'function' && fieldValue.function[index + 1]) ||
  380. param.defaultValue ||
  381. '',
  382. dataType: param.dataType,
  383. required: param.required,
  384. placeholder: param.placeholder,
  385. };
  386. }
  387. );
  388. }
  389. return {field, fieldOptions, parameterDescriptions};
  390. }
  391. renderParameterInputs(parameters: ParameterDescription[]): React.ReactNode[] {
  392. const {
  393. disabled,
  394. inFieldLabels,
  395. filterAggregateParameters,
  396. hideParameterSelector,
  397. skipParameterPlaceholder,
  398. fieldValue,
  399. } = this.props;
  400. const inputs = parameters.map((descriptor: ParameterDescription, index: number) => {
  401. if (descriptor.kind === 'column' && descriptor.options.length > 0) {
  402. if (hideParameterSelector) {
  403. return null;
  404. }
  405. const aggregateParameters = filterAggregateParameters
  406. ? descriptor.options.filter(option =>
  407. filterAggregateParameters(option, fieldValue)
  408. )
  409. : descriptor.options;
  410. aggregateParameters.forEach(opt => {
  411. opt.trailingItems = this.renderTag(opt.value.kind, String(opt.label));
  412. });
  413. return (
  414. <SelectControl
  415. key="select"
  416. name="parameter"
  417. menuPlacement="auto"
  418. placeholder={t('Select value')}
  419. options={aggregateParameters}
  420. value={descriptor.value}
  421. required={descriptor.required}
  422. onChange={this.handleFieldParameterChange}
  423. inFieldLabel={inFieldLabels ? t('Parameter: ') : undefined}
  424. disabled={disabled}
  425. styles={!inFieldLabels ? this.FieldSelectStyles : undefined}
  426. components={this.FieldSelectComponents}
  427. />
  428. );
  429. }
  430. if (descriptor.kind === 'value') {
  431. const inputProps = {
  432. required: descriptor.required,
  433. value: descriptor.value,
  434. onUpdate: this.handleScalarParameterChange(index + 1),
  435. placeholder: descriptor.placeholder,
  436. disabled,
  437. };
  438. switch (descriptor.dataType) {
  439. case 'number':
  440. return (
  441. <BufferedInput
  442. name="refinement"
  443. key="parameter:number"
  444. type="text"
  445. inputMode="numeric"
  446. pattern="[0-9]*(\.[0-9]*)?"
  447. {...inputProps}
  448. />
  449. );
  450. case 'integer':
  451. return (
  452. <BufferedInput
  453. name="refinement"
  454. key="parameter:integer"
  455. type="text"
  456. inputMode="numeric"
  457. pattern="[0-9]*"
  458. {...inputProps}
  459. />
  460. );
  461. default:
  462. return (
  463. <BufferedInput
  464. name="refinement"
  465. key="parameter:text"
  466. type="text"
  467. {...inputProps}
  468. />
  469. );
  470. }
  471. }
  472. if (descriptor.kind === 'dropdown') {
  473. return (
  474. <SelectControl
  475. key="dropdown"
  476. name="dropdown"
  477. menuPlacement="auto"
  478. placeholder={t('Select value')}
  479. options={descriptor.options}
  480. value={descriptor.value}
  481. required={descriptor.required}
  482. onChange={this.handleDropdownParameterChange(index + 1)}
  483. inFieldLabel={inFieldLabels ? t('Parameter: ') : undefined}
  484. disabled={disabled}
  485. />
  486. );
  487. }
  488. throw new Error(`Unknown parameter type encountered for ${this.props.fieldValue}`);
  489. });
  490. if (skipParameterPlaceholder) {
  491. return inputs;
  492. }
  493. // Add enough disabled inputs to fill the grid up.
  494. // We always have 1 input.
  495. const {gridColumns} = this.props;
  496. const requiredInputs = (gridColumns ?? inputs.length + 1) - inputs.length - 1;
  497. if (gridColumns !== undefined && requiredInputs > 0) {
  498. for (let i = 0; i < requiredInputs; i++) {
  499. inputs.push(<BlankSpace key={i} data-test-id="blankSpace" />);
  500. }
  501. }
  502. return inputs;
  503. }
  504. renderTag(kind: FieldValueKind, label: string) {
  505. const {shouldRenderTag} = this.props;
  506. if (shouldRenderTag === false) {
  507. return null;
  508. }
  509. let text, tagType;
  510. switch (kind) {
  511. case FieldValueKind.FUNCTION:
  512. text = 'f(x)';
  513. tagType = 'success';
  514. break;
  515. case FieldValueKind.CUSTOM_MEASUREMENT:
  516. case FieldValueKind.MEASUREMENT:
  517. text = 'field';
  518. tagType = 'highlight';
  519. break;
  520. case FieldValueKind.BREAKDOWN:
  521. text = 'field';
  522. tagType = 'highlight';
  523. break;
  524. case FieldValueKind.TAG:
  525. text = kind;
  526. tagType = 'warning';
  527. break;
  528. case FieldValueKind.NUMERIC_METRICS:
  529. text = 'f(x)';
  530. tagType = 'success';
  531. break;
  532. case FieldValueKind.FIELD:
  533. text = DEPRECATED_FIELDS.includes(label) ? 'deprecated' : 'field';
  534. tagType = 'highlight';
  535. break;
  536. default:
  537. text = kind;
  538. }
  539. return <Tag type={tagType}>{text}</Tag>;
  540. }
  541. render() {
  542. const {
  543. className,
  544. takeFocus,
  545. filterPrimaryOptions,
  546. fieldValue,
  547. inFieldLabels,
  548. disabled,
  549. error,
  550. hidePrimarySelector,
  551. gridColumns,
  552. otherColumns,
  553. placeholder,
  554. noFieldsMessage,
  555. skipParameterPlaceholder,
  556. } = this.props;
  557. const {field, fieldOptions, parameterDescriptions} = this.getFieldData();
  558. const allFieldOptions = filterPrimaryOptions
  559. ? Object.values(fieldOptions).filter(filterPrimaryOptions)
  560. : Object.values(fieldOptions);
  561. allFieldOptions.forEach(opt => {
  562. opt.trailingItems = this.renderTag(opt.value.kind, String(opt.label));
  563. });
  564. const selectProps: ControlProps<FieldValueOption> = {
  565. name: 'field',
  566. options: Object.values(allFieldOptions),
  567. placeholder: placeholder ?? t('(Required)'),
  568. value: field,
  569. onChange: this.handleFieldChange,
  570. inFieldLabel: inFieldLabels ? t('Function: ') : undefined,
  571. disabled,
  572. noOptionsMessage: () => noFieldsMessage,
  573. menuPlacement: 'auto',
  574. };
  575. if (takeFocus && field === null) {
  576. selectProps.autoFocus = true;
  577. }
  578. const parameters = this.renderParameterInputs(parameterDescriptions);
  579. if (fieldValue?.kind === FieldValueKind.EQUATION) {
  580. return (
  581. <Container
  582. className={className}
  583. gridColumns={1}
  584. tripleLayout={false}
  585. error={error !== undefined}
  586. data-test-id="queryField"
  587. >
  588. <ArithmeticInput
  589. name="arithmetic"
  590. key="parameter:text"
  591. type="text"
  592. required
  593. value={fieldValue.field}
  594. onUpdate={this.handleEquationChange}
  595. options={otherColumns}
  596. placeholder={t('Equation')}
  597. />
  598. {error ? (
  599. <ArithmeticError title={error}>
  600. <IconWarning color="errorText" data-test-id="arithmeticErrorWarning" />
  601. </ArithmeticError>
  602. ) : null}
  603. </Container>
  604. );
  605. }
  606. // if there's more than 2 parameters, set gridColumns to 2 so they go onto the next line instead
  607. const containerColumns =
  608. parameters.length > 2 ? 2 : gridColumns ? gridColumns : parameters.length + 1;
  609. let gridColumnsQuantity: undefined | number = undefined;
  610. if (skipParameterPlaceholder) {
  611. // if the selected field is a function and has parameters, we would like to display each value in separate columns.
  612. // Otherwise the field should be displayed in a column, taking up all available space and not displaying the "no parameter" field
  613. if (fieldValue.kind !== 'function') {
  614. gridColumnsQuantity = 1;
  615. } else {
  616. const operation =
  617. AGGREGATIONS[fieldValue.function[0]] ??
  618. SESSIONS_OPERATIONS[fieldValue.function[0]];
  619. if (operation?.parameters.length > 0) {
  620. if (containerColumns === 3 && operation.parameters.length === 1) {
  621. gridColumnsQuantity = 2;
  622. } else {
  623. gridColumnsQuantity = containerColumns;
  624. }
  625. } else {
  626. gridColumnsQuantity = 1;
  627. }
  628. }
  629. }
  630. return (
  631. <Container
  632. className={className}
  633. gridColumns={gridColumnsQuantity ?? containerColumns}
  634. tripleLayout={gridColumns === 3 && parameters.length > 2}
  635. data-test-id="queryField"
  636. >
  637. {!hidePrimarySelector && (
  638. <SelectControl
  639. {...selectProps}
  640. styles={!inFieldLabels ? this.FieldSelectStyles : undefined}
  641. components={this.FieldSelectComponents}
  642. />
  643. )}
  644. {parameters}
  645. </Container>
  646. );
  647. }
  648. }
  649. function validateColumnTypes(
  650. columnTypes: ValidateColumnTypes,
  651. input: FieldValueColumns
  652. ): boolean {
  653. if (typeof columnTypes === 'function') {
  654. return columnTypes({name: input.meta.name, dataType: input.meta.dataType});
  655. }
  656. return (columnTypes as string[]).includes(input.meta.dataType);
  657. }
  658. const Container = styled('div')<{
  659. gridColumns: number;
  660. tripleLayout: boolean;
  661. error?: boolean;
  662. }>`
  663. display: grid;
  664. ${p =>
  665. p.tripleLayout
  666. ? `grid-template-columns: 1fr 2fr;`
  667. : `grid-template-columns: repeat(${p.gridColumns}, 1fr) ${p.error ? 'auto' : ''};`}
  668. gap: ${space(1)};
  669. align-items: center;
  670. flex-grow: 1;
  671. `;
  672. interface BufferedInputProps extends InputProps {
  673. onUpdate: (value: string) => void;
  674. value: string;
  675. }
  676. type InputState = {value: string};
  677. /**
  678. * Because controlled inputs fire onChange on every key stroke,
  679. * we can't update the QueryField that often as it would re-render
  680. * the input elements causing focus to be lost.
  681. *
  682. * Using a buffered input lets us throttle rendering and enforce data
  683. * constraints better.
  684. */
  685. class BufferedInput extends Component<BufferedInputProps, InputState> {
  686. constructor(props: BufferedInputProps) {
  687. super(props);
  688. this.input = createRef();
  689. }
  690. state: InputState = {
  691. value: this.props.value,
  692. };
  693. private input: React.RefObject<HTMLInputElement>;
  694. get isValid() {
  695. if (!this.input.current) {
  696. return true;
  697. }
  698. return this.input.current.validity.valid;
  699. }
  700. handleBlur = () => {
  701. if (this.props.required && this.state.value === '') {
  702. // Handle empty strings separately because we don't pass required
  703. // to input elements, causing isValid to return true
  704. this.setState({value: this.props.value});
  705. } else if (this.isValid) {
  706. this.props.onUpdate(this.state.value);
  707. } else {
  708. this.setState({value: this.props.value});
  709. }
  710. };
  711. handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  712. if (this.isValid) {
  713. this.setState({value: event.target.value});
  714. }
  715. };
  716. render() {
  717. const {onUpdate: _, ...props} = this.props;
  718. return (
  719. <StyledInput
  720. {...props}
  721. ref={this.input}
  722. className="form-control"
  723. value={this.state.value}
  724. onChange={this.handleChange}
  725. onBlur={this.handleBlur}
  726. />
  727. );
  728. }
  729. }
  730. // Set a min-width to allow shrinkage in grid.
  731. const StyledInput = styled(Input)`
  732. min-width: 50px;
  733. `;
  734. const BlankSpace = styled('div')`
  735. /* Match the height of the select boxes */
  736. height: ${p => p.theme.form.md.height}px;
  737. min-width: 50px;
  738. background: ${p => p.theme.backgroundSecondary};
  739. border-radius: ${p => p.theme.borderRadius};
  740. display: flex;
  741. align-items: center;
  742. justify-content: center;
  743. &:after {
  744. font-size: ${p => p.theme.fontSizeMedium};
  745. content: '${t('No parameter')}';
  746. color: ${p => p.theme.subText};
  747. }
  748. `;
  749. const ArithmeticError = styled(Tooltip)`
  750. color: ${p => p.theme.errorText};
  751. animation: ${() => pulse(1.15)} 1s ease infinite;
  752. display: flex;
  753. `;
  754. export {QueryField};
  755. export function appendFieldIfUnknown(
  756. fieldOptions: FieldOptions,
  757. field: FieldValue | null
  758. ): FieldOptions {
  759. if (!field) {
  760. return fieldOptions;
  761. }
  762. if (field && field.kind === FieldValueKind.TAG && field.meta.unknown) {
  763. // Clone the options so we don't mutate other rows.
  764. fieldOptions = Object.assign({}, fieldOptions);
  765. fieldOptions[field.meta.name] = {label: field.meta.name, value: field};
  766. } else if (field && field.kind === FieldValueKind.CUSTOM_MEASUREMENT) {
  767. fieldOptions = Object.assign({}, fieldOptions);
  768. fieldOptions[`measurement:${field.meta.name}`] = {
  769. label: field.meta.name,
  770. value: field,
  771. };
  772. }
  773. return fieldOptions;
  774. }