mirror of
https://github.com/grafana/grafana.git
synced 2025-09-23 18:52:33 +08:00

* Chore: reduces a lot of variable errors * Chore: reduces variable Editor errors * Chore: reduces variable Picker errors * Chore: reduce error count * Chore: reduces errors for ChangeEvent instead of FormEvent * Chore: reduces errors with CombinedState * Chore: reduces ComponentType errors * Chore: reduce errors in reducers * Chore: reduces misc errors * Chore: reduce AdhocPicker errors * Chore: reduce error limit * Update public/app/features/variables/adhoc/picker/AdHocFilterValue.tsx Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com> * Chore: updates after PR comments * Chore: small refactor Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>
89 lines
2.7 KiB
TypeScript
89 lines
2.7 KiB
TypeScript
import React, { PureComponent, ReactNode } from 'react';
|
|
import { connect, ConnectedProps } from 'react-redux';
|
|
import { AdHocVariableFilter, AdHocVariableModel } from 'app/features/variables/types';
|
|
import { VariablePickerProps } from '../../pickers/types';
|
|
import { SelectableValue } from '@grafana/data';
|
|
import { AdHocFilterBuilder } from './AdHocFilterBuilder';
|
|
import { ConditionSegment } from './ConditionSegment';
|
|
import { addFilter, changeFilter, removeFilter } from '../actions';
|
|
import { REMOVE_FILTER_KEY } from './AdHocFilterKey';
|
|
import { AdHocFilterRenderer } from './AdHocFilterRenderer';
|
|
|
|
const mapDispatchToProps = {
|
|
addFilter,
|
|
removeFilter,
|
|
changeFilter,
|
|
};
|
|
|
|
const connector = connect(null, mapDispatchToProps);
|
|
|
|
interface OwnProps extends VariablePickerProps<AdHocVariableModel> {}
|
|
|
|
type Props = OwnProps & ConnectedProps<typeof connector>;
|
|
|
|
export class AdHocPickerUnconnected extends PureComponent<Props> {
|
|
onChange = (index: number, prop: string) => (key: SelectableValue<string | null>) => {
|
|
const { id, filters } = this.props.variable;
|
|
const { value } = key;
|
|
|
|
if (key.value === REMOVE_FILTER_KEY) {
|
|
return this.props.removeFilter(id, index);
|
|
}
|
|
|
|
return this.props.changeFilter(id, {
|
|
index,
|
|
filter: {
|
|
...filters[index],
|
|
[prop]: value,
|
|
},
|
|
});
|
|
};
|
|
|
|
appendFilterToVariable = (filter: AdHocVariableFilter) => {
|
|
const { id } = this.props.variable;
|
|
this.props.addFilter(id, filter);
|
|
};
|
|
|
|
render() {
|
|
const { filters } = this.props.variable;
|
|
|
|
return (
|
|
<div className="gf-form-inline">
|
|
{this.renderFilters(filters)}
|
|
<AdHocFilterBuilder
|
|
datasource={this.props.variable.datasource!}
|
|
appendBefore={filters.length > 0 ? <ConditionSegment label="AND" /> : null}
|
|
onCompleted={this.appendFilterToVariable}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
renderFilters(filters: AdHocVariableFilter[]) {
|
|
return filters.reduce((segments: ReactNode[], filter, index) => {
|
|
if (segments.length > 0) {
|
|
segments.push(<ConditionSegment label="AND" key={`condition-${index}`} />);
|
|
}
|
|
segments.push(this.renderFilterSegments(filter, index));
|
|
return segments;
|
|
}, []);
|
|
}
|
|
|
|
renderFilterSegments(filter: AdHocVariableFilter, index: number) {
|
|
return (
|
|
<React.Fragment key={`filter-${index}`}>
|
|
<AdHocFilterRenderer
|
|
datasource={this.props.variable.datasource!}
|
|
filter={filter}
|
|
onKeyChange={this.onChange(index, 'key')}
|
|
onOperatorChange={this.onChange(index, 'operator')}
|
|
onValueChange={this.onChange(index, 'value')}
|
|
/>
|
|
</React.Fragment>
|
|
);
|
|
}
|
|
}
|
|
|
|
export const AdHocPicker = connector(AdHocPickerUnconnected);
|
|
AdHocPicker.displayName = 'AdHocPicker';
|