Skip to content
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

Use the displayName property in default editor #73311

Merged
merged 4 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
Expand Down Expand Up @@ -169,6 +170,9 @@ describe('DefaultEditorAggParams helpers', () => {
],
advanced: [],
});

// Should be grouped using displayName as label
expect(groupAndSortBy).toHaveBeenCalledWith(expect.anything(), 'type', 'displayName', 'name');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) => {
Expand Down
15 changes: 12 additions & 3 deletions src/plugins/vis_default_editor/public/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
*/

interface ComboBoxOption<T> {
key?: string;
label: string;
target: T;
}
interface ComboBoxGroupedOption<T> {
key?: string;
label: string;
options: Array<ComboBoxOption<T>>;
}
Expand All @@ -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) {
Expand Down