-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Lens] Refactor field select component as shared #134773
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
923add6
:recycle: Refactor field select component as shared
dej611 8fee29b
:rotating_light: Fix linting + types issues
dej611 0fc08ee
Merge remote-tracking branch 'upstream/main' into feature/shared_fiel…
dej611 2cfa155
Merge branch 'main' into feature/shared_field_picker
flash1293 4b26b63
Merge branch 'main' into feature/shared_field_picker
flash1293 096338f
:bug: Fix issue with classes
dej611 5016bdc
Merge remote-tracking branch 'upstream/main' into feature/shared_fiel…
dej611 e125297
Merge remote-tracking branch 'upstream/main' into feature/shared_fiel…
dej611 bb72cc7
:label: Fix type issue
dej611 9f028e0
Merge branch 'main' into feature/shared_field_picker
kibanamachine e856d88
Merge branch 'main' into feature/shared_field_picker
kibanamachine 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 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
This file contains 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
This file contains 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
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
x-pack/plugins/lens/public/shared_components/field_picker/field_picker.scss
This file contains 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,7 @@ | ||
.lnFieldPicker__option--incompatible { | ||
color: $euiColorLightShade; | ||
} | ||
|
||
.lnFieldPicker__option--nonExistant { | ||
background-color: $euiColorLightestShade; | ||
} |
120 changes: 120 additions & 0 deletions
120
x-pack/plugins/lens/public/shared_components/field_picker/field_picker.tsx
This file contains 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,120 @@ | ||
/* | ||
* 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 './field_picker.scss'; | ||
import React, { useRef } from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import useEffectOnce from 'react-use/lib/useEffectOnce'; | ||
import { EuiComboBox, EuiComboBoxProps, EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; | ||
import classNames from 'classnames'; | ||
import { DataType } from '../../types'; | ||
import { LensFieldIcon } from './lens_field_icon'; | ||
import { TruncatedLabel } from './truncated_label'; | ||
import type { FieldOptionValue, FieldOption } from './types'; | ||
|
||
export interface FieldPickerProps<T extends FieldOptionValue> | ||
extends EuiComboBoxProps<FieldOption<T>['value']> { | ||
options: Array<FieldOption<T>>; | ||
selectedField?: string; | ||
onChoose: (choice: T | undefined) => void; | ||
onDelete?: () => void; | ||
fieldIsInvalid: boolean; | ||
'data-test-subj'?: string; | ||
} | ||
|
||
const DEFAULT_COMBOBOX_WIDTH = 305; | ||
const COMBOBOX_PADDINGS = 90; | ||
const DEFAULT_FONT = '14px Inter'; | ||
|
||
export function FieldPicker<T extends FieldOptionValue>({ | ||
selectedOptions, | ||
options, | ||
onChoose, | ||
onDelete, | ||
fieldIsInvalid, | ||
['data-test-subj']: dataTestSub, | ||
...rest | ||
}: FieldPickerProps<T>) { | ||
const styledOptions = options?.map(({ compatible, exists, ...otherAttr }) => ({ | ||
...otherAttr, | ||
compatible, | ||
exists, | ||
className: classNames({ | ||
'lnFieldPicker__option--incompatible': !compatible, | ||
'lnFieldPicker__option--nonExistant': !exists, | ||
}), | ||
})); | ||
const comboBoxRef = useRef<HTMLInputElement>(null); | ||
const [labelProps, setLabelProps] = React.useState<{ | ||
width: number; | ||
font: string; | ||
}>({ | ||
width: DEFAULT_COMBOBOX_WIDTH - COMBOBOX_PADDINGS, | ||
font: DEFAULT_FONT, | ||
}); | ||
|
||
const computeStyles = (_e: UIEvent | undefined, shouldRecomputeAll = false) => { | ||
if (comboBoxRef.current) { | ||
const current = { | ||
...labelProps, | ||
width: comboBoxRef.current?.clientWidth - COMBOBOX_PADDINGS, | ||
}; | ||
if (shouldRecomputeAll) { | ||
current.font = window.getComputedStyle(comboBoxRef.current).font; | ||
} | ||
setLabelProps(current); | ||
} | ||
}; | ||
|
||
useEffectOnce(() => { | ||
if (comboBoxRef.current) { | ||
computeStyles(undefined, true); | ||
} | ||
window.addEventListener('resize', computeStyles); | ||
}); | ||
|
||
return ( | ||
<div ref={comboBoxRef}> | ||
<EuiComboBox | ||
fullWidth | ||
compressed | ||
isClearable={false} | ||
data-test-subj={dataTestSub ?? 'indexPattern-dimension-field'} | ||
placeholder={i18n.translate('xpack.lens.fieldPicker.fieldPlaceholder', { | ||
defaultMessage: 'Field', | ||
})} | ||
options={styledOptions} | ||
isInvalid={fieldIsInvalid} | ||
selectedOptions={selectedOptions} | ||
singleSelection={{ asPlainText: true }} | ||
onChange={(choices) => { | ||
if (choices.length === 0) { | ||
onDelete?.(); | ||
return; | ||
} | ||
onChoose(choices[0].value); | ||
}} | ||
renderOption={(option, searchValue) => { | ||
return ( | ||
<EuiFlexGroup gutterSize="s" alignItems="center" responsive={false}> | ||
<EuiFlexItem grow={null}> | ||
<LensFieldIcon | ||
type={(option.value as unknown as { dataType: DataType }).dataType} | ||
fill="none" | ||
/> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<TruncatedLabel {...labelProps} label={option.label} search={searchValue} /> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
); | ||
}} | ||
{...rest} | ||
/> | ||
</div> | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
x-pack/plugins/lens/public/shared_components/field_picker/index.ts
This file contains 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,11 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { LensFieldIcon } from './lens_field_icon'; | ||
export { FieldPicker } from './field_picker'; | ||
export { TruncatedLabel } from './truncated_label'; | ||
export type { FieldOptionValue, FieldOption } from './types'; |
File renamed without changes.
This file contains 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
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions
22
x-pack/plugins/lens/public/shared_components/field_picker/types.ts
This file contains 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,22 @@ | ||
/* | ||
* 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 type { EuiComboBoxOptionOption } from '@elastic/eui'; | ||
import type { DataType } from '../../types'; | ||
|
||
export interface FieldOptionValue { | ||
type: 'field'; | ||
field: string; | ||
dataType?: DataType; | ||
} | ||
|
||
export interface FieldOption<T extends FieldOptionValue> extends EuiComboBoxOptionOption<T> { | ||
label: string; | ||
value: T; | ||
exists: boolean; | ||
compatible: number | boolean; | ||
'data-test-subj'?: string; | ||
} |
This file contains 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
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.
Are these classes being applied in the appropriate circumstances? When running locally, I noticed that some items in the field selector weren't being given these classes and accompanying color styling that they would have been given previously.
For example, in the screenshot below, I'd expect
@timestamp
andagent.keyword
fields to be given the incompatible treatment (as the "Median" function was selected).