QueryRows.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React, { PureComponent } from 'react';
  2. // TODO make this datasource-plugin-dependent
  3. import QueryField from './PromQueryField';
  4. class QueryRow extends PureComponent<any, {}> {
  5. onChangeQuery = (value, override?: boolean) => {
  6. const { index, onChangeQuery } = this.props;
  7. if (onChangeQuery) {
  8. onChangeQuery(value, index, override);
  9. }
  10. };
  11. onClickAddButton = () => {
  12. const { index, onAddQueryRow } = this.props;
  13. if (onAddQueryRow) {
  14. onAddQueryRow(index);
  15. }
  16. };
  17. onClickClearButton = () => {
  18. this.onChangeQuery('', true);
  19. };
  20. onClickHintFix = action => {
  21. const { index, onClickHintFix } = this.props;
  22. if (onClickHintFix) {
  23. onClickHintFix(action, index);
  24. }
  25. };
  26. onClickRemoveButton = () => {
  27. const { index, onRemoveQueryRow } = this.props;
  28. if (onRemoveQueryRow) {
  29. onRemoveQueryRow(index);
  30. }
  31. };
  32. onPressEnter = () => {
  33. const { onExecuteQuery } = this.props;
  34. if (onExecuteQuery) {
  35. onExecuteQuery();
  36. }
  37. };
  38. render() {
  39. const { edited, history, query, queryError, queryHint, request } = this.props;
  40. return (
  41. <div className="query-row">
  42. <div className="query-row-field">
  43. <QueryField
  44. error={queryError}
  45. hint={queryHint}
  46. initialQuery={edited ? null : query}
  47. history={history}
  48. portalPrefix="explore"
  49. onClickHintFix={this.onClickHintFix}
  50. onPressEnter={this.onPressEnter}
  51. onQueryChange={this.onChangeQuery}
  52. request={request}
  53. />
  54. </div>
  55. <div className="query-row-tools">
  56. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickClearButton}>
  57. <i className="fa fa-times" />
  58. </button>
  59. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickAddButton}>
  60. <i className="fa fa-plus" />
  61. </button>
  62. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickRemoveButton}>
  63. <i className="fa fa-minus" />
  64. </button>
  65. </div>
  66. </div>
  67. );
  68. }
  69. }
  70. export default class QueryRows extends PureComponent<any, {}> {
  71. render() {
  72. const { className = '', queries, queryErrors = [], queryHints = [], ...handlers } = this.props;
  73. return (
  74. <div className={className}>
  75. {queries.map((q, index) => (
  76. <QueryRow
  77. key={q.key}
  78. index={index}
  79. query={q.query}
  80. queryError={queryErrors[index]}
  81. queryHint={queryHints[index]}
  82. edited={q.edited}
  83. {...handlers}
  84. />
  85. ))}
  86. </div>
  87. );
  88. }
  89. }