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

[Lens][Unified search] Auto expand comboboxes and popovers based on the content #171573

Merged
merged 28 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8f31868
widen all the things
mbondyra Nov 20, 2023
d813f82
moving to unfied search
mbondyra Nov 20, 2023
bb964e2
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Nov 20, 2023
d8670fb
fix type
mbondyra Nov 20, 2023
2a7a0b7
move to package
mbondyra Nov 21, 2023
f628e1f
remove package from unified search
mbondyra Nov 21, 2023
dbbe762
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Nov 21, 2023
86e4e91
discover changes
mbondyra Nov 21, 2023
642c672
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Nov 21, 2023
4c2bf9a
trigger CI
mbondyra Nov 21, 2023
aeb1af5
Merge branch 'widen_eui_selectables_steps' of github.com:mbondyra/kib…
mbondyra Nov 21, 2023
a9e440b
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Nov 21, 2023
c31ea17
Merge branch 'main' into widen_eui_selectables_steps
mbondyra Nov 22, 2023
a2fd59a
fix test
mbondyra Nov 22, 2023
29694b8
Merge branch 'widen_eui_selectables_steps' of github.com:mbondyra/kib…
mbondyra Nov 22, 2023
4177e2e
fix test
mbondyra Nov 22, 2023
1c90e9c
Merge commit 'edc1b47bbda889dbbebc612f89e90e3505d171ad' into widen_eu…
mbondyra Nov 22, 2023
9005945
removing the flex width changing behavior from unified search
mbondyra Nov 22, 2023
0ed2882
Merge branch 'main' into widen_eui_selectables_steps
mbondyra Nov 22, 2023
3902e4a
Merge commit '17ed0748e8d7671df31291d7b2bce5460c74b0f5' into widen_eu…
mbondyra Nov 23, 2023
6825bb0
Merge branch 'widen_eui_selectables_steps' of github.com:mbondyra/kib…
mbondyra Nov 23, 2023
8642e05
attach to right
mbondyra Nov 23, 2023
b880b58
add calculateWidthFromEntries
mbondyra Nov 24, 2023
feff1c7
make types smarter
mbondyra Nov 24, 2023
f6c3d64
Merge commit 'f555dbef294516517d89e9d41e14b1b2a55425dd' into widen_eu…
mbondyra Nov 24, 2023
13e98f8
Revert "make types smarter"
mbondyra Nov 24, 2023
2f58688
Merge commit 'a8dc2292c6f967b496611829d39c1bd769984577' into widen_eu…
mbondyra Nov 27, 2023
4dbc679
revert css
mbondyra Nov 28, 2023
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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ packages/kbn-bazel-runner @elastic/kibana-operations
examples/bfetch_explorer @elastic/appex-sharedux
src/plugins/bfetch @elastic/appex-sharedux
packages/kbn-calculate-auto @elastic/obs-ux-management-team
packages/kbn-calculate-width-from-char-count @elastic/kibana-visualizations
x-pack/plugins/canvas @elastic/kibana-presentation
x-pack/test/cases_api_integration/common/plugins/cases @elastic/response-ops
packages/kbn-cases-components @elastic/response-ops
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
"@kbn/bfetch-explorer-plugin": "link:examples/bfetch_explorer",
"@kbn/bfetch-plugin": "link:src/plugins/bfetch",
"@kbn/calculate-auto": "link:packages/kbn-calculate-auto",
"@kbn/calculate-width-from-char-count": "link:packages/kbn-calculate-width-from-char-count",
"@kbn/canvas-plugin": "link:x-pack/plugins/canvas",
"@kbn/cases-api-integration-test-plugin": "link:x-pack/test/cases_api_integration/common/plugins/cases",
"@kbn/cases-components": "link:packages/kbn-cases-components",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

module.exports = require('@kbn/storybook').defaultConfig;
3 changes: 3 additions & 0 deletions packages/kbn-calculate-width-from-char-count/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/calculate-width-from-char-count

This package contains a function that calculates the approximate width of the component from a text length.
9 changes: 9 additions & 0 deletions packages/kbn-calculate-width-from-char-count/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export * from './src';
13 changes: 13 additions & 0 deletions packages/kbn-calculate-width-from-char-count/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-calculate-width-from-char-count'],
};
5 changes: 5 additions & 0 deletions packages/kbn-calculate-width-from-char-count/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "shared-common",
"id": "@kbn/calculate-width-from-char-count",
"owner": "@elastic/kibana-visualizations"
}
7 changes: 7 additions & 0 deletions packages/kbn-calculate-width-from-char-count/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@kbn/calculate-width-from-char-count",
"private": true,
"version": "1.0.0",
"license": "SSPL-1.0 OR Elastic License 2.0",
"sideEffects": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { calculateWidthFromCharCount, MAX_WIDTH } from './calculate_width_from_char_count';

describe('calculateWidthFromCharCount', () => {
it('should return minimum width if char count is smaller than minWidth', () => {
expect(calculateWidthFromCharCount(10, { minWidth: 300 })).toBe(300);
});
it('should return calculated width', () => {
expect(calculateWidthFromCharCount(30)).toBe(30 * 7 + 116);
});
it('should return maximum width if char count is bigger than maxWidth', () => {
expect(calculateWidthFromCharCount(1000)).toBe(MAX_WIDTH);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export interface LIMITS {
paddingsWidth: number;
minWidth?: number;
avCharWidth: number;
maxWidth: number;
}

export const MAX_WIDTH = 550;
const PADDINGS_WIDTH = 116;
const AVERAGE_CHAR_WIDTH = 7;

const defaultPanelWidths: LIMITS = {
maxWidth: MAX_WIDTH,
avCharWidth: AVERAGE_CHAR_WIDTH,
paddingsWidth: PADDINGS_WIDTH,
};

export function calculateWidthFromCharCount(
labelLength: number,
overridesPanelWidths?: Partial<LIMITS>
) {
const { maxWidth, avCharWidth, paddingsWidth, minWidth } = {
...defaultPanelWidths,
...overridesPanelWidths,
};
const widthForCharCount = paddingsWidth + labelLength * avCharWidth;

if (minWidth && widthForCharCount < minWidth) {
return minWidth;
}

return Math.min(widthForCharCount, maxWidth);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { calculateWidthFromEntries } from './calculate_width_from_entries';
import { MAX_WIDTH } from './calculate_width_from_char_count';
import faker from 'faker';

const generateLabel = (length: number) => faker.random.alpha({ count: length });

const generateObjectWithLabelOfLength = (length: number, propOverrides?: Record<string, any>) => ({
label: generateLabel(length),
...propOverrides,
});

describe('calculateWidthFromEntries', () => {
it('calculates width for array of strings', () => {
const shortLabels = [10, 20].map(generateLabel);
expect(calculateWidthFromEntries(shortLabels)).toBe(256);

const mediumLabels = [50, 55, 10, 20].map(generateLabel);
expect(calculateWidthFromEntries(mediumLabels)).toBe(501);

const longLabels = [80, 90, 10].map(generateLabel);
expect(calculateWidthFromEntries(longLabels)).toBe(MAX_WIDTH);
});

it('calculates width for array of objects with keys', () => {
const shortLabels = [10, 20].map((v) => generateObjectWithLabelOfLength(v));
expect(calculateWidthFromEntries(shortLabels, ['label'])).toBe(256);

const mediumLabels = [50, 55, 10, 20].map((v) => generateObjectWithLabelOfLength(v));
expect(calculateWidthFromEntries(mediumLabels, ['label'])).toBe(501);

const longLabels = [80, 90, 10].map((v) => generateObjectWithLabelOfLength(v));
expect(calculateWidthFromEntries(longLabels, ['label'])).toBe(MAX_WIDTH);
});
it('calculates width for array of objects for fallback keys', () => {
const shortLabels = [10, 20].map((v) =>
generateObjectWithLabelOfLength(v, { label: undefined, name: generateLabel(v) })
);
expect(calculateWidthFromEntries(shortLabels, ['id', 'label', 'name'])).toBe(256);

const mediumLabels = [50, 55, 10, 20].map((v) =>
generateObjectWithLabelOfLength(v, { label: undefined, name: generateLabel(v) })
);
expect(calculateWidthFromEntries(mediumLabels, ['id', 'label', 'name'])).toBe(501);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { LIMITS, calculateWidthFromCharCount } from './calculate_width_from_char_count';

type GenericObject<T = Record<string, any>> = T;

const getMaxLabelLengthForObjects = (
entries: GenericObject[],
labelKeys: Array<keyof GenericObject>
) =>
entries.reduce((acc, curr) => {
const labelKey = labelKeys.find((key) => curr[key]);
if (!labelKey) {
return acc;
}
const labelLength = curr[labelKey].length;
return acc > labelLength ? acc : labelLength;
}, 0);

const getMaxLabelLengthForStrings = (arr: string[]) =>
arr.reduce((acc, curr) => (acc > curr.length ? acc : curr.length), 0);

export function calculateWidthFromEntries(
entries: GenericObject[] | string[],
labelKeys?: Array<keyof GenericObject>,
overridesPanelWidths?: Partial<LIMITS>
) {
const maxLabelLength = labelKeys
? getMaxLabelLengthForObjects(entries as GenericObject[], labelKeys)
: getMaxLabelLengthForStrings(entries as string[]);

return calculateWidthFromCharCount(maxLabelLength, overridesPanelWidths);
}
11 changes: 11 additions & 0 deletions packages/kbn-calculate-width-from-char-count/src/index.ts
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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { calculateWidthFromCharCount } from './calculate_width_from_char_count';

export { calculateWidthFromEntries } from './calculate_width_from_entries';
19 changes: 19 additions & 0 deletions packages/kbn-calculate-width-from-char-count/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node",
"react",
],
},
"include": [
"**/*.ts",
"**/*.tsx",
],
"kbn_references": [],
"exclude": [
"target/**/*",
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { FieldPicker, FieldPickerProps } from './field_picker';
import { render, screen } from '@testing-library/react';
import faker from 'faker';
import userEvent from '@testing-library/user-event';
import { DataType, FieldOptionValue } from './types';

const generateFieldWithLabelOfLength = (length: number) => ({
label: faker.random.alpha({ count: length }),
value: {
type: 'field' as const,
field: faker.random.alpha({ count: length }),
dataType: 'date' as DataType,
operationType: 'count',
},
exists: true,
compatible: 1,
});

const generateProps = (customField = generateFieldWithLabelOfLength(20)) =>
({
selectedOptions: [
{
label: 'Category',
value: {
type: 'field' as const,
field: 'category.keyword',
dataType: 'keyword' as DataType,
operationType: 'count',
},
},
],
options: [
{
label: 'nested options',
exists: true,
compatible: 1,
value: generateFieldWithLabelOfLength(20),
options: [
generateFieldWithLabelOfLength(20),
customField,
generateFieldWithLabelOfLength(20),
],
},
],
onChoose: jest.fn(),
fieldIsInvalid: false,
} as unknown as FieldPickerProps<FieldOptionValue>);

describe('field picker', () => {
const renderFieldPicker = (customField = generateFieldWithLabelOfLength(20)) => {
const props = generateProps(customField);
const rtlRender = render(<FieldPicker {...props} />);
return {
openCombobox: () => userEvent.click(screen.getByLabelText(/open list of options/i)),
...rtlRender,
};
};

it('should render minimum width dropdown list if all labels are short', async () => {
const { openCombobox } = renderFieldPicker();
openCombobox();
const popover = screen.getByRole('dialog');
expect(popover).toHaveStyle('inline-size: 256px');
});

it('should render calculated width dropdown list if the longest label is longer than min width', async () => {
const { openCombobox } = renderFieldPicker(generateFieldWithLabelOfLength(50));
openCombobox();

const popover = screen.getByRole('dialog');
expect(popover).toHaveStyle('inline-size: 466px');
});

it('should render maximum width dropdown list if the longest label is longer than max width', async () => {
const { openCombobox } = renderFieldPicker(generateFieldWithLabelOfLength(80));
openCombobox();
const popover = screen.getByRole('dialog');
expect(popover).toHaveStyle('inline-size: 550px');
});
});
Loading