mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
115 lines
3.3 KiB
TypeScript
115 lines
3.3 KiB
TypeScript
/*
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
* 2.0.
|
|
*/
|
|
|
|
import { i18n } from '@kbn/i18n';
|
|
import React, { useState } from 'react';
|
|
import { EuiPopover, EuiPopoverTitle, EuiSelectable, EuiSelectableProps } from '@elastic/eui';
|
|
import { IndexPatternRef } from './types';
|
|
import { trackUiEvent } from '../lens_ui_telemetry';
|
|
import { ToolbarButton, ToolbarButtonProps } from '../../../../../src/plugins/kibana_react/public';
|
|
|
|
export type ChangeIndexPatternTriggerProps = ToolbarButtonProps & {
|
|
label: string;
|
|
title?: string;
|
|
};
|
|
|
|
export function ChangeIndexPattern({
|
|
indexPatternRefs,
|
|
indexPatternId,
|
|
onChangeIndexPattern,
|
|
trigger,
|
|
selectableProps,
|
|
}: {
|
|
trigger: ChangeIndexPatternTriggerProps;
|
|
indexPatternRefs: IndexPatternRef[];
|
|
onChangeIndexPattern: (newId: string) => void;
|
|
indexPatternId?: string;
|
|
selectableProps?: EuiSelectableProps;
|
|
}) {
|
|
const [isPopoverOpen, setPopoverIsOpen] = useState(false);
|
|
|
|
const isMissingCurrent = !indexPatternRefs.some(({ id }) => id === indexPatternId);
|
|
|
|
// be careful to only add color with a value, otherwise it will fallbacks to "primary"
|
|
const colorProp = isMissingCurrent
|
|
? {
|
|
color: 'danger' as const,
|
|
}
|
|
: {};
|
|
|
|
const createTrigger = function () {
|
|
const { label, title, ...rest } = trigger;
|
|
return (
|
|
<ToolbarButton
|
|
title={title}
|
|
onClick={() => setPopoverIsOpen(!isPopoverOpen)}
|
|
fullWidth
|
|
{...colorProp}
|
|
{...rest}
|
|
>
|
|
{label}
|
|
</ToolbarButton>
|
|
);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<EuiPopover
|
|
panelClassName="lnsChangeIndexPatternPopover"
|
|
button={createTrigger()}
|
|
isOpen={isPopoverOpen}
|
|
closePopover={() => setPopoverIsOpen(false)}
|
|
display="block"
|
|
panelPaddingSize="s"
|
|
ownFocus
|
|
>
|
|
<div>
|
|
<EuiPopoverTitle>
|
|
{i18n.translate('xpack.lens.indexPattern.changeIndexPatternTitle', {
|
|
defaultMessage: 'Change index pattern',
|
|
})}
|
|
</EuiPopoverTitle>
|
|
<EuiSelectable<{
|
|
key?: string;
|
|
label: string;
|
|
value?: string;
|
|
checked?: 'on' | 'off' | undefined;
|
|
}>
|
|
{...selectableProps}
|
|
searchable
|
|
singleSelection="always"
|
|
options={indexPatternRefs.map(({ title, id }) => ({
|
|
key: id,
|
|
label: title,
|
|
value: id,
|
|
checked: id === indexPatternId ? 'on' : undefined,
|
|
}))}
|
|
onChange={(choices) => {
|
|
const choice = (choices.find(({ checked }) => checked) as unknown) as {
|
|
value: string;
|
|
};
|
|
trackUiEvent('indexpattern_changed');
|
|
onChangeIndexPattern(choice.value);
|
|
setPopoverIsOpen(false);
|
|
}}
|
|
searchProps={{
|
|
compressed: true,
|
|
...(selectableProps ? selectableProps.searchProps : undefined),
|
|
}}
|
|
>
|
|
{(list, search) => (
|
|
<>
|
|
{search}
|
|
{list}
|
|
</>
|
|
)}
|
|
</EuiSelectable>
|
|
</div>
|
|
</EuiPopover>
|
|
</>
|
|
);
|
|
}
|