-
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.
Merge branch 'main' into move-structure-list-apis
- Loading branch information
Showing
229 changed files
with
2,827 additions
and
1,136 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
Validating CODEOWNERS rules …
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
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
9 changes: 9 additions & 0 deletions
9
packages/kbn-calculate-width-from-char-count/.storybook/main.js
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,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; |
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,3 @@ | ||
# @kbn/calculate-width-from-char-count | ||
|
||
This package contains a function that calculates the approximate width of the component from a text length. |
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,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
13
packages/kbn-calculate-width-from-char-count/jest.config.js
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,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'], | ||
}; |
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,5 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/calculate-width-from-char-count", | ||
"owner": "@elastic/kibana-visualizations" | ||
} |
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 @@ | ||
{ | ||
"name": "@kbn/calculate-width-from-char-count", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "SSPL-1.0 OR Elastic License 2.0", | ||
"sideEffects": false | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/kbn-calculate-width-from-char-count/src/calculate_width_from_char_count.test.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,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); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/kbn-calculate-width-from-char-count/src/calculate_width_from_char_count.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,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); | ||
} |
53 changes: 53 additions & 0 deletions
53
packages/kbn-calculate-width-from-char-count/src/calculate_width_from_entries.test.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,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); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
packages/kbn-calculate-width-from-char-count/src/calculate_width_from_entries.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,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); | ||
} |
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 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
19
packages/kbn-calculate-width-from-char-count/tsconfig.json
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,19 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types", | ||
"types": [ | ||
"jest", | ||
"node", | ||
"react", | ||
], | ||
}, | ||
"include": [ | ||
"**/*.ts", | ||
"**/*.tsx", | ||
], | ||
"kbn_references": [], | ||
"exclude": [ | ||
"target/**/*", | ||
] | ||
} |
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
Oops, something went wrong.