mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
* Working on the all value * Support for custom allValue * Fixes * More progress * Progress * Updated * Fixed issue with multi and All value * Clarified tests
51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { Observable, of } from 'rxjs';
|
|
|
|
import { SceneComponentProps } from '../../core/types';
|
|
import { VariableDependencyConfig } from '../VariableDependencyConfig';
|
|
import { renderSelectForVariable } from '../components/VariableValueSelect';
|
|
import { VariableValueOption } from '../types';
|
|
|
|
import { MultiValueVariable, MultiValueVariableState, VariableGetOptionsArgs } from './MultiValueVariable';
|
|
|
|
export interface CustomVariableState extends MultiValueVariableState {
|
|
query: string;
|
|
}
|
|
|
|
export class CustomVariable extends MultiValueVariable<CustomVariableState> {
|
|
protected _variableDependency = new VariableDependencyConfig(this, {
|
|
statePaths: ['query'],
|
|
});
|
|
|
|
public constructor(initialState: Partial<CustomVariableState>) {
|
|
super({
|
|
type: 'custom',
|
|
query: '',
|
|
value: '',
|
|
text: '',
|
|
options: [],
|
|
name: '',
|
|
...initialState,
|
|
});
|
|
}
|
|
|
|
public getValueOptions(args: VariableGetOptionsArgs): Observable<VariableValueOption[]> {
|
|
const match = this.state.query.match(/(?:\\,|[^,])+/g) ?? [];
|
|
const options = match.map((text) => {
|
|
text = text.replace(/\\,/g, ',');
|
|
const textMatch = /^(.+)\s:\s(.+)$/g.exec(text) ?? [];
|
|
if (textMatch.length === 3) {
|
|
const [, key, value] = textMatch;
|
|
return { label: key.trim(), value: value.trim() };
|
|
} else {
|
|
return { label: text.trim(), value: text.trim() };
|
|
}
|
|
});
|
|
|
|
return of(options);
|
|
}
|
|
|
|
public static Component = ({ model }: SceneComponentProps<MultiValueVariable>) => {
|
|
return renderSelectForVariable(model);
|
|
};
|
|
}
|