@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
100 lines (99 loc) • 3.7 kB
JavaScript
import * as React from 'react';
import { renderWithAdaptableContext } from '../View/renderWithAdaptableContext';
import { AdaptableFloatingFilter } from '../View/Components/ColumnFilter/AdaptableFloatingFilter';
const filterContainerStyle = {
overflow: 'hidden',
minWidth: '0',
height: '100%',
display: 'flex',
alignItems: 'stretch',
position: 'relative',
flex: '1 1 auto',
};
export const AgGridFloatingFilterAdapterFactory = (adaptable) => {
const adaptableApi = adaptable.api;
function getContainerId(colId) {
return `floatingFilter_${colId}_${adaptable.adaptableOptions.adaptableId}`;
}
function getFilterProps(colId) {
const column = adaptableApi.columnApi.getColumnWithColumnId(colId);
const columnFilterProps = {
Column: column,
Adaptable: adaptable,
ShowCloseButton: false,
};
return columnFilterProps;
}
function findParentWithClass(element, className, stopClasses, applyToAll) {
let current = element.parentElement;
while (current) {
if (stopClasses.some((stopClass) => current.classList.contains(stopClass))) {
return null;
}
if (current.classList.contains(className)) {
return current;
}
applyToAll?.(current);
current = current.parentElement;
}
return null;
}
function patchParentElement(filterContainer) {
if (!filterContainer) {
return;
}
const stopClasses = ['ag-header-row', 'ag-header-row-column-filter'];
const filterBody = findParentWithClass(filterContainer, 'ag-floating-filter-full-body', stopClasses, (currentElem) => {
currentElem.style.height = '100%';
});
if (filterBody) {
filterBody.style.overflow = 'visible';
}
const headerCell = findParentWithClass(filterContainer, 'ag-header-cell', stopClasses);
if (headerCell?.classList.contains('ag-floating-filter')) {
headerCell.style.padding = 'var(--ab-base-space)';
}
}
return class AgGridFloatingFilterAdapter {
colId;
filterContainer;
unmountReactRoot;
init(params) {
const colId = params.column.getColId();
this.colId = colId;
this.filterContainer = document.createElement('div');
this.filterContainer.id = getContainerId(colId);
Object.keys(filterContainerStyle).forEach((key) => {
this.filterContainer.style[key] = filterContainerStyle[key];
});
const column = adaptableApi.columnApi.getColumnWithColumnId(colId);
if (column) {
const filterProps = getFilterProps(colId);
this.unmountReactRoot = adaptable.renderReactRoot(renderWithAdaptableContext(React.createElement(AdaptableFloatingFilter, {
...filterProps,
}), adaptable), this.filterContainer);
}
}
onParentModelChanged(parentModel, filterChangedEvent) {
}
afterGuiAttached() {
patchParentElement(this.filterContainer);
}
getGui() {
return this.filterContainer;
}
refresh(params) {
return true;
}
destroy() {
if (this.unmountReactRoot) {
const unmount = this.unmountReactRoot;
setTimeout(() => {
unmount();
}, 0);
this.unmountReactRoot = undefined;
}
this.filterContainer = null;
}
};
};