QueryRows.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, supportsLogs } = 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. supportsLogs={supportsLogs}
  54. />
  55. </div>
  56. <div className="query-row-tools">
  57. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickClearButton}>
  58. <i className="fa fa-times" />
  59. </button>
  60. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickAddButton}>
  61. <i className="fa fa-plus" />
  62. </button>
  63. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickRemoveButton}>
  64. <i className="fa fa-minus" />
  65. </button>
  66. </div>
  67. </div>
  68. );
  69. }
  70. }
  71. export default class QueryRows extends PureComponent<any, {}> {
  72. render() {
  73. const { className = '', queries, queryErrors = [], queryHints = [], ...handlers } = this.props;
  74. return (
  75. <div className={className}>
  76. {queries.map((q, index) => (
  77. <QueryRow
  78. key={q.key}
  79. index={index}
  80. query={q.query}
  81. queryError={queryErrors[index]}
  82. queryHint={queryHints[index]}
  83. edited={q.edited}
  84. {...handlers}
  85. />
  86. ))}
  87. </div>
  88. );
  89. }
  90. }