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

[table] fix: column resize calculation on header double click #3732

Merged
merged 2 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 16 additions & 16 deletions packages/table/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
* limitations under the License.
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { Icon } from "@blueprintjs/core";

const CLASSNAME_EXCLUDED_FROM_TEXT_MEASUREMENT = "bp-table-text-no-measure";
// used to exclude icons from column header measure
export const CLASSNAME_EXCLUDED_FROM_TEXT_MEASUREMENT = "bp-table-text-no-measure";
// supposed width of the icons placeholder
const EXCLUDED_ICON_PLACEHOLDER_WIDTH = 16;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const EXCLUDED_ICON_PLACEHOLDER_WIDTH = 16;
const EXCLUDED_ICON_PLACEHOLDER_WIDTH = Icon.SIZE_STANDARD;


/**
* Since Firefox doesn't provide a computed "font" property, we manually
Expand Down Expand Up @@ -350,23 +353,20 @@ export const Utils = {
* exclude an element's text from the computation.
*/
function measureTextContentWithExclusions(context: CanvasRenderingContext2D, element: Element): TextMetrics {
// We only expect one or zero excluded elements in this subtree
// We don't have a need for more than one, so we avoid that complexity altogether.
const elementToExclude = element.querySelector(`.${CLASSNAME_EXCLUDED_FROM_TEXT_MEASUREMENT}`);
let removedElementParent: Element | undefined;
let removedElementNextSibling: Node | undefined;

if (elementToExclude != null) {
removedElementParent = elementToExclude.parentElement;
removedElementNextSibling = elementToExclude.nextSibling;
removedElementParent.removeChild(elementToExclude);
const elementsToExclude = element.querySelectorAll(`.${CLASSNAME_EXCLUDED_FROM_TEXT_MEASUREMENT}`);
let excludedElementsWidth = 0;
if (elementsToExclude && elementsToExclude.length) {
elementsToExclude.forEach((e) => {
const excludedMetrics = context.measureText(e.textContent);
excludedElementsWidth += excludedMetrics.width - EXCLUDED_ICON_PLACEHOLDER_WIDTH;
});
}

const metrics = context.measureText(element.textContent);
const metricsWithExclusions = {
...metrics,
width: metrics.width - excludedElementsWidth,
};

if (elementToExclude != null) {
removedElementParent.insertBefore(elementToExclude, removedElementNextSibling);
}

return metrics;
return metricsWithExclusions;
}
3 changes: 2 additions & 1 deletion packages/table/src/headers/columnHeaderCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import * as Classes from "../common/classes";
import { columnInteractionBarContextTypes, IColumnInteractionBarContextTypes } from "../common/context";
import { LoadableContent } from "../common/loadableContent";
import { CLASSNAME_EXCLUDED_FROM_TEXT_MEASUREMENT } from "../common/utils";
import { HeaderCell, IHeaderCellProps } from "./headerCell";

export interface IColumnNameProps {
Expand Down Expand Up @@ -194,7 +195,7 @@ export class ColumnHeaderCell extends AbstractPureComponent<IColumnHeaderCellPro
return undefined;
}

const classes = classNames(Classes.TABLE_TH_MENU_CONTAINER, {
const classes = classNames(Classes.TABLE_TH_MENU_CONTAINER, CLASSNAME_EXCLUDED_FROM_TEXT_MEASUREMENT, {
[Classes.TABLE_TH_MENU_OPEN]: this.state.isActive,
});

Expand Down
5 changes: 4 additions & 1 deletion packages/table/src/headers/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as React from "react";
import { Grid } from "../common";
import { IFocusedCellCoordinates } from "../common/cell";
import * as Classes from "../common/classes";
import { CLASSNAME_EXCLUDED_FROM_TEXT_MEASUREMENT } from "../common/utils";
import { DragEvents } from "../interactions/dragEvents";
import { IClientCoordinates, ICoordinateData } from "../interactions/draggable";
import { DragReorderable, IReorderableProps } from "../interactions/reorderable";
Expand Down Expand Up @@ -378,7 +379,9 @@ export class Header extends React.Component<IInternalHeaderProps, IHeaderState>
: this.wrapInDragReorderable(
index,
<div className={Classes.TABLE_REORDER_HANDLE_TARGET}>
<div className={Classes.TABLE_REORDER_HANDLE}>
<div
className={classNames(Classes.TABLE_REORDER_HANDLE, CLASSNAME_EXCLUDED_FROM_TEXT_MEASUREMENT)}
>
<Icon icon="drag-handle-vertical" />
</div>
</div>,
Expand Down