QueryRows.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import React, { PureComponent } from 'react';
  2. import QueryField from './QueryField';
  3. class QueryRow extends PureComponent<any, any> {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. query: '',
  8. };
  9. }
  10. handleChangeQuery = value => {
  11. const { index, onChangeQuery } = this.props;
  12. this.setState({ query: value });
  13. if (onChangeQuery) {
  14. onChangeQuery(value, index);
  15. }
  16. };
  17. handleClickAddButton = () => {
  18. const { index, onAddQueryRow } = this.props;
  19. if (onAddQueryRow) {
  20. onAddQueryRow(index);
  21. }
  22. };
  23. handleClickRemoveButton = () => {
  24. const { index, onRemoveQueryRow } = this.props;
  25. if (onRemoveQueryRow) {
  26. onRemoveQueryRow(index);
  27. }
  28. };
  29. handlePressEnter = () => {
  30. const { onExecuteQuery } = this.props;
  31. if (onExecuteQuery) {
  32. onExecuteQuery();
  33. }
  34. };
  35. render() {
  36. const { request } = this.props;
  37. return (
  38. <div className="query-row">
  39. <div className="query-row-tools">
  40. <button className="btn btn-small btn-inverse" onClick={this.handleClickAddButton}>
  41. <i className="fa fa-plus" />
  42. </button>
  43. <button className="btn btn-small btn-inverse" onClick={this.handleClickRemoveButton}>
  44. <i className="fa fa-minus" />
  45. </button>
  46. </div>
  47. <div className="query-field-wrapper">
  48. <QueryField onPressEnter={this.handlePressEnter} onQueryChange={this.handleChangeQuery} request={request} />
  49. </div>
  50. </div>
  51. );
  52. }
  53. }
  54. export default class QueryRows extends PureComponent<any, any> {
  55. render() {
  56. const { className = '', queries, ...handlers } = this.props;
  57. return (
  58. <div className={className}>{queries.map((q, index) => <QueryRow key={q.key} index={index} {...handlers} />)}</div>
  59. );
  60. }
  61. }