Skip to content

Commit f76c851

Browse files
authored
Update CLI legacy switch to ButtonGroup (#74)
* Update CLI legacy switch to ButtonGroup * Fix tests
1 parent e31d88c commit f76c851

File tree

4 files changed

+91
-20
lines changed

4 files changed

+91
-20
lines changed

src/redis-cli-panel/components/redis-cli-panel/redis-cli-panel.test.tsx

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import React from 'react';
21
import { shallow, ShallowWrapper } from 'enzyme';
2+
import React from 'react';
33
import { Observable } from 'rxjs';
44
import { LoadingState, PanelData } from '@grafana/data';
5-
import { Button } from '@grafana/ui';
6-
import { Help } from '../../constants';
5+
import { Button, RadioButtonGroup } from '@grafana/ui';
6+
import { Help, ResponseMode } from '../../constants';
77
import { PanelOptions } from '../../types';
8-
import { RedisCLIPanel } from './redis-cli-panel';
98
import { CLITextArea } from '../auto-scrolling-text-area';
9+
import { RedisCLIPanel } from './redis-cli-panel';
1010

1111
/**
1212
* Override
@@ -513,6 +513,45 @@ describe('RedisCLIPanel', () => {
513513
});
514514
});
515515

516+
describe('Options', () => {
517+
/**
518+
* Response Mode
519+
*/
520+
describe('ResponseMode', () => {
521+
it('Should apply options value and change', () => {
522+
const options = {};
523+
const wrapper = shallow(
524+
<RedisCLIPanel
525+
{...additionalProps}
526+
width={width}
527+
height={height}
528+
data={data}
529+
onOptionsChange={onOptionsChangeMock}
530+
replaceVariables={replaceVariablesMock}
531+
options={options}
532+
/>
533+
);
534+
const testedComponent = wrapper.find(RadioButtonGroup);
535+
expect(testedComponent.prop('value')).toEqual(ResponseMode.CLI);
536+
537+
testedComponent.simulate('change');
538+
expect(onOptionsChangeMock).not.toHaveBeenCalled();
539+
540+
testedComponent.simulate('change', ResponseMode.RAW);
541+
expect(onOptionsChangeMock).toHaveBeenCalledWith({
542+
...options,
543+
raw: true,
544+
});
545+
546+
testedComponent.simulate('change', ResponseMode.CLI);
547+
expect(onOptionsChangeMock).toHaveBeenCalledWith({
548+
...options,
549+
raw: false,
550+
});
551+
});
552+
});
553+
});
554+
516555
afterAll(() => {
517556
jest.resetAllMocks();
518557
});

src/redis-cli-panel/components/redis-cli-panel/redis-cli-panel.tsx

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ import { map as map$, switchMap as switchMap$ } from 'rxjs/operators';
44
import { css, cx } from '@emotion/css';
55
import { DataFrame, DataQueryRequest, DataQueryResponse, PanelProps } from '@grafana/data';
66
import { getDataSourceSrv } from '@grafana/runtime';
7-
import { Button, LegacyForms, useTheme2 } from '@grafana/ui';
8-
import { Help } from '../../constants';
7+
import { Button, RadioButtonGroup, useTheme2 } from '@grafana/ui';
8+
import { Help, ResponseMode, ResponseModeOptions } from '../../constants';
99
import { Styles } from '../../styles';
1010
import { HelpCommand, PanelOptions, RedisQuery } from '../../types';
1111
import { CLITextArea } from '../auto-scrolling-text-area';
1212

13-
/**
14-
* Legacy Forms
15-
*/
16-
const { Switch } = LegacyForms;
17-
1813
/**
1914
* Redis CLI Panel
2015
*/
@@ -26,7 +21,7 @@ export const RedisCLIPanel: React.FC<PanelProps<PanelOptions>> = ({
2621
onOptionsChange,
2722
replaceVariables,
2823
}) => {
29-
const { query, raw, output, help } = options;
24+
const { query, output, help } = options;
3025
const styles = Styles(useTheme2());
3126

3227
/**
@@ -107,6 +102,21 @@ export const RedisCLIPanel: React.FC<PanelProps<PanelOptions>> = ({
107102
onOptionsChange({ ...options, output: `${output ? `${output}\n` : ''}${result}`, query: '' });
108103
};
109104

105+
/**
106+
* Change view mode
107+
* @param event
108+
*/
109+
const onChangeResponseMode = (event?: ResponseMode) => {
110+
if (event === undefined) {
111+
return;
112+
}
113+
114+
onOptionsChange({
115+
...options,
116+
raw: event === ResponseMode.RAW ? true : false,
117+
});
118+
};
119+
110120
/**
111121
* Run Query
112122
*
@@ -197,14 +207,11 @@ export const RedisCLIPanel: React.FC<PanelProps<PanelOptions>> = ({
197207
value={query}
198208
/>
199209

200-
<Switch
201-
label="Raw"
202-
labelClass="width-4"
203-
tooltip="If checked, use raw formatting for replies."
204-
checked={raw || false}
205-
onChange={(event: any) => {
206-
onOptionsChange({ ...options, raw: event.currentTarget.checked });
207-
}}
210+
<RadioButtonGroup
211+
className={cx(styles.cli)}
212+
value={options.raw ? ResponseMode.RAW : ResponseMode.CLI}
213+
options={ResponseModeOptions}
214+
onChange={onChangeResponseMode}
208215
/>
209216

210217
<Button

src/redis-cli-panel/constants.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,25 @@ export const Help: { [key: string]: HelpCommand } = {
2323
...RedisSearchHelp,
2424
...RedisTimeSeriesHelp,
2525
};
26+
27+
/**
28+
* Response Modes
29+
*/
30+
export enum ResponseMode {
31+
CLI = 'CLI',
32+
RAW = 'Raw',
33+
}
34+
35+
/**
36+
* Response Mode options
37+
*/
38+
export const ResponseModeOptions = [
39+
{
40+
label: 'CLI',
41+
value: ResponseMode.CLI,
42+
},
43+
{
44+
label: 'Raw',
45+
value: ResponseMode.RAW,
46+
},
47+
];

src/redis-cli-panel/styles.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,8 @@ export const Styles = (theme: GrafanaTheme2) => {
4545
right: 0;
4646
margin-right: 12px;
4747
`,
48+
cli: css`
49+
margin-right: 4px;
50+
`,
4851
};
4952
};

0 commit comments

Comments
 (0)