mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
UI: Use SelectableValue as Segment value (#20867)
* Use SelectableValue for segment value * Update cloudwatch components to use new segment props
This commit is contained in:
@@ -34,15 +34,17 @@ export const Dimensions: FunctionComponent<Props> = ({ dimensions, loadValues, l
|
||||
return options.filter(({ value }) => !Object.keys(data).includes(value));
|
||||
};
|
||||
|
||||
const toOption = (value: any) => ({ label: value, value });
|
||||
|
||||
return (
|
||||
<>
|
||||
{Object.entries(data).map(([key, value], index) => (
|
||||
<Fragment key={index}>
|
||||
<SegmentAsync
|
||||
allowCustomValue
|
||||
value={key}
|
||||
value={toOption(key)}
|
||||
loadOptions={() => loadKeys().then(keys => [removeOption, ...excludeUsedKeys(keys)])}
|
||||
onChange={newKey => {
|
||||
onChange={({ value: newKey }) => {
|
||||
const { [key]: value, ...newDimensions } = data;
|
||||
if (newKey === removeText) {
|
||||
setData({ ...newDimensions });
|
||||
@@ -54,9 +56,9 @@ export const Dimensions: FunctionComponent<Props> = ({ dimensions, loadValues, l
|
||||
<label className="gf-form-label query-segment-operator">=</label>
|
||||
<SegmentAsync
|
||||
allowCustomValue
|
||||
value={value || 'select dimension value'}
|
||||
value={toOption(value || 'select dimension value')}
|
||||
loadOptions={() => loadValues(key)}
|
||||
onChange={newValue => setData({ ...data, [key]: newValue })}
|
||||
onChange={({ value: newValue }) => setData({ ...data, [key]: newValue })}
|
||||
/>
|
||||
{Object.values(data).length > 1 && index + 1 !== Object.values(data).length && (
|
||||
<label className="gf-form-label query-keyword">AND</label>
|
||||
@@ -72,7 +74,7 @@ export const Dimensions: FunctionComponent<Props> = ({ dimensions, loadValues, l
|
||||
</a>
|
||||
}
|
||||
loadOptions={() => loadKeys().then(excludeUsedKeys)}
|
||||
onChange={(newKey: string) => setData({ ...data, [newKey]: '' })}
|
||||
onChange={({ value: newKey }) => setData({ ...data, [newKey]: '' })}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
|
||||
@@ -112,10 +112,10 @@ export class QueryEditor extends PureComponent<Props, State> {
|
||||
<>
|
||||
<QueryInlineField label="Region">
|
||||
<Segment
|
||||
value={query.region || 'Select region'}
|
||||
value={this.toOption(query.region || 'Select region')}
|
||||
options={regions}
|
||||
allowCustomValue
|
||||
onChange={region => this.onChange({ ...query, region })}
|
||||
onChange={({ value: region }) => this.onChange({ ...query, region })}
|
||||
/>
|
||||
</QueryInlineField>
|
||||
|
||||
@@ -123,19 +123,19 @@ export class QueryEditor extends PureComponent<Props, State> {
|
||||
<>
|
||||
<QueryInlineField label="Namespace">
|
||||
<Segment
|
||||
value={query.namespace || 'Select namespace'}
|
||||
value={this.toOption(query.namespace || 'Select namespace')}
|
||||
allowCustomValue
|
||||
options={namespaces}
|
||||
onChange={namespace => this.onChange({ ...query, namespace })}
|
||||
onChange={({ value: namespace }) => this.onChange({ ...query, namespace })}
|
||||
/>
|
||||
</QueryInlineField>
|
||||
|
||||
<QueryInlineField label="Metric Name">
|
||||
<SegmentAsync
|
||||
value={query.metricName || 'Select metric name'}
|
||||
value={this.toOption(query.metricName || 'Select metric name')}
|
||||
allowCustomValue
|
||||
loadOptions={this.loadMetricNames}
|
||||
onChange={metricName => this.onChange({ ...query, metricName })}
|
||||
onChange={({ value: metricName }) => this.onChange({ ...query, metricName })}
|
||||
/>
|
||||
</QueryInlineField>
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ export interface Props {
|
||||
|
||||
const removeText = '-- remove stat --';
|
||||
const removeOption: SelectableValue<string> = { label: removeText, value: removeText };
|
||||
const toOption = (value: any) => ({ label: value, value });
|
||||
|
||||
export const Stats: FunctionComponent<Props> = ({ stats, values, onChange, variableOptionGroup }) => (
|
||||
<>
|
||||
@@ -20,9 +21,9 @@ export const Stats: FunctionComponent<Props> = ({ stats, values, onChange, varia
|
||||
<Segment
|
||||
allowCustomValue
|
||||
key={value + index}
|
||||
value={value}
|
||||
value={toOption(value)}
|
||||
options={[removeOption, ...stats, variableOptionGroup]}
|
||||
onChange={value =>
|
||||
onChange={({ value }) =>
|
||||
onChange(
|
||||
value === removeText
|
||||
? values.filter((_, i) => i !== index)
|
||||
@@ -39,7 +40,7 @@ export const Stats: FunctionComponent<Props> = ({ stats, values, onChange, varia
|
||||
</a>
|
||||
}
|
||||
allowCustomValue
|
||||
onChange={(value: string) => onChange([...values, value])}
|
||||
onChange={({ value }) => onChange([...values, value])}
|
||||
options={[...stats.filter(({ value }) => !values.includes(value)), variableOptionGroup]}
|
||||
/>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user