-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[discover] De-angularize index pattern selection #46347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kertal
merged 12 commits into
elastic:master
from
kertal:kertal-pr-2019-09-23-discover-index-pattern-select
Sep 30, 2019
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
48224e0
DiscoverIndexPattern react component
kertal 67f16de
Merge remote-tracking branch 'upstream/master' into kertal-pr-2019-09…
kertal 8ba7d8a
Implement usage of the IndexPattern select
kertal 666fb29
Implementation of change link
kertal 62e27e6
Adapt tests
kertal 5b9f497
Merge remote-tracking branch 'upstream/master' into kertal-pr-2019-09…
kertal 754d589
Catch console warning when hiding/removing the EuiCombo
kertal adbd509
add comments + improve variable naming
kertal e09bbba
sidebar style updates
ryankeairns 788c53b
Merge pull request #2 from ryankeairns/kertal-pr-2019-09-23-discover-…
kertal 1d7a878
Merge branch 'kertal-pr-2019-09-23-discover-index-pattern-select' of …
kertal 3a3d885
Merge branch 'master' into kertal-pr-2019-09-23-discover-index-patter…
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...blic/discover/components/field_chooser/__snapshots__/discover_index_pattern.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
...e_plugins/kibana/public/discover/components/field_chooser/discover_index_pattern.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import React from 'react'; | ||
| import { shallowWithIntl, mountWithIntl } from 'test_utils/enzyme_helpers'; | ||
| // @ts-ignore | ||
| import { findTestSubject } from '@elastic/eui/lib/test'; | ||
| import { SavedObject } from 'kibana/server'; | ||
| import { DiscoverIndexPattern, DiscoverIndexPatternProps } from './discover_index_pattern'; | ||
| import { comboBoxKeyCodes } from '@elastic/eui'; | ||
|
|
||
| const indexPattern1 = { | ||
| id: 'test1', | ||
| attributes: { | ||
| title: 'test1', | ||
| }, | ||
| } as SavedObject; | ||
|
|
||
| const indexPattern2 = { | ||
| id: 'test2', | ||
| attributes: { | ||
| title: 'test2', | ||
| }, | ||
| } as SavedObject; | ||
|
|
||
| describe('DiscoverIndexPattern', () => { | ||
| test('Invalid props dont cause an exception', () => { | ||
| const props = { | ||
| indexPatternList: null, | ||
| selectedIndexPattern: null, | ||
| setIndexPattern: jest.fn(), | ||
| } as any; | ||
|
|
||
| expect(shallowWithIntl(<DiscoverIndexPattern {...props} />)).toMatchSnapshot(`""`); | ||
| }); | ||
| test('A single index pattern is just displayed', () => { | ||
| const props = { | ||
| indexPatternList: [indexPattern1], | ||
| selectedIndexPattern: indexPattern1, | ||
| setIndexPattern: jest.fn(), | ||
| } as DiscoverIndexPatternProps; | ||
|
|
||
| expect(shallowWithIntl(<DiscoverIndexPattern {...props} />)).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('Multiple index patterns are selectable', () => { | ||
| const props = { | ||
| indexPatternList: [indexPattern1, indexPattern2], | ||
| selectedIndexPattern: indexPattern2, | ||
| setIndexPattern: jest.fn(), | ||
| } as DiscoverIndexPatternProps; | ||
| const component = mountWithIntl(<DiscoverIndexPattern {...props} />); | ||
| findTestSubject(component, 'indexPattern-switch-link').simulate('click'); | ||
|
|
||
| const searchInput = findTestSubject(component, 'comboBoxSearchInput'); | ||
| searchInput.simulate('change', { target: { value: 'test1' } }); | ||
| searchInput.simulate('keyDown', { keyCode: comboBoxKeyCodes.ENTER }); | ||
| expect(props.setIndexPattern).toBeCalledWith('test1'); | ||
| }); | ||
| }); |
107 changes: 107 additions & 0 deletions
107
...y/core_plugins/kibana/public/discover/components/field_chooser/discover_index_pattern.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import React, { useState } from 'react'; | ||
| import { EuiComboBox } from '@elastic/eui'; | ||
| import { SavedObject } from 'kibana/server'; | ||
| import { DiscoverIndexPatternTitle } from './discover_index_pattern_title'; | ||
|
|
||
| export interface DiscoverIndexPatternProps { | ||
| /** | ||
| * list of available index patterns, if length > 1, component offers a "change" link | ||
| */ | ||
| indexPatternList: SavedObject[]; | ||
| /** | ||
| * currently selected index pattern, due to angular issues it's undefined at first rendering | ||
| */ | ||
| selectedIndexPattern: SavedObject; | ||
| /** | ||
| * triggered when user selects a new index pattern | ||
| */ | ||
| setIndexPattern: (id: string) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Component allows you to select an index pattern in discovers side bar | ||
| */ | ||
| export function DiscoverIndexPattern({ | ||
| indexPatternList, | ||
| selectedIndexPattern, | ||
| setIndexPattern, | ||
| }: DiscoverIndexPatternProps) { | ||
| if (!indexPatternList || indexPatternList.length === 0 || !selectedIndexPattern) { | ||
| // just in case, shouldn't happen | ||
| return null; | ||
| } | ||
| const [selected, setSelected] = useState(selectedIndexPattern); | ||
| const [showCombo, setShowCombo] = useState(false); | ||
| const options = indexPatternList.map(entity => ({ | ||
| value: entity.id, | ||
| label: entity.attributes!.title, | ||
| })); | ||
| const selectedOptions = selected | ||
| ? [{ value: selected.id, label: selected.attributes.title }] | ||
| : []; | ||
|
|
||
| const findIndexPattern = (id?: string) => indexPatternList.find(entity => entity.id === id); | ||
|
|
||
| if (!showCombo) { | ||
| return ( | ||
| <DiscoverIndexPatternTitle | ||
| isChangeable={indexPatternList.length > 1} | ||
| onChange={() => setShowCombo(true)} | ||
| title={selected.attributes ? selected.attributes.title : ''} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * catches a EuiComboBox related 'Can't perform a React state update on an unmounted component' | ||
| * warning in console by delaying the hiding/removal of the EuiComboBox a bit | ||
| */ | ||
| function hideCombo() { | ||
| setTimeout(() => setShowCombo(false), 50); | ||
| } | ||
|
|
||
| return ( | ||
| <EuiComboBox | ||
| className="index-pattern-selection" | ||
| data-test-subj="index-pattern-selection" | ||
| fullWidth={true} | ||
| isClearable={false} | ||
| onBlur={() => hideCombo()} | ||
| onChange={choices => { | ||
| const newSelected = choices[0] && findIndexPattern(choices[0].value); | ||
| if (newSelected) { | ||
| setSelected(newSelected); | ||
| setIndexPattern(newSelected.id); | ||
| } | ||
| hideCombo(); | ||
| }} | ||
| inputRef={el => { | ||
| // auto focus input element when combo box is displayed | ||
| if (el) { | ||
| el.focus(); | ||
| } | ||
| }} | ||
| options={options} | ||
| selectedOptions={selectedOptions} | ||
| singleSelection={{ asPlainText: true }} | ||
| /> | ||
| ); | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
...ugins/kibana/public/discover/components/field_chooser/discover_index_pattern_directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| // @ts-ignore | ||
| import { uiModules } from 'ui/modules'; | ||
| import { wrapInI18nContext } from 'ui/i18n'; | ||
| import { DiscoverIndexPattern } from './discover_index_pattern'; | ||
|
|
||
| const app = uiModules.get('apps/discover'); | ||
|
|
||
| app.directive('discoverIndexPatternSelect', function(reactDirective: any) { | ||
| return reactDirective(wrapInI18nContext(DiscoverIndexPattern), [ | ||
| ['indexPatternList', { watchDepth: 'reference' }], | ||
| ['selectedIndexPattern', { watchDepth: 'reference' }], | ||
| ['setIndexPattern', { watchDepth: 'reference' }], | ||
| ]); | ||
| }); |
85 changes: 85 additions & 0 deletions
85
..._plugins/kibana/public/discover/components/field_chooser/discover_index_pattern_title.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import React from 'react'; | ||
| import { EuiToolTip, EuiFlexItem, EuiFlexGroup, EuiTitle, EuiButtonEmpty } from '@elastic/eui'; | ||
|
|
||
| import { FormattedMessage } from '@kbn/i18n/react'; | ||
|
|
||
| export interface DiscoverIndexPatternTitleProps { | ||
| /** | ||
| * determines whether the change link is displayed | ||
| */ | ||
| isChangeable: boolean; | ||
| /** | ||
| * function triggered when the change link is clicked | ||
| */ | ||
| onChange: () => void; | ||
| /** | ||
| * title of the current index pattern | ||
| */ | ||
| title: string; | ||
| } | ||
|
|
||
| /** | ||
| * Component displaying the title of the current selected index pattern | ||
| * and if changeable is true, a link is provided to change the index pattern | ||
| */ | ||
| export function DiscoverIndexPatternTitle({ | ||
| isChangeable, | ||
| onChange, | ||
| title, | ||
| }: DiscoverIndexPatternTitleProps) { | ||
| return ( | ||
| <EuiFlexGroup gutterSize="none" responsive={false} className="index-pattern-selection"> | ||
| <EuiFlexItem className="eui-textTruncate"> | ||
| <EuiToolTip content={title}> | ||
| <EuiTitle size="xxs" className="eui-textTruncate"> | ||
| <h2 title={title}>{title}</h2> | ||
| </EuiTitle> | ||
| </EuiToolTip> | ||
| </EuiFlexItem> | ||
| {isChangeable && ( | ||
| <EuiFlexItem grow={false}> | ||
| <EuiToolTip | ||
| content={ | ||
| <FormattedMessage | ||
| id="kbn.discover.fieldChooser.indexPattern.changeLinkTooltip" | ||
| defaultMessage="Change current index pattern" | ||
| /> | ||
| } | ||
| > | ||
| <EuiButtonEmpty | ||
| data-test-subj="indexPattern-switch-link" | ||
| size="xs" | ||
| onClick={() => onChange()} | ||
| > | ||
| ( | ||
| <FormattedMessage | ||
| id="kbn.discover.fieldChooser.indexPattern.changeLinkLabel" | ||
| defaultMessage="change" | ||
| description="should be a short word since lack of space" | ||
| /> | ||
| ) | ||
| </EuiButtonEmpty> | ||
| </EuiToolTip> | ||
| </EuiFlexItem> | ||
| )} | ||
| </EuiFlexGroup> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch!