summaryrefslogtreecommitdiffstats
path: root/client/js/app/src/app/pages/querybuilder/Components/Buttons/AddQueryInputButton.jsx
blob: 8aeaff971bf54753bbea145aabc0b40e4f40a6dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import React, { useContext } from 'react';
import { QueryInputContext } from '../Contexts/QueryInputContext';
import OverlayTrigger from 'react-bootstrap/OverlayTrigger';
import Tooltip from 'react-bootstrap/Tooltip';

export default function AddQueryInput() {
  const { inputs, setInputs, id, setId } = useContext(QueryInputContext);

  /**
   * Adds a new element to inputs.
   * @param {Event} e Event that happened.
   */
  const updateInputs = (e) => {
    e.preventDefault();
    setId((id) => id + 1);
    setInputs((prevInputs) => [
      ...prevInputs,
      {
        id: `${id + 1}`,
        type: 'yql',
        typeof: 'String',
        input: '',
        hasChildren: false,
        children: [],
      },
    ]);
  };

  return (
    <OverlayTrigger
      placement="right"
      delay={{ show: 250, hide: 400 }}
      overlay={<Tooltip id="button-tooltip">Add row</Tooltip>}
    >
      <span>
        <button
          id="addRow"
          className="addRow"
          height="0"
          width="0"
          onClick={updateInputs}
        >
          +
        </button>
      </span>
    </OverlayTrigger>
  );
}