2020-04-21 02:29:32 +08:00
|
|
|
import React, { useCallback, useState } from 'react';
|
2020-02-12 19:54:58 +08:00
|
|
|
import { css } from 'emotion';
|
|
|
|
|
import { GrafanaTheme, PanelPlugin, PanelPluginMeta } from '@grafana/data';
|
2020-04-26 19:16:31 +08:00
|
|
|
import { useTheme, stylesFactory, Icon, Input } from '@grafana/ui';
|
2020-02-12 19:54:58 +08:00
|
|
|
import { changePanelPlugin } from '../../state/actions';
|
|
|
|
|
import { StoreState } from 'app/types';
|
|
|
|
|
import { PanelModel } from '../../state/PanelModel';
|
|
|
|
|
import { connect, MapStateToProps, MapDispatchToProps } from 'react-redux';
|
2020-04-06 21:06:41 +08:00
|
|
|
import { VizTypePicker, getAllPanelPluginMeta, filterPluginList } from '../../panel_editor/VizTypePicker';
|
2020-04-06 14:22:49 +08:00
|
|
|
import { Field } from '@grafana/ui/src/components/Forms/Field';
|
2020-02-12 19:54:58 +08:00
|
|
|
|
|
|
|
|
interface OwnProps {
|
|
|
|
|
panel: PanelModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ConnectedProps {
|
|
|
|
|
plugin?: PanelPlugin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface DispatchProps {
|
|
|
|
|
changePanelPlugin: typeof changePanelPlugin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Props = OwnProps & ConnectedProps & DispatchProps;
|
|
|
|
|
|
2020-04-21 02:29:32 +08:00
|
|
|
export const VisualizationTabUnconnected = React.forwardRef<HTMLInputElement, Props>(
|
|
|
|
|
({ panel, plugin, changePanelPlugin }, ref) => {
|
|
|
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const styles = getStyles(theme);
|
2020-02-12 19:54:58 +08:00
|
|
|
|
2020-04-21 02:29:32 +08:00
|
|
|
if (!plugin) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-02-12 19:54:58 +08:00
|
|
|
|
2020-04-21 02:29:32 +08:00
|
|
|
const onPluginTypeChange = (meta: PanelPluginMeta) => {
|
|
|
|
|
changePanelPlugin(panel, meta.id);
|
|
|
|
|
};
|
2020-04-06 21:06:41 +08:00
|
|
|
|
2020-04-21 02:29:32 +08:00
|
|
|
const onKeyPress = useCallback(
|
|
|
|
|
(e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
|
const query = e.currentTarget.value;
|
|
|
|
|
const plugins = getAllPanelPluginMeta();
|
|
|
|
|
const match = filterPluginList(plugins, query, plugin.meta);
|
|
|
|
|
if (match && match.length) {
|
|
|
|
|
onPluginTypeChange(match[0]);
|
|
|
|
|
}
|
2020-04-06 21:06:41 +08:00
|
|
|
}
|
2020-04-21 02:29:32 +08:00
|
|
|
},
|
|
|
|
|
[onPluginTypeChange]
|
|
|
|
|
);
|
2020-04-06 21:06:41 +08:00
|
|
|
|
2020-04-21 02:29:32 +08:00
|
|
|
const suffix =
|
|
|
|
|
searchQuery !== '' ? (
|
|
|
|
|
<span className={styles.searchClear} onClick={() => setSearchQuery('')}>
|
|
|
|
|
<Icon name="times" />
|
|
|
|
|
Clear filter
|
|
|
|
|
</span>
|
|
|
|
|
) : null;
|
2020-02-12 19:54:58 +08:00
|
|
|
|
2020-04-21 02:29:32 +08:00
|
|
|
return (
|
|
|
|
|
<div className={styles.wrapper}>
|
2020-04-26 19:16:31 +08:00
|
|
|
<Field>
|
|
|
|
|
<Input
|
|
|
|
|
value={searchQuery}
|
|
|
|
|
onChange={e => setSearchQuery(e.currentTarget.value)}
|
|
|
|
|
onKeyPress={onKeyPress}
|
|
|
|
|
prefix={<Icon name="filter" className={styles.icon} />}
|
|
|
|
|
suffix={suffix}
|
|
|
|
|
placeholder="Filter visualisations"
|
|
|
|
|
ref={ref}
|
|
|
|
|
/>
|
|
|
|
|
</Field>
|
|
|
|
|
|
|
|
|
|
<VizTypePicker
|
|
|
|
|
current={plugin.meta}
|
|
|
|
|
onTypeChange={onPluginTypeChange}
|
|
|
|
|
searchQuery={searchQuery}
|
|
|
|
|
onClose={() => {}}
|
|
|
|
|
/>
|
2020-04-02 04:09:14 +08:00
|
|
|
</div>
|
2020-04-21 02:29:32 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
2020-02-12 19:54:58 +08:00
|
|
|
const getStyles = stylesFactory((theme: GrafanaTheme) => {
|
|
|
|
|
return {
|
2020-04-02 04:09:14 +08:00
|
|
|
icon: css`
|
2020-04-12 21:05:49 +08:00
|
|
|
color: ${theme.palette.gray33};
|
2020-04-02 04:09:14 +08:00
|
|
|
`,
|
2020-02-12 19:54:58 +08:00
|
|
|
wrapper: css`
|
2020-04-02 04:09:14 +08:00
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
`,
|
2020-04-03 14:39:57 +08:00
|
|
|
searchClear: css`
|
2020-04-12 21:05:49 +08:00
|
|
|
color: ${theme.palette.gray60};
|
2020-04-03 14:39:57 +08:00
|
|
|
cursor: pointer;
|
|
|
|
|
`,
|
2020-02-12 19:54:58 +08:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, props) => {
|
|
|
|
|
return {
|
|
|
|
|
plugin: state.plugins.panels[props.panel.type],
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = { changePanelPlugin };
|
|
|
|
|
|
2020-04-21 02:29:32 +08:00
|
|
|
export const VisualizationTab = connect(mapStateToProps, mapDispatchToProps, undefined, { forwardRef: true })(
|
|
|
|
|
VisualizationTabUnconnected
|
|
|
|
|
);
|