-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[Controls] Use EUI Selectable for Field search #151231
Changes from 5 commits
f28a995
1821464
4343026
6163811
100c713
d63e4f2
d5fa9b2
0b168f8
20c9c46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,4 @@ | ||
.presFieldPicker__fieldButton { | ||
background: $euiColorEmptyShade; | ||
} | ||
|
||
.presFieldPickerFieldButtonActive { | ||
box-shadow: 0 0 0 2px $euiColorPrimary; | ||
} | ||
|
||
.presFieldPicker__fieldPanel { | ||
height: 300px; | ||
overflow-y: scroll; | ||
} | ||
|
||
.presFieldPicker__container--disabled { | ||
opacity: .7; | ||
pointer-events: none; | ||
background-color: transparentize($euiColorPrimary, .9); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,14 @@ | |
|
||
import classNames from 'classnames'; | ||
import { sortBy, uniq } from 'lodash'; | ||
import React, { useState } from 'react'; | ||
import { FormattedMessage } from '@kbn/i18n-react'; | ||
import { EuiFlexGroup, EuiFlexItem, EuiPanel, EuiText } from '@elastic/eui'; | ||
import { FieldButton, FieldIcon } from '@kbn/react-field'; | ||
import React, { useEffect, useMemo, useState } from 'react'; | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import { FieldIcon } from '@kbn/react-field'; | ||
import { EuiSelectable, EuiSelectableOption, EuiSpacer } from '@elastic/eui'; | ||
import { DataView, DataViewField } from '@kbn/data-views-plugin/common'; | ||
import { FieldSearch } from './field_search'; | ||
|
||
import { FieldTypeFilter } from './field_type_filter'; | ||
|
||
import './field_picker.scss'; | ||
|
||
|
@@ -31,115 +32,94 @@ export const FieldPicker = ({ | |
filterPredicate, | ||
selectedFieldName, | ||
}: FieldPickerProps) => { | ||
const [nameFilter, setNameFilter] = useState<string>(''); | ||
const [typesFilter, setTypesFilter] = useState<string[]>([]); | ||
const [fieldSelectableOptions, setFieldSelectableOptions] = useState<EuiSelectableOption[]>([]); | ||
|
||
// Retrieve, filter, and sort fields from data view | ||
const fields = dataView | ||
? sortBy( | ||
dataView.fields | ||
.filter( | ||
(f) => | ||
f.name.toLowerCase().includes(nameFilter.toLowerCase()) && | ||
(typesFilter.length === 0 || typesFilter.includes(f.type as string)) | ||
) | ||
const availableFields = useMemo( | ||
() => | ||
sortBy( | ||
(dataView?.fields ?? []) | ||
.filter((f) => typesFilter.length === 0 || typesFilter.includes(f.type as string)) | ||
.filter((f) => (filterPredicate ? filterPredicate(f) : true)), | ||
['name'] | ||
) | ||
: []; | ||
), | ||
[dataView, filterPredicate, typesFilter] | ||
); | ||
|
||
useEffect(() => { | ||
if (!dataView) return; | ||
const options: EuiSelectableOption[] = (availableFields ?? []).map((field) => { | ||
return { | ||
key: field.name, | ||
label: field.displayName ?? field.name, | ||
className: classNames('presFieldPicker__fieldButton', { | ||
presFieldPickerFieldButtonActive: field.name === selectedFieldName, | ||
}), | ||
'data-test-subj': `field-picker-select-${field.name}`, | ||
prepend: <FieldIcon type={field.type} label={field.name} scripted={field.scripted} />, | ||
}; | ||
}); | ||
setFieldSelectableOptions(options); | ||
}, [availableFields, dataView, filterPredicate, selectedFieldName, typesFilter]); | ||
|
||
const uniqueTypes = dataView | ||
? uniq( | ||
dataView.fields | ||
.filter((f) => (filterPredicate ? filterPredicate(f) : true)) | ||
.map((f) => f.type as string) | ||
) | ||
: []; | ||
const uniqueTypes = useMemo( | ||
() => | ||
dataView | ||
? uniq( | ||
dataView.fields | ||
.filter((f) => (filterPredicate ? filterPredicate(f) : true)) | ||
.map((f) => f.type as string) | ||
) | ||
: [], | ||
[dataView, filterPredicate] | ||
); | ||
|
||
const fieldTypeFilter = ( | ||
<FieldTypeFilter | ||
onFieldTypesChange={(types) => setTypesFilter(types)} | ||
fieldTypesValue={typesFilter} | ||
availableFieldTypes={uniqueTypes} | ||
/> | ||
); | ||
|
||
return ( | ||
<EuiFlexGroup | ||
direction="column" | ||
alignItems="stretch" | ||
gutterSize="s" | ||
className={`presFieldPicker__container ${ | ||
!dataView && 'presFieldPicker__container--disabled' | ||
}`} | ||
<EuiSelectable | ||
emptyMessage={i18n.translate('presentationUtil.fieldPicker.noFieldsLabel', { | ||
defaultMessage: 'No matching fields', | ||
})} | ||
aria-label={i18n.translate('presentationUtil.fieldPicker.selectableAriaLabel', { | ||
defaultMessage: 'Select a field', | ||
})} | ||
searchable | ||
options={fieldSelectableOptions} | ||
onChange={(options, _, changedOption) => { | ||
setFieldSelectableOptions(options); | ||
if (!dataView || !changedOption.key) return; | ||
const field = dataView.getFieldByName(changedOption.key); | ||
if (field) onSelectField?.(field); | ||
}} | ||
searchProps={{ | ||
'data-test-subj': 'field-search-input', | ||
placeholder: i18n.translate('presentationUtil.fieldSearch.searchPlaceHolder', { | ||
defaultMessage: 'Search field names', | ||
}), | ||
}} | ||
listProps={{ | ||
isVirtualized: true, | ||
showIcons: false, | ||
Comment on lines
+115
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 on Hannah's suggestion, a border would be a nice addition There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I felt that the border looked a bit too noisy, but after seeing the screenshots - and knowing that y'all are down for the border - I'll add it! |
||
}} | ||
height={300} | ||
> | ||
<EuiFlexItem grow={false}> | ||
<FieldSearch | ||
onSearchChange={(val) => setNameFilter(val)} | ||
searchValue={nameFilter} | ||
onFieldTypesChange={(types) => setTypesFilter(types)} | ||
fieldTypesValue={typesFilter} | ||
availableFieldTypes={uniqueTypes} | ||
/> | ||
</EuiFlexItem> | ||
<EuiFlexItem grow={false}> | ||
<EuiPanel | ||
paddingSize="s" | ||
hasShadow={false} | ||
hasBorder={true} | ||
className="presFieldPicker__fieldPanel" | ||
> | ||
{fields.length > 0 && ( | ||
<EuiFlexGroup direction="column" gutterSize="none"> | ||
{fields.map((f, i) => { | ||
return ( | ||
<EuiFlexItem key={f.name}> | ||
<FieldButton | ||
data-test-subj={`field-picker-select-${f.name}`} | ||
className={classNames('presFieldPicker__fieldButton', { | ||
presFieldPickerFieldButtonActive: f.name === selectedFieldName, | ||
})} | ||
onClick={() => { | ||
onSelectField?.(f); | ||
}} | ||
isActive={f.name === selectedFieldName} | ||
fieldName={f.name} | ||
fieldIcon={<FieldIcon type={f.type} label={f.name} scripted={f.scripted} />} | ||
/> | ||
</EuiFlexItem> | ||
); | ||
})} | ||
</EuiFlexGroup> | ||
)} | ||
{!dataView && ( | ||
<EuiFlexGroup | ||
direction="column" | ||
gutterSize="none" | ||
alignItems="center" | ||
justifyContent="center" | ||
> | ||
<EuiFlexItem> | ||
<EuiText color="subdued"> | ||
<FormattedMessage | ||
id="presentationUtil.fieldPicker.noDataViewLabel" | ||
defaultMessage="No data view selected" | ||
/> | ||
</EuiText> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
)} | ||
{dataView && fields.length === 0 && ( | ||
<EuiFlexGroup | ||
direction="column" | ||
gutterSize="none" | ||
alignItems="center" | ||
justifyContent="center" | ||
> | ||
<EuiFlexItem> | ||
<EuiText color="subdued"> | ||
<FormattedMessage | ||
id="presentationUtil.fieldPicker.noFieldsLabel" | ||
defaultMessage="No matching fields" | ||
/> | ||
</EuiText> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
)} | ||
</EuiPanel> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
{(list, search) => ( | ||
<> | ||
{search} | ||
<EuiSpacer size={'s'} /> | ||
{fieldTypeFilter} | ||
<EuiSpacer size={'s'} /> | ||
{list} | ||
</> | ||
)} | ||
</EuiSelectable> | ||
); | ||
}; | ||
|
||
|
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.
nit: Since you added the
fieldRegistry && ...
catch, could this be changed to: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, committed this!