grafana/public/app/plugins/panel/timeseries/NullsThresholdInput.tsx
Ashley Harrison 47f8717149
React: Use new JSX transform (#88802)
* update eslint, tsconfig + esbuild to handle new jsx transform

* remove thing that breaks the new jsx transform

* remove react imports

* adjust grafana-icons build

* is this the correct syntax?

* try this

* well this was much easier than expected...

* change grafana-plugin-configs webpack config

* fixes

* fix lockfile

* fix 2 more violations

* use path.resolve instead of require.resolve

* remove react import

* fix react imports

* more fixes

* remove React import

* remove import React from docs

* remove another react import
2024-06-25 12:43:47 +01:00

70 lines
1.6 KiB
TypeScript

import * as React from 'react';
import { rangeUtil } from '@grafana/data';
import { Input } from '@grafana/ui';
export enum InputPrefix {
LessThan = 'lessthan',
GreaterThan = 'greaterthan',
}
type Props = {
value: number;
onChange: (value?: number | boolean | undefined) => void;
inputPrefix?: InputPrefix;
isTime: boolean;
};
export const NullsThresholdInput = ({ value, onChange, inputPrefix, isTime }: Props) => {
let defaultValue = rangeUtil.secondsToHms(value / 1000);
if (!isTime) {
defaultValue = '10';
}
const checkAndUpdate = (txt: string) => {
let val: boolean | number = false;
if (txt) {
try {
if (isTime && rangeUtil.isValidTimeSpan(txt)) {
val = rangeUtil.intervalToMs(txt);
} else {
val = Number(txt);
}
} catch (err) {
console.warn('ERROR', err);
}
}
onChange(val);
};
const handleEnterKey = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key !== 'Enter') {
return;
}
checkAndUpdate(e.currentTarget.value);
};
const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {
checkAndUpdate(e.currentTarget.value);
};
const prefix =
inputPrefix === InputPrefix.GreaterThan ? (
<div>&gt;</div>
) : inputPrefix === InputPrefix.LessThan ? (
<div>&lt;</div>
) : null;
return (
<Input
autoFocus={false}
placeholder="never"
width={10}
defaultValue={defaultValue}
onKeyDown={handleEnterKey}
onBlur={handleBlur}
prefix={prefix}
spellCheck={false}
/>
);
};