@blueprintjs/table
Version:
Scalable interactive table component
96 lines • 5.7 kB
JavaScript
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
/*
* Copyright 2022 Palantir Technologies, Inc. All rights reserved.
* * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import classNames from "classnames";
import { AbstractPureComponent, Utils as CoreUtils, DISPLAYNAME_PREFIX, Icon, Popover, } from "@blueprintjs/core";
import * as Classes from "../common/classes";
import { LoadableContent } from "../common/loadableContent";
import { CLASSNAME_EXCLUDED_FROM_TEXT_MEASUREMENT } from "../common/utils";
import { HeaderCell } from "./headerCell";
import { HorizontalCellDivider } from "./horizontalCellDivider";
/**
* Column header cell component.
*
* @see https://blueprintjs.com/docs/#table/api.columnheadercell
*/
export class ColumnHeaderCell extends AbstractPureComponent {
constructor() {
super(...arguments);
this.state = {
isActive: false,
};
this.handlePopoverOpened = () => this.setState({ isActive: true });
this.handlePopoverClosing = () => this.setState({ isActive: false });
}
/**
* This method determines if a `MouseEvent` was triggered on a target that
* should be used as the header click/drag target. This enables users of
* this component to render fully interactive components in their header
* cells without worry of selection or resize operations from capturing
* their mouse events.
*/
static isHeaderMouseTarget(target) {
return (target.classList.contains(Classes.TABLE_HEADER) ||
target.classList.contains(Classes.TABLE_COLUMN_NAME) ||
target.classList.contains(Classes.TABLE_INTERACTION_BAR) ||
target.classList.contains(Classes.TABLE_HEADER_CONTENT));
}
render() {
const { enableColumnInteractionBar, enableColumnReordering, isColumnSelected, menuIcon, name, nameRenderer, ...spreadableProps } = this.props;
const classes = classNames(spreadableProps.className, Classes.TABLE_COLUMN_HEADER_CELL, {
[Classes.TABLE_HAS_INTERACTION_BAR]: enableColumnInteractionBar,
[Classes.TABLE_HAS_REORDER_HANDLE]: this.props.reorderHandle != null,
});
return (_jsxs(HeaderCell, { isReorderable: enableColumnReordering, isSelected: isColumnSelected, ...spreadableProps, className: classes, children: [this.renderName(), this.maybeRenderContent(), this.props.loading ? undefined : this.props.resizeHandle] }));
}
renderName() {
var _a;
const { enableColumnInteractionBar, index, loading, name, nameRenderer, reorderHandle } = this.props;
const dropdownMenu = this.maybeRenderDropdownMenu();
const defaultName = _jsx("div", { className: Classes.TABLE_TRUNCATED_TEXT, children: name });
const nameComponent = (_jsx(LoadableContent, { loading: loading !== null && loading !== void 0 ? loading : false, variableLength: true, children: (_a = nameRenderer === null || nameRenderer === void 0 ? void 0 : nameRenderer(name, index)) !== null && _a !== void 0 ? _a : defaultName }));
if (enableColumnInteractionBar) {
return (_jsxs("div", { className: Classes.TABLE_COLUMN_NAME, title: name, children: [_jsxs("div", { className: Classes.TABLE_INTERACTION_BAR, children: [reorderHandle, dropdownMenu] }), _jsx(HorizontalCellDivider, {}), _jsx("div", { className: Classes.TABLE_COLUMN_NAME_TEXT, children: nameComponent })] }));
}
else {
return (_jsxs("div", { className: Classes.TABLE_COLUMN_NAME, title: name, children: [reorderHandle, dropdownMenu, _jsx("div", { className: Classes.TABLE_COLUMN_NAME_TEXT, children: nameComponent })] }));
}
}
maybeRenderContent() {
if (this.props.children === null) {
return undefined;
}
return _jsx("div", { className: Classes.TABLE_HEADER_CONTENT, children: this.props.children });
}
maybeRenderDropdownMenu() {
const { index, menuIcon, menuPopoverProps, menuRenderer, selectCellsOnMenuClick } = this.props;
if (!CoreUtils.isFunction(menuRenderer)) {
return undefined;
}
const classes = classNames(Classes.TABLE_TH_MENU_CONTAINER, CLASSNAME_EXCLUDED_FROM_TEXT_MEASUREMENT, {
[Classes.TABLE_TH_MENU_OPEN]: this.state.isActive,
[Classes.TABLE_TH_MENU_SELECT_CELLS]: selectCellsOnMenuClick,
});
return (_jsxs("div", { className: classes, children: [_jsx("div", { className: Classes.TABLE_TH_MENU_CONTAINER_BACKGROUND }), _jsx(Popover, { className: classNames(Classes.TABLE_TH_MENU, menuPopoverProps === null || menuPopoverProps === void 0 ? void 0 : menuPopoverProps.className), content: menuRenderer(index), onClosing: this.handlePopoverClosing, onOpened: this.handlePopoverOpened, placement: "bottom", rootBoundary: "document", ...menuPopoverProps, children: _jsx(Icon, { icon: menuIcon }) })] }));
}
}
ColumnHeaderCell.displayName = `${DISPLAYNAME_PREFIX}.ColumnHeaderCell`;
ColumnHeaderCell.defaultProps = {
enableColumnInteractionBar: false,
isActive: false,
menuIcon: "chevron-down",
selectCellsOnMenuClick: true,
};
//# sourceMappingURL=columnHeaderCell.js.map