-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lens] Refactor field select component as shared (#134773)
* ♻️ Refactor field select component as shared * 🚨 Fix linting + types issues * 🐛 Fix issue with classes * 🏷️ Fix type issue Co-authored-by: Joe Reuter <johannes.reuter@elastic.co> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
0060abf
commit 05b5c91
Showing
15 changed files
with
222 additions
and
118 deletions.
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
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; | ||
} |
136 changes: 136 additions & 0 deletions
136
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,136 @@ | ||
/* | ||
* 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 }) => { | ||
if (otherAttr.options) { | ||
return { | ||
...otherAttr, | ||
compatible, | ||
exists, | ||
options: otherAttr.options.map((fieldOption) => ({ | ||
...fieldOption, | ||
className: classNames({ | ||
'lnFieldPicker__option--incompatible': !fieldOption.compatible, | ||
'lnFieldPicker__option--nonExistant': !fieldOption.exists, | ||
}), | ||
})), | ||
}; | ||
} | ||
return { | ||
...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.
Oops, something went wrong.