AliasBy.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import React, { Component } from 'react';
  2. import { debounce } from 'lodash';
  3. export interface Props {
  4. onChange: (alignmentPeriod) => void;
  5. value: string;
  6. }
  7. export interface State {
  8. value: string;
  9. }
  10. export class AliasBy extends Component<Props, State> {
  11. constructor(props) {
  12. super(props);
  13. this.handleChange = this.handleChange.bind(this);
  14. this.onChange = debounce(this.onChange.bind(this), 500);
  15. this.state = { value: '' };
  16. }
  17. componentDidMount() {
  18. this.setState({ value: this.props.value });
  19. }
  20. componentWillReceiveProps(nextProps: Props) {
  21. if (nextProps.value !== this.props.value) {
  22. this.setState({ value: nextProps.value });
  23. }
  24. }
  25. handleChange(e) {
  26. this.setState({ value: e.target.value });
  27. this.onChange(e.target.value);
  28. }
  29. onChange(value) {
  30. this.props.onChange(value);
  31. }
  32. render() {
  33. return (
  34. <>
  35. <div className="gf-form-inline">
  36. <div className="gf-form">
  37. <label className="gf-form-label query-keyword width-9">Alias By</label>
  38. <input
  39. type="text"
  40. className="gf-form-input width-24"
  41. value={this.state.value}
  42. onChange={this.handleChange}
  43. />
  44. </div>
  45. <div className="gf-form gf-form--grow">
  46. <div className="gf-form-label gf-form-label--grow" />
  47. </div>
  48. </div>
  49. </>
  50. );
  51. }
  52. }