@grafana/ui
Version:
Grafana Components Library
68 lines (61 loc) • 2.3 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var calculateSize = require('calculate-size');
var completion = require('../types/completion.cjs');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
var calculateSize__default = /*#__PURE__*/_interopDefaultCompat(calculateSize);
;
const flattenGroupItems = (groupedItems) => {
return groupedItems.reduce((all, { items, label }) => {
all.push({
label,
kind: completion.CompletionItemKind.GroupTitle
});
return items.reduce((all2, item) => {
all2.push(item);
return all2;
}, all);
}, []);
};
const calculateLongestLabel = (allItems) => {
return allItems.reduce((longest, current) => {
return longest.length < current.label.length ? current.label : longest;
}, "");
};
const calculateListSizes = (theme, allItems, longestLabel) => {
const size = calculateSize__default.default(longestLabel, {
font: theme.typography.fontFamilyMonospace,
fontSize: theme.typography.bodySmall.fontSize,
fontWeight: "normal"
});
const listWidth = calculateListWidth(size.width, theme);
const itemHeight = calculateItemHeight(size.height, theme);
const listHeight = calculateListHeight(itemHeight, allItems);
return {
listWidth,
listHeight,
itemHeight
};
};
const calculateItemHeight = (longestLabelHeight, theme) => {
const horizontalPadding = theme.spacing.gridSize * 2;
const itemHeight = longestLabelHeight + horizontalPadding;
return itemHeight;
};
const calculateListWidth = (longestLabelWidth, theme) => {
const verticalPadding = theme.spacing.gridSize * 3;
const maxWidth = 800;
const listWidth = Math.min(Math.max(longestLabelWidth + verticalPadding, 200), maxWidth);
return listWidth;
};
const calculateListHeight = (itemHeight, allItems) => {
const numberOfItemsToShow = Math.min(allItems.length, 10);
const minHeight = 100;
const totalHeight = numberOfItemsToShow * itemHeight;
const listHeight = Math.max(totalHeight, minHeight);
return listHeight;
};
exports.calculateListSizes = calculateListSizes;
exports.calculateLongestLabel = calculateLongestLabel;
exports.flattenGroupItems = flattenGroupItems;
//# sourceMappingURL=typeahead.cjs.map