mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
* Use displayName in field list in visualize editor * Set key in combo box * Fix jest tests Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
parent
61ac72e5ee
commit
080b62aada
4 changed files with 18 additions and 5 deletions
|
@ -33,6 +33,7 @@ import { FieldParamEditor, OrderByParamEditor } from './controls';
|
|||
import { EditorConfig } from './utils';
|
||||
import { Schema } from '../schemas';
|
||||
import { EditorVisState } from './sidebar/state/reducers';
|
||||
import { groupAndSortBy } from '../utils';
|
||||
|
||||
jest.mock('../utils', () => ({
|
||||
groupAndSortBy: jest.fn(() => ['indexedFields']),
|
||||
|
@ -169,6 +170,9 @@ describe('DefaultEditorAggParams helpers', () => {
|
|||
],
|
||||
advanced: [],
|
||||
});
|
||||
|
||||
// Should be grouped using displayName as label
|
||||
expect(groupAndSortBy).toHaveBeenCalledWith(expect.anything(), 'type', 'displayName', 'name');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -93,7 +93,7 @@ function getAggParamsToRender({
|
|||
}
|
||||
}
|
||||
fields = filterAggTypeFields(availableFields, agg);
|
||||
indexedFields = groupAndSortBy(fields, 'type', 'name');
|
||||
indexedFields = groupAndSortBy(fields, 'type', 'displayName', 'name');
|
||||
|
||||
if (fields && !indexedFields.length && index > 0) {
|
||||
// don't draw the rest of the options if there are no indexed fields and it's an extra param (index > 0).
|
||||
|
|
|
@ -52,7 +52,7 @@ function FieldParamEditor({
|
|||
}: FieldParamEditorProps) {
|
||||
const [isDirty, setIsDirty] = useState(false);
|
||||
const selectedOptions: ComboBoxGroupedOptions<IndexPatternField> = value
|
||||
? [{ label: value.displayName || value.name, target: value }]
|
||||
? [{ label: value.displayName, target: value, key: value.name }]
|
||||
: [];
|
||||
|
||||
const onChange = (options: EuiComboBoxOptionOption[]) => {
|
||||
|
|
|
@ -18,10 +18,12 @@
|
|||
*/
|
||||
|
||||
interface ComboBoxOption<T> {
|
||||
key?: string;
|
||||
label: string;
|
||||
target: T;
|
||||
}
|
||||
interface ComboBoxGroupedOption<T> {
|
||||
key?: string;
|
||||
label: string;
|
||||
options: Array<ComboBoxOption<T>>;
|
||||
}
|
||||
|
@ -40,15 +42,22 @@ export type ComboBoxGroupedOptions<T> = Array<GroupOrOption<T>>;
|
|||
* @returns An array of grouped and sorted alphabetically `objects` that are compatible with EuiComboBox options.
|
||||
*/
|
||||
export function groupAndSortBy<
|
||||
T extends Record<TGroupBy | TLabelName, string>,
|
||||
T extends Record<TGroupBy | TLabelName | TKeyName, string>,
|
||||
TGroupBy extends string = 'type',
|
||||
TLabelName extends string = 'title'
|
||||
>(objects: T[], groupBy: TGroupBy, labelName: TLabelName): ComboBoxGroupedOptions<T> {
|
||||
TLabelName extends string = 'title',
|
||||
TKeyName extends string = never
|
||||
>(
|
||||
objects: T[],
|
||||
groupBy: TGroupBy,
|
||||
labelName: TLabelName,
|
||||
keyName?: TKeyName
|
||||
): ComboBoxGroupedOptions<T> {
|
||||
const groupedOptions = objects.reduce((array, obj) => {
|
||||
const group = array.find((element) => element.label === obj[groupBy]);
|
||||
const option = {
|
||||
label: obj[labelName],
|
||||
target: obj,
|
||||
...(keyName ? { key: obj[keyName] } : {}),
|
||||
};
|
||||
|
||||
if (group && group.options) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue