grafana/public/app/features/scenes/variables/variants/CustomVariable.tsx
Torkel Ödegaard ef46761b9a
Scene: Variables and All value support (#59635)
* Working on the all value

* Support for custom allValue

* Fixes

* More progress

* Progress

* Updated

* Fixed issue with multi and All value

* Clarified tests
2022-12-13 08:20:09 +01:00

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);
};
}