Variable query editor (#856)

* refactor: convert module to typescript

* refactor: covert utils to typescript

* variable query editor WIP

* variable editor: fix type error after grafana/ui update

* variable editor: use FormLabel from grafana/ui

* variable editor: refactor

* variable editor: input validation and highlights

* variable editor: fix tests

* variable query: fix backward compatibility with empty queries

* fix linter errors

* variable editor: fix variable replacement in queries
This commit is contained in:
Alexander Zobnin
2020-01-13 11:31:40 +03:00
committed by GitHub
parent 4f24b2bf23
commit 82cfda6524
11 changed files with 519 additions and 78 deletions

View File

@@ -0,0 +1,69 @@
import React, { FC } from 'react';
import { css, cx } from 'emotion';
import { Themeable, withTheme, Input, GrafanaTheme, EventsWithValidation, ValidationEvents } from '@grafana/ui';
import { isRegex, variableRegex } from '../utils';
const variablePattern = RegExp(`^${variableRegex.source}`);
const getStyles = (theme: GrafanaTheme) => ({
inputRegex: css`
color: ${theme.colors.orange}
`,
inputVariable: css`
color: ${theme.colors.variable}
`,
});
const zabbixInputValidationEvents: ValidationEvents = {
[EventsWithValidation.onBlur]: [
{
rule: value => {
if (!value) {
return true;
}
if (value.length > 1 && value[0] === '/') {
if (value[value.length - 1] !== '/') {
return false;
}
}
return true;
},
errorMessage: 'Not a valid regex',
},
{
rule: value => {
if (value === '*') {
return false;
}
return true;
},
errorMessage: 'Wildcards not supported. Use /.*/ instead',
},
],
};
interface Props extends React.ComponentProps<typeof Input>, Themeable {
}
const UnthemedZabbixInput: FC<Props> = ({ theme, value, ref, validationEvents, ...restProps }) => {
const styles = getStyles(theme);
let inputClass;
if (variablePattern.test(value as string)) {
inputClass = styles.inputVariable;
}
if (isRegex(value)) {
inputClass = styles.inputRegex;
}
return (
<Input
className={inputClass}
value={value}
validationEvents={zabbixInputValidationEvents}
{...restProps}
/>
);
};
export const ZabbixInput = withTheme(UnthemedZabbixInput);