Skip to content

Commit 105fb9c

Browse files
authored
Modify query buttons into header buttons (#10)
* Release 0.1.4
1 parent 7344230 commit 105fb9c

File tree

5 files changed

+42
-38
lines changed

5 files changed

+42
-38
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ cypress/screenshots/actual
66
cypress/videos/
77
dist/
88
compiled/
9-
yarn-error.log
9+
yarn-error.log
10+
.DS_Store
11+
.idea/

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## v0.1.4
6+
7+
- Modify query buttons into header buttons
8+
59
## v0.0.1
610

711
- Add `DatasourceWithAsyncBackend` class to handle async query flow on the frontend

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@grafana/async-query-data",
3-
"version": "0.0.4",
3+
"version": "0.1.4",
44
"description": "Async query support for Grafana",
55
"main": "dist/index.js",
66
"scripts": {

src/RunQueryButtons.test.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,42 @@ const getDefaultProps = (overrides?: Partial<RunQueryButtonsProps<DataQuery>>) =
88
return {
99
onRunQuery: jest.fn(),
1010
onCancelQuery: jest.fn(),
11-
isQueryValid: jest.fn(),
11+
enableRun: true,
1212
query: { refId: 'refId' },
1313
...overrides,
1414
};
1515
};
1616

1717
describe('RunQueryButtons', () => {
18-
it('disable the run button if the query is invalid', () => {
19-
const props = getDefaultProps({ isQueryValid: jest.fn().mockReturnValue(false) });
18+
it('disable the run button if the if the enableRun button is false', () => {
19+
const props = getDefaultProps({ enableRun: false});
2020
render(<RunQueryButtons {...props} />);
21-
const runButton = screen.getByRole('button', { name: 'Run' });
21+
const runButton = screen.getByRole('button', { name: 'Run query' });
2222
expect(runButton).toBeDisabled();
2323
});
2424

25-
it('run button should be enabled if the query is valid', () => {
26-
const props = getDefaultProps({ isQueryValid: jest.fn().mockReturnValue(true) });
25+
it('run button should be enabled if the enableRun button is true', () => {
26+
const props = getDefaultProps({ enableRun: true});
2727
render(<RunQueryButtons {...props} />);
28-
const runButton = screen.getByRole('button', { name: 'Run' });
28+
const runButton = screen.getByRole('button', { name: 'Run query' });
2929
expect(runButton).not.toBeDisabled();
3030
});
3131

3232
it('only renders the `Run` button if onCancelQuery is undefined', () => {
3333
const props = getDefaultProps({ onCancelQuery: undefined });
3434
render(<RunQueryButtons {...props} />);
35-
const runButton = screen.getByRole('button', { name: 'Run' });
35+
const runButton = screen.getByRole('button', { name: 'Run query' });
3636
expect(runButton).toBeInTheDocument();
37-
const stopButton = screen.queryByRole('button', { name: 'Stop' });
37+
const stopButton = screen.queryByRole('button', { name: 'Stop query' });
3838
expect(stopButton).not.toBeInTheDocument();
3939
});
4040

4141
it('renders the `Run` and `Stop` buttons if onCancelQuery defined', () => {
4242
const props = getDefaultProps();
4343
render(<RunQueryButtons {...props} />);
44-
const runButton = screen.getByRole('button', { name: 'Run' });
44+
const runButton = screen.getByRole('button', { name: 'Run query' });
4545
expect(runButton).toBeInTheDocument();
46-
const stopButton = screen.queryByRole('button', { name: 'Stop' });
46+
const stopButton = screen.queryByRole('button', { name: 'Stop query' });
4747
expect(stopButton).toBeInTheDocument();
4848
});
4949
});

src/RunQueryButtons.tsx

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React, { useEffect, useState } from 'react';
2-
import { HorizontalGroup, Button, Spinner } from '@grafana/ui';
2+
import { Button } from '@grafana/ui';
33
import { DataQuery, LoadingState } from '@grafana/data';
44

55
export interface RunQueryButtonsProps<TQuery extends DataQuery> {
6+
enableRun?: boolean;
67
onRunQuery: () => void;
78
onCancelQuery?: (query: TQuery) => void;
8-
isQueryValid: (query: TQuery) => boolean;
99
query: TQuery;
1010
state?: LoadingState;
1111
}
@@ -39,30 +39,28 @@ export const RunQueryButtons = <TQuery extends DataQuery>(props: RunQueryButtons
3939
}
4040
: undefined;
4141

42-
const isQueryValid = props.isQueryValid(props.query);
43-
4442
return (
45-
<HorizontalGroup>
46-
<Button icon={running ? undefined : 'play'} disabled={running || !isQueryValid} onClick={onRunQuery}>
47-
{running && !stopping ? (
48-
<HorizontalGroup>
49-
<Spinner /> Running
50-
</HorizontalGroup>
51-
) : (
52-
'Run'
53-
)}
54-
</Button>
55-
{onCancelQuery && (
56-
<Button icon={running ? undefined : 'square-shape'} disabled={!running || stopping} onClick={onCancelQuery}>
57-
{stopping ? (
58-
<HorizontalGroup>
59-
<Spinner /> Stopping
60-
</HorizontalGroup>
61-
) : (
62-
'Stop'
63-
)}
43+
<>
44+
<Button
45+
variant={props.enableRun ? 'primary' : 'secondary'}
46+
size="sm"
47+
onClick={onRunQuery}
48+
icon={running && !stopping ? 'fa fa-spinner' : undefined}
49+
disabled={state === LoadingState.Loading || !props.enableRun}
50+
>
51+
Run query
6452
</Button>
65-
)}
66-
</HorizontalGroup>
53+
{onCancelQuery &&
54+
<Button
55+
variant={running && !stopping ? 'primary' : 'secondary'}
56+
size="sm"
57+
disabled={!running || stopping}
58+
icon={stopping ? 'fa fa-spinner' : undefined}
59+
onClick={onCancelQuery}
60+
>
61+
Stop query
62+
</Button>
63+
}
64+
</>
6765
);
6866
};

0 commit comments

Comments
 (0)