@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
53 lines (52 loc) • 1.87 kB
JavaScript
const BUTTON_HORIZONTAL_PADDING = 28;
const ICON_WIDTH = 20;
const ICON_LABEL_GAP = 4;
const BUTTON_GAP = 8;
const CHAR_WIDTH = 7.5;
const DROPDOWN_CHEVRON_WIDTH = 24;
const HEADER_EXTRA_PADDING = 32;
function estimateButtonWidth(label, hasIcon) {
let width = BUTTON_HORIZONTAL_PADDING;
if (label) {
width += label.length * CHAR_WIDTH;
}
if (hasIcon) {
width += ICON_WIDTH + (label ? ICON_LABEL_GAP : 0);
}
return Math.ceil(width);
}
function getStaticButtonLabel(button) {
if (!button.label || typeof button.label === 'function') {
return '';
}
return button.label;
}
function hasStaticIcon(button) {
return Boolean(button.icon && typeof button.icon !== 'function');
}
export function calculateActionColumnWidth(actionColumn, buttons) {
const settings = actionColumn.actionColumnSettings ?? {};
if (settings.width != null) {
return settings.width;
}
const minWidth = settings.minWidth ?? 80;
const headerName = actionColumn.friendlyName ?? actionColumn.columnId;
const headerWidth = HEADER_EXTRA_PADDING + headerName.length * CHAR_WIDTH;
if (!settings.autoWidth) {
return settings.minWidth;
}
const visibleButtons = buttons.filter((button) => !button.hidden);
if (settings.displayMode === 'dropdown') {
const dropdownLabel = settings.dropdownLabel ?? 'Actions';
const triggerWidth = estimateButtonWidth(dropdownLabel, false) + DROPDOWN_CHEVRON_WIDTH;
return Math.max(minWidth, triggerWidth, headerWidth);
}
let buttonsWidth = 0;
visibleButtons.forEach((button, index) => {
buttonsWidth += estimateButtonWidth(getStaticButtonLabel(button), hasStaticIcon(button));
if (index > 0) {
buttonsWidth += BUTTON_GAP;
}
});
return Math.max(minWidth, buttonsWidth, headerWidth);
}