Skip to content

Commit

Permalink
improve exports and release version patch
Browse files Browse the repository at this point in the history
  • Loading branch information
radubrehar committed Oct 17, 2024
1 parent 03a0018 commit e76dd95
Show file tree
Hide file tree
Showing 13 changed files with 432 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import * as React from 'react';

import {
InfiniteTable,
InfiniteTablePropColumns,
DataSource,
useInfiniteHeaderCell,
useInfinitePortalContainer,
alignNode,
InfiniteTableColumn,
} from '@infinite-table/infinite-react';
import { createPortal } from 'react-dom';
import { FilterIcon } from 'lucide-react';

type Developer = {
id: number;

firstName: string;
lastName: string;

currency: string;
preferredLanguage: string;
stack: string;
canDesign: 'yes' | 'no';

age: number;
};

const data: Developer[] = [
{
id: 1,
firstName: 'John',
lastName: 'Bob',
age: 20,
canDesign: 'yes',
currency: 'USD',
preferredLanguage: 'JavaScript',
stack: 'frontend',
},
{
id: 2,
firstName: 'Marry',
lastName: 'Bob',
age: 25,
canDesign: 'yes',
currency: 'USD',
preferredLanguage: 'JavaScript',
stack: 'frontend',
},
{
id: 3,
firstName: 'Bill',
lastName: 'Bobson',
age: 30,
canDesign: 'no',
currency: 'CAD',
preferredLanguage: 'TypeScript',
stack: 'frontend',
},
{
id: 4,
firstName: 'Mark',
lastName: 'Twain',
age: 31,
canDesign: 'yes',
currency: 'CAD',
preferredLanguage: 'Rust',
stack: 'backend',
},
{
id: 5,
firstName: 'Matthew',
lastName: 'Hilson',
age: 29,
canDesign: 'yes',
currency: 'CAD',
preferredLanguage: 'Go',
stack: 'backend',
},
];

function ColumnFilterMenuIcon() {
const { renderBag, htmlElementRef: alignToRef } = useInfiniteHeaderCell();

const portalContainer = useInfinitePortalContainer();

const [visible, setVisible] = React.useState(false);

React.useEffect(() => {
if (!domRef.current || !alignToRef.current) {
return;
}

alignNode(domRef.current, {
alignTo: alignToRef.current,
alignPosition: [
['TopRight', 'BottomRight'],
['TopRight', 'BottomRight'],
],
});
});

const domRef = React.useRef<HTMLDivElement>(null);

return (
<div
onMouseDown={(e) => {
if (e.nativeEvent) {
// @ts-ignore
e.nativeEvent.__insideMenu = true;
}
}}
onPointerDown={(event) => {
event.stopPropagation();

if (visible) {
setVisible(false);
return;
}

setVisible(true);

function handleMouseDown(event: MouseEvent) {
// @ts-ignore
if (event.__insideMenu !== true) {
setVisible(false);
document.documentElement.removeEventListener(
'mousedown',
handleMouseDown,
);
}
}
document.documentElement.addEventListener('mousedown', handleMouseDown);
}}
>
<FilterIcon size={16} />
{createPortal(
<div
ref={domRef}
style={{
position: 'absolute',
color: 'white',
top: 0,
overflow: 'visible',
background: 'var(--infinite-background)',
border: `1px solid currentColor`,
left: 0,
width: 200,
padding: 10,
display: visible ? 'block' : 'none',
}}
>
{renderBag.filterEditor}
</div>,
portalContainer!,
)}
</div>
);
}

const customHeader: InfiniteTableColumn<any>['renderHeader'] = ({
renderBag,
}) => {
return (
<>
{renderBag.header}
<div style={{ flex: 1 }}></div>

{renderBag.filterIcon}

{renderBag.menuIcon}
<ColumnFilterMenuIcon />
</>
);
};

const columns: InfiniteTablePropColumns<Developer> = {
firstName: {
field: 'firstName',
renderHeader: customHeader,
defaultFilterable: false,
},
age: {
field: 'age',
type: 'number',

renderHeader: customHeader,
},

stack: { field: 'stack', renderMenuIcon: false },
currency: { field: 'currency' },
};

export default () => {
return (
<>
<React.StrictMode>
<DataSource<Developer>
data={data}
primaryKey="id"
defaultFilterValue={[]}
>
<InfiniteTable<Developer>
domProps={{
style: {
margin: '5px',

height: '80vh',
width: '80vw',
border: '1px solid gray',
position: 'relative',
},
}}
showColumnFilters={true}
columnDefaultWidth={300}
columnMinWidth={50}
columns={columns}
/>
</DataSource>
</React.StrictMode>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { join } from '../../../../utils/join';

import { useInfiniteTable } from '../../hooks/useInfiniteTable';
import { useInfiniteTableState } from '../../hooks/useInfiniteTableState';
import { height } from '../../utilities.css';
import { FilterIcon } from '../icons/FilterIcon';

import { getColumnLabel } from './getColumnLabel';
Expand Down Expand Up @@ -109,8 +110,7 @@ export function InfiniteTableColumnHeaderFilterEmpty() {
onPointerDown={stopPropagation}
className={`${InfiniteTableColumnHeaderFilterClassName} ${HeaderFilterRecipe(
{ active: false },
)}`}
style={{ height: '100%' }}
)} ${height['50%']}`}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,13 @@ export function InfiniteTableHeaderCell<T>(
column.components?.MenuIcon || components?.MenuIcon || MenuIcon;
const menuIcon = <MenuIconCmp {...menuIconProps} />;

const domRef = useRef<HTMLElement | null>(null);

const initialRenderParam: InfiniteTableColumnHeaderParam<T> = {
horizontalLayoutPageIndex,
dragging,
domRef: ref,
htmlElementRef: domRef,
insideColumnMenu: false,
column,
columnsMap,
Expand Down Expand Up @@ -466,8 +469,6 @@ export function InfiniteTableHeaderCell<T>(
return all;
};

const domRef = useRef<HTMLElement | null>(null);

useEffect(() => {
let clearOnResize: null | (() => void) = null;
const node = domRef.current;
Expand Down Expand Up @@ -593,6 +594,27 @@ export function InfiniteTableHeaderCell<T>(
'data-sort-index': `${column.computedSortIndex ?? -1}`,
};

const columnFilterEditor = column.computedFilterable ? (
<InfiniteTableColumnHeaderFilter
horizontalLayoutPageIndex={horizontalLayoutPageIndex}
filterEditor={FilterEditor}
filterOperatorSwitch={
FilterOperatorSwitch ?? InfiniteTableFilterOperatorSwitch
}
operator={operator}
filterTypes={filterTypes}
onChange={debouncedOnFilterValueChange}
columnFilterType={filterTypeKey}
columnLabel={column.field || column.name || column.id}
columnFilterValue={column.computedFilterValue}
columnHeaderHeight={columnHeaderHeight}
/>
) : (
<InfiniteTableColumnHeaderFilterEmpty />
);

renderParam.renderBag.filterEditor = columnFilterEditor;

return (
<ContextProvider value={renderParam}>
<InfiniteTableCell<T>
Expand Down Expand Up @@ -643,26 +665,7 @@ export function InfiniteTableHeaderCell<T>(
cssEllipsis={headerCSSEllipsis}
afterChildren={
<>
{showColumnFilters ? (
column.computedFilterable ? (
<InfiniteTableColumnHeaderFilter
horizontalLayoutPageIndex={horizontalLayoutPageIndex}
filterEditor={FilterEditor}
filterOperatorSwitch={
FilterOperatorSwitch ?? InfiniteTableFilterOperatorSwitch
}
operator={operator}
filterTypes={filterTypes}
onChange={debouncedOnFilterValueChange}
columnFilterType={filterTypeKey}
columnLabel={column.field || column.name || column.id}
columnFilterValue={column.computedFilterValue}
columnHeaderHeight={columnHeaderHeight}
/>
) : (
<InfiniteTableColumnHeaderFilterEmpty />
)
) : null}
{showColumnFilters ? columnFilterEditor : null}
{resizeHandle}
</>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useMasterDetailContext } from '../../DataSource/publicHooks/useDataSourceState';
import { useInfiniteTable } from './useInfiniteTable';

export function useInfinitePortalContainer() {
const masterContext = useMasterDetailContext();

const masterState = masterContext ? masterContext.getMasterState() : null;
const infiniteState = useInfiniteTable().getState();

const portalContainer = (masterState || infiniteState).portalDOMRef.current;

return portalContainer;
}
2 changes: 2 additions & 0 deletions source/src/components/InfiniteTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import { DEBUG_NAME } from './InfiniteDebugName';
import { useToggleWrapRowsHorizontally } from './hooks/useToggleWrapRowsHorizontally';
import { useHorizontalLayout } from './hooks/useHorizontalLayout';
import { useDebugMode } from './hooks/useDebugMode';
import { useInfinitePortalContainer } from './hooks/useInfinitePortalContainer';

export const InfiniteTableClassName = internalProps.rootClassName;

Expand Down Expand Up @@ -605,6 +606,7 @@ export {
useInfiniteHeaderCell,
useInfiniteColumnEditor,
useInfiniteColumnFilterEditor,
useInfinitePortalContainer,
eventMatchesKeyboardShortcut,
useGridScroll,
useVisibleColumnSizes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ export type InfiniteTableColumnHeaderParam<
menuIcon?: Renderable;
menuIconProps?: MenuIconProps;
filterIcon?: Renderable;
filterEditor?: Renderable;
selectionCheckBox?: Renderable;
};
} & (
| {
domRef: InfiniteTableCellProps<DATA_TYPE>['domRef'];
htmlElementRef: React.MutableRefObject<HTMLElement | null>;
insideColumnMenu: false;
}
| {
Expand Down Expand Up @@ -160,6 +162,7 @@ export type InfiniteTableColumnCellContextType<
export type InfiniteTableHeaderCellContextType<DATA_TYPE> =
InfiniteTableColumnHeaderParam<DATA_TYPE> & {
domRef: InfiniteTableCellProps<DATA_TYPE>['domRef'];
htmlElementRef: React.MutableRefObject<HTMLElement | null>;
insideColumnMenu: false;
};

Expand Down
2 changes: 2 additions & 0 deletions source/src/components/InfiniteTable/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import type {
InfiniteTablePropMultiSortBehavior,
InfiniteTablePropKeyboardShorcut,
InfiniteTablePropColumnGroupVisibility,
InfiniteTablePropGetContextMenuItems,
} from './InfiniteTableProps';
import type { InfiniteTableState } from './InfiniteTableState';

Expand Down Expand Up @@ -115,4 +116,5 @@ export type {
InfiniteTableColumnValueFormatterParams,
ScrollStopInfo,
TableRenderRange,
InfiniteTablePropGetContextMenuItems,
};
1 change: 1 addition & 0 deletions source/src/components/InfiniteTable/utilities.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const userSelect = styleVariants({

export const height = styleVariants({
'100%': { height: '100%' },
'50%': { height: '50%' },
'0': { height: '0' },
});
export const width = styleVariants({
Expand Down
Loading

0 comments on commit e76dd95

Please sign in to comment.