@elastic/eui
Version:
Elastic UI Component Library
77 lines (76 loc) • 3.19 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "className", "scope", "style", "width", "minWidth", "maxWidth", "append"];
/*
* 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 React, { useEffect, useRef } from 'react';
import classNames from 'classnames';
import { useEuiMemoizedStyles } from '../../services';
import { resolveWidthPropsAsStyle } from './utils';
import { euiTableCellCheckboxStyles } from './table_cells_shared.styles';
import { useEuiTableColumnDataStore } from './store/provider';
import { useEuiTableWithinStickyHeader } from './sticky_header';
import { useEuiTableStoreUniqueColumnId } from './store/use_unique_column_id';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var EuiTableHeaderCellCheckbox = function EuiTableHeaderCellCheckbox(props) {
var children = props.children,
className = props.className,
_props$scope = props.scope,
scope = _props$scope === void 0 ? 'col' : _props$scope,
_style = props.style,
width = props.width,
minWidth = props.minWidth,
maxWidth = props.maxWidth,
append = props.append,
rest = _objectWithoutProperties(props, _excluded);
var storeCellId = useEuiTableStoreUniqueColumnId();
var store = useEuiTableColumnDataStore();
var isWithinStickyHeader = useEuiTableWithinStickyHeader();
var styles = useEuiMemoizedStyles(euiTableCellCheckboxStyles);
var renderHeaderCellRef = useRef();
renderHeaderCellRef.current = function (extraProps) {
var classes = classNames('euiTableHeaderCellCheckbox', className);
var style = resolveWidthPropsAsStyle(_style, {
width: width,
minWidth: minWidth,
maxWidth: maxWidth
});
return ___EmotionJSX("th", _extends({
css: styles.euiTableHeaderCellCheckbox,
className: classes,
scope: scope,
style: style
}, rest, extraProps), ___EmotionJSX("div", {
className: "euiTableCellContent"
}, children), append);
};
useEffect(function () {
// Don't register the column inside the sticky header as the original
// column is already registered. This would cause an infinite loop.
if (isWithinStickyHeader) {
return;
}
var unregisterColumn = store.registerColumn(storeCellId, {
renderHeaderCellRef: renderHeaderCellRef
});
return function () {
unregisterColumn();
};
}, [store, isWithinStickyHeader, storeCellId]);
useEffect(function () {
// Notify the store on every render so the sticky header stays in sync.
// React's reconciliation will efficiently handle any duplicate renders.
if (isWithinStickyHeader) {
return;
}
store.updateColumn(storeCellId, {
renderHeaderCellRef: renderHeaderCellRef
});
});
return renderHeaderCellRef.current({});
};