-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSettingField.tsx
46 lines (39 loc) · 1.41 KB
/
SettingField.tsx
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
import { uniqueId } from 'lodash';
import React, { ComponentProps, useState } from 'react';
import { InlineField, Input } from '@grafana/ui';
import { useDispatch } from '@/hooks/useStatelessReducer';
import { MetricAggregationWithSettings } from '@/types';
import { SettingKeyOf } from '@/components/types';
import { changeMetricSetting } from '../state/actions';
interface Props<T extends MetricAggregationWithSettings, K extends SettingKeyOf<T>> {
label: string;
settingName: K;
metric: T;
placeholder?: ComponentProps<typeof Input>['placeholder'];
tooltip?: ComponentProps<typeof InlineField>['tooltip'];
}
export function SettingField<T extends MetricAggregationWithSettings, K extends SettingKeyOf<T>>({
label,
settingName,
metric,
placeholder,
tooltip,
}: Props<T, K>) {
const dispatch = useDispatch();
const [id] = useState(uniqueId(`es-field-id-`));
const settings = metric.settings;
let defaultValue = settings?.[settingName as keyof typeof settings] || '';
// if (settingName === 'script') {
// defaultValue = getScriptValue(metric as MetricAggregationWithInlineScript);
// }
return (
<InlineField label={label} labelWidth={16} tooltip={tooltip}>
<Input
id={id}
placeholder={placeholder}
onBlur={(e) => dispatch(changeMetricSetting({ metric, settingName, newValue: e.target.value }))}
defaultValue={defaultValue}
/>
</InlineField>
);
}