diff --git a/packages/mantine-react-table/src/body/MRT_TableBody.tsx b/packages/mantine-react-table/src/body/MRT_TableBody.tsx index 6cba839b4..e3c5c2720 100644 --- a/packages/mantine-react-table/src/body/MRT_TableBody.tsx +++ b/packages/mantine-react-table/src/body/MRT_TableBody.tsx @@ -10,11 +10,12 @@ import { type MRT_VirtualItem, type MRT_Virtualizer, } from '../types'; +import { funcValue, styleValue } from '../funcValue'; interface Props = {}> { columnVirtualizer?: MRT_Virtualizer; enableHover?: boolean; - isStriped?: boolean; + isStriped?: boolean | 'odd' | 'even'; table: MRT_TableInstance; virtualColumns?: MRT_VirtualItem[]; virtualPaddingLeft?: number; @@ -64,15 +65,9 @@ export const MRT_TableBody = = {}>({ sorting, } = getState(); - const tableBodyProps = - mantineTableBodyProps instanceof Function - ? mantineTableBodyProps({ table }) - : mantineTableBodyProps; + const tableBodyProps = funcValue(mantineTableBodyProps, { table }); - const vProps = - rowVirtualizerProps instanceof Function - ? rowVirtualizerProps({ table }) - : rowVirtualizerProps; + const vProps = funcValue(rowVirtualizerProps, { table }); const shouldRankRows = useMemo( () => @@ -145,9 +140,7 @@ export const MRT_TableBody = = {}>({ : 'inherit', minHeight: !rows.length ? '100px' : undefined, position: 'relative', - ...(tableBodyProps?.style instanceof Function - ? tableBodyProps?.style(theme) - : (tableBodyProps?.style as any)), + ...styleValue(tableBodyProps, theme), })} > {creatingRow && createDisplayMode === 'row' && ( diff --git a/packages/mantine-react-table/src/body/MRT_TableBodyCell.tsx b/packages/mantine-react-table/src/body/MRT_TableBodyCell.tsx index d1fad93ab..0b33b77f2 100644 --- a/packages/mantine-react-table/src/body/MRT_TableBodyCell.tsx +++ b/packages/mantine-react-table/src/body/MRT_TableBodyCell.tsx @@ -22,11 +22,13 @@ import { type MRT_TableInstance, type MRT_VirtualItem, } from '../types'; +import { funcValue } from '../funcValue'; + import classes from './MRT_TableBodyRow.module.css'; interface Props = {}> { cell: MRT_Cell; - isStriped?: boolean; + isStriped?: boolean | 'odd' | 'even'; measureElement?: (element: HTMLTableCellElement) => void; numRows?: number; rowIndex: number; @@ -81,25 +83,13 @@ export const MRT_TableBodyCell = = {}>({ const { columnDef } = column; const { columnDefType } = columnDef; - const mTableCellBodyProps = - mantineTableBodyCellProps instanceof Function - ? mantineTableBodyCellProps({ cell, column, row, table }) - : mantineTableBodyCellProps; - - const mcTableCellBodyProps = - columnDef.mantineTableBodyCellProps instanceof Function - ? columnDef.mantineTableBodyCellProps({ cell, column, row, table }) - : columnDef.mantineTableBodyCellProps; - + const arg = { cell, column, row, table }; const tableCellProps = { - ...mTableCellBodyProps, - ...mcTableCellBodyProps, + ...funcValue(mantineTableBodyCellProps, arg), + ...funcValue(columnDef.mantineTableBodyCellProps, arg), }; - const skeletonProps = - mantineSkeletonProps instanceof Function - ? mantineSkeletonProps({ cell, column, row, table }) - : mantineSkeletonProps; + const skeletonProps = funcValue(mantineSkeletonProps, arg); const [skeletonWidth, setSkeletonWidth] = useState(100); useEffect(() => { @@ -152,10 +142,8 @@ export const MRT_TableBodyCell = = {}>({ }, [draggingColumn, draggingRow, hoveredColumn, hoveredRow, rowIndex]); const isEditable = - (enableEditing instanceof Function ? enableEditing(row) : enableEditing) && - (columnDef.enableEditing instanceof Function - ? columnDef.enableEditing(row) - : columnDef.enableEditing) !== false; + (funcValue(enableEditing, row) && + funcValue(columnDef.enableEditing, row)) !== false; const isEditing = isEditable && @@ -196,8 +184,8 @@ export const MRT_TableBodyCell = = {}>({ const { style, className, __vars } = getCommonCellStyles({ column, - isStriped, - row, + // isStriped, TODO: why were these here? + // row, table, theme, tableCellProps, @@ -230,7 +218,7 @@ export const MRT_TableBodyCell = = {}>({ '--white-space': density === 'xs' ? 'nowrap' : 'normal', '--z-index': draggingColumn?.id === column.id - ? 2 + ? '2' : column.getIsPinned() ? '1' : '0', diff --git a/packages/mantine-react-table/src/body/MRT_TableBodyCellValue.tsx b/packages/mantine-react-table/src/body/MRT_TableBodyCellValue.tsx index 5986e9785..d8e531eba 100644 --- a/packages/mantine-react-table/src/body/MRT_TableBodyCellValue.tsx +++ b/packages/mantine-react-table/src/body/MRT_TableBodyCellValue.tsx @@ -1,5 +1,6 @@ import { Highlight, type HighlightProps } from '@mantine/core'; import { type MRT_Cell, type MRT_TableInstance } from '../types'; +import { funcValue } from '../funcValue'; const allowedTypes = ['string', 'number']; const allowedFilterVariants = ['text', 'autocomplete']; @@ -22,11 +23,12 @@ export const MRT_TableBodyCellValue = = {}>({ const { globalFilter, globalFilterFn } = getState(); const filterValue = column.getFilterValue(); - const highlightProps = ( - mantineHighlightProps instanceof Function - ? mantineHighlightProps({ cell, column, row, table }) - : mantineHighlightProps - ) as Partial; + const highlightProps = funcValue(mantineHighlightProps, { + cell, + column, + row, + table, + }) as Partial; let renderedCellValue = cell.getIsAggregated() && columnDef.AggregatedCell @@ -75,11 +77,7 @@ export const MRT_TableBodyCellValue = = {}>({ } renderedCellValue = ( - + {renderedCellValue?.toString()} ); diff --git a/packages/mantine-react-table/src/body/MRT_TableBodyRow.tsx b/packages/mantine-react-table/src/body/MRT_TableBodyRow.tsx index 49470b279..28e68a022 100644 --- a/packages/mantine-react-table/src/body/MRT_TableBodyRow.tsx +++ b/packages/mantine-react-table/src/body/MRT_TableBodyRow.tsx @@ -1,10 +1,7 @@ import { type DragEvent, memo, useRef } from 'react'; import { Box } from '@mantine/core'; import clsx from 'clsx'; -import { - Memo_MRT_TableBodyCell, - MRT_TableBodyCell, -} from './MRT_TableBodyCell'; +import { Memo_MRT_TableBodyCell, MRT_TableBodyCell } from './MRT_TableBodyCell'; import { MRT_TableDetailPanel } from './MRT_TableDetailPanel'; import { type MRT_Cell, @@ -13,13 +10,14 @@ import { type MRT_VirtualItem, type MRT_Virtualizer, } from '../types'; +import { funcValue, styleValue } from '../funcValue'; import classes from './MRT_TableBodyRow.module.css'; interface Props = {}> { columnVirtualizer?: MRT_Virtualizer; enableHover?: boolean; - isStriped?: boolean; + isStriped?: boolean | 'odd' | 'even'; measureElement?: (element: HTMLTableRowElement) => void; numRows?: number; row: MRT_Row; @@ -61,10 +59,11 @@ export const MRT_TableBodyRow = = {}>({ const { draggingColumn, draggingRow, editingCell, editingRow, hoveredRow } = getState(); - const tableRowProps = - mantineTableBodyRowProps instanceof Function - ? mantineTableBodyRowProps({ row, staticRowIndex: rowIndex, table }) - : mantineTableBodyRowProps; + const tableRowProps = funcValue(mantineTableBodyRowProps, { + row, + staticRowIndex: rowIndex, + table, + }); const handleDragEnter = (_e: DragEvent) => { if (enableRowOrdering && draggingRow) { @@ -102,9 +101,7 @@ export const MRT_TableBodyRow = = {}>({ transform: virtualRow ? `translateY(${virtualRow?.start}px)` : undefined, - ...(tableRowProps?.style instanceof Function - ? tableRowProps.style(theme) - : (tableRowProps?.style as any)), + ...styleValue(tableRowProps, theme), })} > {virtualPaddingLeft ? ( diff --git a/packages/mantine-react-table/src/body/MRT_TableBodyRowGrabHandle.tsx b/packages/mantine-react-table/src/body/MRT_TableBodyRowGrabHandle.tsx index d796e20e3..667f240eb 100644 --- a/packages/mantine-react-table/src/body/MRT_TableBodyRowGrabHandle.tsx +++ b/packages/mantine-react-table/src/body/MRT_TableBodyRowGrabHandle.tsx @@ -1,6 +1,7 @@ import { type DragEvent, type RefObject } from 'react'; import { MRT_GrabHandleButton } from '../buttons/MRT_GrabHandleButton'; import { type MRT_Row, type MRT_TableInstance } from '../types'; +import { funcValue } from '../funcValue'; interface Props = {}> { row: MRT_Row; @@ -19,10 +20,7 @@ export const MRT_TableBodyRowGrabHandle = < options: { mantineRowDragHandleProps }, } = table; - const actionIconProps = - mantineRowDragHandleProps instanceof Function - ? mantineRowDragHandleProps({ row, table }) - : mantineRowDragHandleProps; + const actionIconProps = funcValue(mantineRowDragHandleProps, { row, table }); const handleDragStart = (event: DragEvent) => { actionIconProps?.onDragStart?.(event); diff --git a/packages/mantine-react-table/src/body/MRT_TableDetailPanel.tsx b/packages/mantine-react-table/src/body/MRT_TableDetailPanel.tsx index fe6899fc6..53223e603 100644 --- a/packages/mantine-react-table/src/body/MRT_TableDetailPanel.tsx +++ b/packages/mantine-react-table/src/body/MRT_TableDetailPanel.tsx @@ -5,6 +5,7 @@ import { type MRT_TableInstance, type MRT_VirtualItem, } from '../types'; +import { funcValue, styleValue } from '../funcValue'; interface Props = {}> { parentRowRef: React.RefObject; @@ -33,20 +34,14 @@ export const MRT_TableDetailPanel = = {}>({ } = table; const { isLoading } = getState(); - const tableRowProps = - mantineTableBodyRowProps instanceof Function - ? mantineTableBodyRowProps({ - isDetailPanel: true, - row, - staticRowIndex: rowIndex, - table, - }) - : mantineTableBodyRowProps; + const tableRowProps = funcValue(mantineTableBodyRowProps, { + isDetailPanel: true, + row, + staticRowIndex: rowIndex, + table, + }); - const tableCellProps = - mantineDetailPanelProps instanceof Function - ? mantineDetailPanelProps({ row, table }) - : mantineDetailPanelProps; + const tableCellProps = funcValue(mantineDetailPanelProps, { row, table }); return ( = {}>({ : undefined, width: '100%', zIndex: virtualRow ? 2 : undefined, - ...(tableRowProps?.style instanceof Function - ? tableRowProps.style(theme) - : (tableRowProps?.style as any)), + ...styleValue(tableRowProps, theme), })} > = {}>({ paddingTop: row.getIsExpanded() ? '16px !important' : '0 !important', transition: 'all 100ms ease-in-out', width: `${table.getTotalSize()}px`, - ...(tableCellProps?.style instanceof Function - ? tableCellProps.style(theme) - : (tableCellProps?.style as any)), + ...styleValue(tableCellProps, theme), })} > {renderDetailPanel && ( diff --git a/packages/mantine-react-table/src/buttons/MRT_CopyButton.tsx b/packages/mantine-react-table/src/buttons/MRT_CopyButton.tsx index d7b760867..a8172d9fb 100644 --- a/packages/mantine-react-table/src/buttons/MRT_CopyButton.tsx +++ b/packages/mantine-react-table/src/buttons/MRT_CopyButton.tsx @@ -2,6 +2,7 @@ import { type ReactNode } from 'react'; import { UnstyledButton, CopyButton, Tooltip, rgba } from '@mantine/core'; import { type MRT_Cell, type MRT_TableInstance } from '../types'; import { getPrimaryColor } from '../column.utils'; +import { funcValue, styleValue } from '../funcValue'; interface Props = {}> { cell: MRT_Cell; @@ -20,24 +21,10 @@ export const MRT_CopyButton = = {}>({ const { column, row } = cell; const { columnDef } = column; - const mTableBodyCellCopyButtonProps = - mantineCopyButtonProps instanceof Function - ? mantineCopyButtonProps({ cell, column, row, table }) - : mantineCopyButtonProps; - - const mcTableBodyCellCopyButtonProps = - columnDef.mantineCopyButtonProps instanceof Function - ? columnDef.mantineCopyButtonProps({ - cell, - column, - row, - table, - }) - : columnDef.mantineCopyButtonProps; - + const arg = { cell, column, row, table }; const buttonProps = { - ...mTableBodyCellCopyButtonProps, - ...mcTableBodyCellCopyButtonProps, + ...funcValue(mantineCopyButtonProps, arg), + ...funcValue(columnDef.mantineCopyButtonProps, arg), }; return ( @@ -80,9 +67,7 @@ export const MRT_CopyButton = = {}>({ '&:hover': { backgroundColor: rgba(getPrimaryColor(theme), 0.1), }, - ...(buttonProps?.style instanceof Function - ? buttonProps.style(theme) - : (buttonProps?.style as any)), + ...styleValue(buttonProps, theme), })} title={undefined} > diff --git a/packages/mantine-react-table/src/buttons/MRT_ExpandAllButton.tsx b/packages/mantine-react-table/src/buttons/MRT_ExpandAllButton.tsx index 9e67fc9d4..b82c57709 100644 --- a/packages/mantine-react-table/src/buttons/MRT_ExpandAllButton.tsx +++ b/packages/mantine-react-table/src/buttons/MRT_ExpandAllButton.tsx @@ -1,5 +1,6 @@ import { ActionIcon, Tooltip } from '@mantine/core'; import { type MRT_TableInstance } from '../types'; +import { funcValue, styleValue } from '../funcValue'; interface Props = {}> { table: MRT_TableInstance; @@ -23,10 +24,7 @@ export const MRT_ExpandAllButton = = {}>({ } = table; const { density, isLoading } = getState(); - const actionIconProps = - mantineExpandAllButtonProps instanceof Function - ? mantineExpandAllButtonProps({ table }) - : mantineExpandAllButtonProps; + const actionIconProps = funcValue(mantineExpandAllButtonProps, { table }); const isAllRowsExpanded = getIsAllRowsExpanded(); @@ -56,9 +54,7 @@ export const MRT_ExpandAllButton = = {}>({ '&:hover': { opacity: 1, }, - ...(actionIconProps?.style instanceof Function - ? actionIconProps?.style(theme) - : (actionIconProps?.style as any)), + ...styleValue(actionIconProps, theme), })} title={undefined} > diff --git a/packages/mantine-react-table/src/buttons/MRT_ExpandButton.tsx b/packages/mantine-react-table/src/buttons/MRT_ExpandButton.tsx index 979326621..46fb23df4 100644 --- a/packages/mantine-react-table/src/buttons/MRT_ExpandButton.tsx +++ b/packages/mantine-react-table/src/buttons/MRT_ExpandButton.tsx @@ -1,6 +1,7 @@ import { type MouseEvent } from 'react'; import { ActionIcon, Tooltip } from '@mantine/core'; import { type MRT_Row, type MRT_TableInstance } from '../types'; +import { funcValue, styleValue } from '../funcValue'; interface Props = {}> { row: MRT_Row; @@ -20,11 +21,7 @@ export const MRT_ExpandButton = = {}>({ }, } = table; - const actionIconProps = - mantineExpandButtonProps instanceof Function - ? mantineExpandButtonProps({ table, row }) - : mantineExpandButtonProps; - + const actionIconProps = funcValue(mantineExpandButtonProps, { table, row }); const canExpand = row.getCanExpand(); const isExpanded = row.getIsExpanded(); @@ -59,9 +56,7 @@ export const MRT_ExpandButton = = {}>({ '&:hover': { opacity: 1, }, - ...(actionIconProps?.style instanceof Function - ? actionIconProps.style(theme) - : (actionIconProps?.style as any)), + ...styleValue(actionIconProps, theme), })} title={undefined} > diff --git a/packages/mantine-react-table/src/buttons/MRT_GrabHandleButton.tsx b/packages/mantine-react-table/src/buttons/MRT_GrabHandleButton.tsx index 192d6276e..ff2478d97 100644 --- a/packages/mantine-react-table/src/buttons/MRT_GrabHandleButton.tsx +++ b/packages/mantine-react-table/src/buttons/MRT_GrabHandleButton.tsx @@ -1,6 +1,7 @@ import { type DragEventHandler } from 'react'; import { ActionIcon, type ActionIconProps, Tooltip } from '@mantine/core'; import { type HTMLPropsRef, type MRT_TableInstance } from '../types'; +import { styleValue } from '../funcValue'; interface Props = {}> { actionIconProps?: ActionIconProps & HTMLPropsRef; @@ -52,9 +53,7 @@ export const MRT_GrabHandleButton = = {}>({ '&:active': { cursor: 'grabbing', }, - ...(actionIconProps?.style instanceof Function - ? actionIconProps?.style(theme) - : (actionIconProps?.style as any)), + ...styleValue(actionIconProps, theme), })} title={undefined} > diff --git a/packages/mantine-react-table/src/buttons/MRT_ToggleRowActionMenuButton.tsx b/packages/mantine-react-table/src/buttons/MRT_ToggleRowActionMenuButton.tsx index 439492c63..ff9e0ef53 100644 --- a/packages/mantine-react-table/src/buttons/MRT_ToggleRowActionMenuButton.tsx +++ b/packages/mantine-react-table/src/buttons/MRT_ToggleRowActionMenuButton.tsx @@ -3,6 +3,8 @@ import { ActionIcon, Tooltip } from '@mantine/core'; import { MRT_RowActionMenu } from '../menus/MRT_RowActionMenu'; import { MRT_EditActionButtons } from './MRT_EditActionButtons'; import { type MRT_Cell, type MRT_Row, type MRT_TableInstance } from '../types'; +import { funcValue } from '../funcValue'; + import classes from './MRT_ToggleRowActionMenuButton.module.css'; interface Props = {}> { @@ -52,10 +54,7 @@ export const MRT_ToggleRowActionMenuButton = < renderRowActions({ cell, row, table }) ) : showEditActionButtons ? ( - ) : !renderRowActionMenuItems && - (enableEditing instanceof Function - ? enableEditing(row) - : enableEditing) ? ( + ) : !renderRowActionMenuItems && funcValue(enableEditing, row) ? ( = {}>( columnDef: MRT_ColumnDef, @@ -355,13 +356,12 @@ export const getCommonCellStyles = = {}>({ __vars['--transition'] = table.options.enableColumnVirtualization ? 'none' : `padding 100ms ease-in-out`; + return { className: classes.MRT_ColumnCommonStyles, style: { ...(!table.options.enableColumnResizing && widthStyles), //let devs pass in width styles if column resizing is disabled - ...(tableCellProps?.style instanceof Function - ? tableCellProps.style(theme) - : (tableCellProps?.style as any)), + ...funcValue(tableCellProps?.style, theme), ...(table.options.enableColumnResizing && widthStyles), //do not let devs pass in width styles if column resizing is enabled }, __vars, @@ -393,11 +393,15 @@ export const MRT_DefaultDisplayColumn = { export const parseCSSVarId = (id: string) => id.replace(/[^a-zA-Z0-9]/g, '_'); export const getPrimaryShade = (theme: MantineTheme): number => - (theme.colorScheme === 'dark' - ? // @ts-ignore - theme.primaryShade?.dark ?? theme.primaryShade - : // @ts-ignore - theme.primaryShade?.light ?? theme.primaryShade) ?? 7; + typeof theme.primaryShade === 'number' + ? theme.primaryShade + : theme.primaryShade?.dark ?? 7; +// TODO: where is colorScheme? +// (theme.colorScheme === 'dark' +// ? // @ts-ignore +// theme.primaryShade?.dark ?? theme.primaryShade +// : // @ts-ignore +// theme.primaryShade?.light ?? theme.primaryShade) ?? 7; export const getPrimaryColor = ( theme: MantineTheme, diff --git a/packages/mantine-react-table/src/footer/MRT_TableFooter.tsx b/packages/mantine-react-table/src/footer/MRT_TableFooter.tsx index 92493cebb..06489e864 100644 --- a/packages/mantine-react-table/src/footer/MRT_TableFooter.tsx +++ b/packages/mantine-react-table/src/footer/MRT_TableFooter.tsx @@ -2,6 +2,8 @@ import { Box } from '@mantine/core'; import clsx from 'clsx'; import { MRT_TableFooterRow } from './MRT_TableFooterRow'; import { type MRT_TableInstance, type MRT_VirtualItem } from '../types'; +import { funcValue, styleValue } from '../funcValue'; + import classes from './MRT_TableFooter.module.css'; interface Props = {}> { @@ -24,10 +26,7 @@ export const MRT_TableFooter = = {}>({ } = table; const { isFullScreen } = getState(); - const tableFooterProps = - mantineTableFooterProps instanceof Function - ? mantineTableFooterProps({ table }) - : mantineTableFooterProps; + const tableFooterProps = funcValue(mantineTableFooterProps, { table }); const stickFooter = (isFullScreen || enableStickyFooter) && enableStickyFooter !== false; @@ -43,9 +42,7 @@ export const MRT_TableFooter = = {}>({ : classes.MRT_TableFooterTableRowGroup, )} style={(theme) => ({ - ...(tableFooterProps?.style instanceof Function - ? tableFooterProps?.style(theme) - : (tableFooterProps?.style as any)), + ...styleValue(tableFooterProps, theme), })} > {getFooterGroups().map((footerGroup) => ( diff --git a/packages/mantine-react-table/src/footer/MRT_TableFooterCell.tsx b/packages/mantine-react-table/src/footer/MRT_TableFooterCell.tsx index ad43ccba2..33d23e0f9 100644 --- a/packages/mantine-react-table/src/footer/MRT_TableFooterCell.tsx +++ b/packages/mantine-react-table/src/footer/MRT_TableFooterCell.tsx @@ -2,6 +2,8 @@ import { Box, useMantineTheme } from '@mantine/core'; import clsx from 'clsx'; import { getCommonCellStyles } from '../column.utils'; import { type MRT_Header, type MRT_TableInstance } from '../types'; +import { funcValue } from '../funcValue'; + import classes from './MRT_TableFooterCell.module.css'; interface Props = {}> { @@ -21,19 +23,10 @@ export const MRT_TableFooterCell = = {}>({ const { columnDef } = column; const { columnDefType } = columnDef; - const mTableFooterCellProps = - mantineTableFooterCellProps instanceof Function - ? mantineTableFooterCellProps({ column, table }) - : mantineTableFooterCellProps; - - const mcTableFooterCellProps = - columnDef.mantineTableFooterCellProps instanceof Function - ? columnDef.mantineTableFooterCellProps({ column, table }) - : columnDef.mantineTableFooterCellProps; - + const arg = { column, table }; const { className, ...tableCellProps } = { - ...mTableFooterCellProps, - ...mcTableFooterCellProps, + ...funcValue(mantineTableFooterCellProps, arg), + ...funcValue(columnDef.mantineTableFooterCellProps, arg), }; const { @@ -47,6 +40,14 @@ export const MRT_TableFooterCell = = {}>({ tableCellProps, }); + const footerProps = footer.isPlaceholder + ? null + : funcValue(columnDef.Footer, { + column, + footer, + table, + }); + return ( = {}>({ }} style={style} > - <> - {footer.isPlaceholder - ? null - : (columnDef.Footer instanceof Function - ? columnDef.Footer?.({ - column, - footer, - table, - }) - : columnDef.Footer) ?? - columnDef.footer ?? - null} - + <>{footerProps} ); }; diff --git a/packages/mantine-react-table/src/footer/MRT_TableFooterRow.tsx b/packages/mantine-react-table/src/footer/MRT_TableFooterRow.tsx index 0ab41046d..a9afa5aa7 100644 --- a/packages/mantine-react-table/src/footer/MRT_TableFooterRow.tsx +++ b/packages/mantine-react-table/src/footer/MRT_TableFooterRow.tsx @@ -7,6 +7,8 @@ import { type MRT_TableInstance, type MRT_VirtualItem, } from '../types'; +import { funcValue, styleValue } from '../funcValue'; + import classes from './MRT_TableFooterRow.module.css'; interface Props = {}> { @@ -39,10 +41,10 @@ export const MRT_TableFooterRow = = {}>({ ) return null; - const tableRowProps = - mantineTableFooterRowProps instanceof Function - ? mantineTableFooterRowProps({ footerGroup, table }) - : mantineTableFooterRowProps; + const tableRowProps = funcValue(mantineTableFooterRowProps, { + footerGroup, + table, + }); return ( = {}>({ )} {...tableRowProps} style={(theme) => ({ - ...(tableRowProps?.style instanceof Function - ? tableRowProps?.style(theme) - : (tableRowProps?.style as any)), + ...styleValue(tableRowProps, theme), })} > {virtualPaddingLeft ? ( diff --git a/packages/mantine-react-table/src/funcValue.ts b/packages/mantine-react-table/src/funcValue.ts new file mode 100644 index 000000000..04d1d7d31 --- /dev/null +++ b/packages/mantine-react-table/src/funcValue.ts @@ -0,0 +1,16 @@ +import type { CSSProperties } from 'react'; +import type { MantineStyleProp, MantineTheme } from '@mantine/core'; + +export function funcValue( + fn: T | ((arg: U) => T) | undefined, + arg: U, +): T | undefined { + return fn instanceof Function ? fn(arg) : fn; +} + +export function styleValue( + x: { style?: MantineStyleProp } | undefined, + theme: MantineTheme, +) { + return funcValue(x?.style, theme) as CSSProperties | undefined; +} diff --git a/packages/mantine-react-table/src/head/MRT_TableHead.tsx b/packages/mantine-react-table/src/head/MRT_TableHead.tsx index 1437fef1a..e2932e138 100644 --- a/packages/mantine-react-table/src/head/MRT_TableHead.tsx +++ b/packages/mantine-react-table/src/head/MRT_TableHead.tsx @@ -2,6 +2,7 @@ import { Box } from '@mantine/core'; import { MRT_TableHeadRow } from './MRT_TableHeadRow'; import { MRT_ToolbarAlertBanner } from '../toolbar'; import { type MRT_TableInstance, type MRT_VirtualItem } from '../types'; +import { funcValue, styleValue } from '../funcValue'; interface Props = {}> { table: MRT_TableInstance; @@ -29,10 +30,7 @@ export const MRT_TableHead = = {}>({ } = table; const { isFullScreen, showAlertBanner } = getState(); - const tableHeadProps = - mantineTableHeadProps instanceof Function - ? mantineTableHeadProps({ table }) - : mantineTableHeadProps; + const tableHeadProps = funcValue(mantineTableHeadProps, { table }); const stickyHeader = enableStickyHeader || isFullScreen; @@ -46,9 +44,7 @@ export const MRT_TableHead = = {}>({ opacity: 0.97, top: stickyHeader ? 0 : undefined, zIndex: stickyHeader ? 2 : undefined, - ...(tableHeadProps?.style instanceof Function - ? tableHeadProps?.style(theme) - : (tableHeadProps?.style as any)), + ...styleValue(tableHeadProps, theme), })} > {positionToolbarAlertBanner === 'head-overlay' && diff --git a/packages/mantine-react-table/src/head/MRT_TableHeadCell.tsx b/packages/mantine-react-table/src/head/MRT_TableHeadCell.tsx index c3a3fe43f..a0772c54a 100644 --- a/packages/mantine-react-table/src/head/MRT_TableHeadCell.tsx +++ b/packages/mantine-react-table/src/head/MRT_TableHeadCell.tsx @@ -1,5 +1,5 @@ import { type DragEvent, type ReactNode, useMemo } from 'react'; -import { Box, Flex, type MantineTheme, useMantineTheme } from '@mantine/core'; +import { Box, Flex, useMantineTheme } from '@mantine/core'; import clsx from 'clsx'; import { MRT_ColumnActionMenu } from '../menus/MRT_ColumnActionMenu'; import { MRT_TableHeadCellFilterContainer } from './MRT_TableHeadCellFilterContainer'; @@ -9,6 +9,7 @@ import { MRT_TableHeadCellResizeHandle } from './MRT_TableHeadCellResizeHandle'; import { MRT_TableHeadCellSortLabel } from './MRT_TableHeadCellSortLabel'; import { getCommonCellStyles } from '../column.utils'; import { type MRT_Header, type MRT_TableInstance } from '../types'; +import { funcValue } from '../funcValue'; import classes from './MRT_TableHeadCell.module.css'; @@ -42,19 +43,10 @@ export const MRT_TableHeadCell = = {}>({ const { columnDef } = column; const { columnDefType } = columnDef; - const mTableHeadCellProps = - mantineTableHeadCellProps instanceof Function - ? mantineTableHeadCellProps({ column, table }) - : mantineTableHeadCellProps; - - const mcTableHeadCellProps = - columnDef.mantineTableHeadCellProps instanceof Function - ? columnDef.mantineTableHeadCellProps({ column, table }) - : columnDef.mantineTableHeadCellProps; - + const arg = { column, table }; const tableCellProps = { - ...mTableHeadCellProps, - ...mcTableHeadCellProps, + ...funcValue(mantineTableHeadCellProps, arg), + ...funcValue(columnDef.mantineTableHeadCellProps, arg), }; const showColumnActions = @@ -108,13 +100,12 @@ export const MRT_TableHeadCell = = {}>({ }; const headerElement = - columnDef?.Header instanceof Function - ? columnDef?.Header?.({ - column, - header, - table, - }) - : columnDef?.Header ?? (columnDef.header as ReactNode); + funcValue(columnDef?.Header, { + column, + header, + table, + }) ?? (columnDef.header as ReactNode); + const { className, __vars, style } = getCommonCellStyles({ column, header, diff --git a/packages/mantine-react-table/src/head/MRT_TableHeadCellGrabHandle.tsx b/packages/mantine-react-table/src/head/MRT_TableHeadCellGrabHandle.tsx index 9c5249be9..cc5e66ba5 100644 --- a/packages/mantine-react-table/src/head/MRT_TableHeadCellGrabHandle.tsx +++ b/packages/mantine-react-table/src/head/MRT_TableHeadCellGrabHandle.tsx @@ -2,6 +2,7 @@ import { type DragEvent, type RefObject } from 'react'; import { MRT_GrabHandleButton } from '../buttons/MRT_GrabHandleButton'; import { reorderColumn } from '../column.utils'; import { type MRT_Column, type MRT_TableInstance } from '../types'; +import { funcValue } from '../funcValue'; interface Props = {}> { column: MRT_Column; @@ -26,19 +27,10 @@ export const MRT_TableHeadCellGrabHandle = < const { columnDef } = column; const { hoveredColumn, draggingColumn, columnOrder } = getState(); - const mActionIconProps = - mantineColumnDragHandleProps instanceof Function - ? mantineColumnDragHandleProps({ column, table }) - : mantineColumnDragHandleProps; - - const mcActionIconProps = - columnDef.mantineColumnDragHandleProps instanceof Function - ? columnDef.mantineColumnDragHandleProps({ column, table }) - : columnDef.mantineColumnDragHandleProps; - + const arg = { column, table }; const actionIconProps = { - ...mActionIconProps, - ...mcActionIconProps, + ...funcValue(mantineColumnDragHandleProps, arg), + ...funcValue(columnDef.mantineColumnDragHandleProps, arg), }; const handleDragStart = (event: DragEvent) => { diff --git a/packages/mantine-react-table/src/head/MRT_TableHeadCellResizeHandle.tsx b/packages/mantine-react-table/src/head/MRT_TableHeadCellResizeHandle.tsx index c05e87698..5b3a2bc93 100644 --- a/packages/mantine-react-table/src/head/MRT_TableHeadCellResizeHandle.tsx +++ b/packages/mantine-react-table/src/head/MRT_TableHeadCellResizeHandle.tsx @@ -1,6 +1,6 @@ import { Box, Divider } from '@mantine/core'; import { type MRT_Header, type MRT_TableInstance } from '../types'; -import { getPrimaryColor } from '../column.utils'; + import classes from './MRT_TableHeadCellResizeHandle.module.css'; interface Props = {}> { diff --git a/packages/mantine-react-table/src/head/MRT_TableHeadRow.tsx b/packages/mantine-react-table/src/head/MRT_TableHeadRow.tsx index cf397d1a3..fc1958fcf 100644 --- a/packages/mantine-react-table/src/head/MRT_TableHeadRow.tsx +++ b/packages/mantine-react-table/src/head/MRT_TableHeadRow.tsx @@ -1,4 +1,4 @@ -import { Box, rgba } from '@mantine/core'; +import { Box } from '@mantine/core'; import clsx from 'clsx'; import { MRT_TableHeadCell } from './MRT_TableHeadCell'; import { @@ -7,6 +7,8 @@ import { type MRT_TableInstance, type MRT_VirtualItem, } from '../types'; +import { funcValue, styleValue } from '../funcValue'; + import classes from './MRT_TableHeadRow.module.css'; interface Props = {}> { @@ -30,10 +32,10 @@ export const MRT_TableHeadRow = = {}>({ } = table; const { isFullScreen } = getState(); - const tableRowProps = - mantineTableHeadRowProps instanceof Function - ? mantineTableHeadRowProps({ headerGroup, table }) - : mantineTableHeadRowProps; + const tableRowProps = funcValue(mantineTableHeadRowProps, { + headerGroup, + table, + }); const stickyHeader = enableStickyHeader || isFullScreen; @@ -49,9 +51,7 @@ export const MRT_TableHeadRow = = {}>({ '--display': layoutMode === 'grid' ? 'flex' : 'table-row', }} style={(theme) => ({ - ...(tableRowProps?.style instanceof Function - ? tableRowProps?.style(theme) - : (tableRowProps?.style as any)), + ...styleValue(tableRowProps, theme), })} > {virtualPaddingLeft ? ( diff --git a/packages/mantine-react-table/src/inputs/MRT_EditCellTextInput.tsx b/packages/mantine-react-table/src/inputs/MRT_EditCellTextInput.tsx index 9e49ee6e9..c7959d8aa 100644 --- a/packages/mantine-react-table/src/inputs/MRT_EditCellTextInput.tsx +++ b/packages/mantine-react-table/src/inputs/MRT_EditCellTextInput.tsx @@ -1,6 +1,7 @@ import { type FocusEvent, type KeyboardEvent, useState } from 'react'; import { Select, TextInput } from '@mantine/core'; import { type MRT_Cell, type MRT_TableInstance } from '../types'; +import { funcValue } from '../funcValue'; interface Props = {}> { cell: MRT_Cell; @@ -34,44 +35,15 @@ export const MRT_EditCellTextInput = = {}>({ const [value, setValue] = useState(() => cell.getValue()); - const mTableBodyCellEditTextInputProps = - mantineEditTextInputProps instanceof Function - ? mantineEditTextInputProps({ cell, column, row, table }) - : mantineEditTextInputProps; - - const mcTableBodyCellEditTextInputProps = - columnDef.mantineEditTextInputProps instanceof Function - ? columnDef.mantineEditTextInputProps({ - cell, - column, - row, - table, - }) - : columnDef.mantineEditTextInputProps; - + const arg = { cell, column, row, table }; const textInputProps = { - ...mTableBodyCellEditTextInputProps, - ...mcTableBodyCellEditTextInputProps, + ...funcValue(mantineEditTextInputProps, arg), + ...funcValue(columnDef.mantineEditTextInputProps, arg), }; - const mTableBodyCellEditSelectProps = - mantineEditSelectProps instanceof Function - ? mantineEditSelectProps({ cell, column, row, table }) - : mantineEditSelectProps; - - const mcTableBodyCellEditSelectProps = - columnDef.mantineEditSelectProps instanceof Function - ? columnDef.mantineEditSelectProps({ - cell, - column, - row, - table, - }) - : columnDef.mantineEditSelectProps; - const selectProps = { - ...mTableBodyCellEditSelectProps, - ...mcTableBodyCellEditSelectProps, + ...funcValue(mantineEditSelectProps, arg), + ...funcValue(columnDef.mantineEditSelectProps, arg), }; const saveInputValueToRowCache = (newValue: string | null) => { @@ -102,10 +74,7 @@ export const MRT_EditCellTextInput = = {}>({ } const commonProps = { - disabled: - (columnDef.enableEditing instanceof Function - ? columnDef.enableEditing(row) - : columnDef.enableEditing) === false, + disabled: funcValue(columnDef.enableEditing, row) === false, label: ['modal', 'custom'].includes( (isCreating ? createDisplayMode : editDisplayMode) as string, ) diff --git a/packages/mantine-react-table/src/inputs/MRT_FilterCheckbox.tsx b/packages/mantine-react-table/src/inputs/MRT_FilterCheckbox.tsx index 6648ba12f..f1432dc32 100644 --- a/packages/mantine-react-table/src/inputs/MRT_FilterCheckbox.tsx +++ b/packages/mantine-react-table/src/inputs/MRT_FilterCheckbox.tsx @@ -1,5 +1,6 @@ import { Checkbox, type CheckboxProps, Tooltip } from '@mantine/core'; import { type MRT_Column, type MRT_TableInstance } from '../types'; +import { funcValue, styleValue } from '../funcValue'; interface Props = {}> { column: MRT_Column; @@ -17,25 +18,10 @@ export const MRT_FilterCheckbox = = {}>({ const { density } = getState(); const { columnDef } = column; - const mTableHeadCellFilterCheckboxProps = - mantineFilterCheckboxProps instanceof Function - ? mantineFilterCheckboxProps({ - column, - table, - }) - : mantineFilterCheckboxProps; - - const mcTableHeadCellFilterCheckboxProps = - columnDef.mantineFilterCheckboxProps instanceof Function - ? columnDef.mantineFilterCheckboxProps({ - column, - table, - }) - : columnDef.mantineFilterCheckboxProps; - + const arg = { column, table }; const checkboxProps = { - ...mTableHeadCellFilterCheckboxProps, - ...mcTableHeadCellFilterCheckboxProps, + ...funcValue(mantineFilterCheckboxProps, arg), + ...funcValue(columnDef.mantineFilterCheckboxProps, arg), } as CheckboxProps; const filterLabel = localization.filterByColumn?.replace( @@ -73,9 +59,7 @@ export const MRT_FilterCheckbox = = {}>({ style={(theme) => ({ fontWeight: 'normal', marginTop: '8px', - ...(checkboxProps?.style instanceof Function - ? checkboxProps.style(theme) - : (checkboxProps?.style as any)), + ...styleValue(checkboxProps, theme), })} title={undefined} /> diff --git a/packages/mantine-react-table/src/inputs/MRT_FilterRangeSlider.tsx b/packages/mantine-react-table/src/inputs/MRT_FilterRangeSlider.tsx index 32e1fccf1..cdd0b460b 100644 --- a/packages/mantine-react-table/src/inputs/MRT_FilterRangeSlider.tsx +++ b/packages/mantine-react-table/src/inputs/MRT_FilterRangeSlider.tsx @@ -1,6 +1,7 @@ import { useEffect, useRef, useState } from 'react'; import { RangeSlider, type RangeSliderProps } from '@mantine/core'; import { type MRT_TableInstance, type MRT_Header } from '../types'; +import { funcValue, styleValue } from '../funcValue'; interface Props = {}> { header: MRT_Header; @@ -18,25 +19,10 @@ export const MRT_FilterRangeSlider = = {}>({ const { column } = header; const { columnDef } = column; - const mFilterRangeSliderProps = - mantineFilterRangeSliderProps instanceof Function - ? mantineFilterRangeSliderProps({ - column, - table, - }) - : mantineFilterRangeSliderProps; - - const mcFilterRangeSliderProps = - columnDef.mantineFilterRangeSliderProps instanceof Function - ? columnDef.mantineFilterRangeSliderProps({ - column, - table, - }) - : columnDef.mantineFilterRangeSliderProps; - + const arg = { column, table }; const rangeSliderProps = { - ...mFilterRangeSliderProps, - ...mcFilterRangeSliderProps, + ...funcValue(mantineFilterRangeSliderProps, arg), + ...funcValue(columnDef.mantineFilterRangeSliderProps, arg), } as RangeSliderProps; let [min, max] = @@ -106,9 +92,7 @@ export const MRT_FilterRangeSlider = = {}>({ marginTop: '16px', marginBottom: '6px', width: 'calc(100% - 8px)', - ...(rangeSliderProps?.style instanceof Function - ? rangeSliderProps.style(theme) - : (rangeSliderProps?.style as any)), + ...styleValue(rangeSliderProps, theme), })} /> ); diff --git a/packages/mantine-react-table/src/inputs/MRT_FilterTextInput.tsx b/packages/mantine-react-table/src/inputs/MRT_FilterTextInput.tsx index b29460a15..d44863952 100644 --- a/packages/mantine-react-table/src/inputs/MRT_FilterTextInput.tsx +++ b/packages/mantine-react-table/src/inputs/MRT_FilterTextInput.tsx @@ -14,6 +14,8 @@ import clsx from 'clsx'; import { DateInput } from '@mantine/dates'; import { useDebouncedValue } from '@mantine/hooks'; import { type MRT_Header, type MRT_TableInstance } from '../types'; +import { funcValue } from '../funcValue'; + import classes from './MRT_FilterTextInput.module.css'; interface Props = {}> { @@ -65,99 +67,30 @@ export const MRT_FilterTextInput = = {}>({ const { column } = header; const { columnDef } = column; - const mFilterTextInputProps = - mantineFilterTextInputProps instanceof Function - ? mantineFilterTextInputProps({ - column, - table, - rangeFilterIndex, - }) - : mantineFilterTextInputProps; - - const mcFilterTextInputProps = - columnDef.mantineFilterTextInputProps instanceof Function - ? columnDef.mantineFilterTextInputProps({ - column, - table, - rangeFilterIndex, - }) - : columnDef.mantineFilterTextInputProps; - + const arg = { column, table, rangeFilterIndex }; const textInputProps = { - ...mFilterTextInputProps, - ...mcFilterTextInputProps, + ...funcValue(mantineFilterTextInputProps, arg), + ...funcValue(columnDef.mantineFilterTextInputProps, arg), }; - const mSelectProps = - mantineFilterSelectProps instanceof Function - ? mantineFilterSelectProps({ column, table, rangeFilterIndex }) - : mantineFilterSelectProps; - - const mcSelectProps = - columnDef.mantineFilterSelectProps instanceof Function - ? columnDef.mantineFilterSelectProps({ column, table, rangeFilterIndex }) - : columnDef.mantineFilterSelectProps; - const selectProps = { - ...mSelectProps, - ...mcSelectProps, + ...funcValue(mantineFilterSelectProps, arg), + ...funcValue(columnDef.mantineFilterSelectProps, arg), }; - const mMultiSelectProps = - mantineFilterMultiSelectProps instanceof Function - ? mantineFilterMultiSelectProps({ column, table, rangeFilterIndex }) - : mantineFilterMultiSelectProps; - - const mcMultiSelectProps = - columnDef.mantineFilterMultiSelectProps instanceof Function - ? columnDef.mantineFilterMultiSelectProps({ - column, - table, - rangeFilterIndex, - }) - : columnDef.mantineFilterMultiSelectProps; - const multiSelectProps = { - ...mMultiSelectProps, - ...mcMultiSelectProps, + ...funcValue(mantineFilterMultiSelectProps, arg), + ...funcValue(columnDef.mantineFilterMultiSelectProps, arg), }; - const mDateInputProps = - mantineFilterDateInputProps instanceof Function - ? mantineFilterDateInputProps({ column, table, rangeFilterIndex }) - : mantineFilterDateInputProps; - - const mcDateInputProps = - columnDef.mantineFilterDateInputProps instanceof Function - ? columnDef.mantineFilterDateInputProps({ - column, - table, - rangeFilterIndex, - }) - : columnDef.mantineFilterDateInputProps; - const dateInputProps = { - ...mDateInputProps, - ...mcDateInputProps, + ...funcValue(mantineFilterDateInputProps, arg), + ...funcValue(columnDef.mantineFilterDateInputProps, arg), }; - const mAutoCompleteProps = - mantineFilterAutocompleteProps instanceof Function - ? mantineFilterAutocompleteProps({ column, table, rangeFilterIndex }) - : mantineFilterAutocompleteProps; - - const mcAutoCompleteProps = - columnDef.mantineFilterAutocompleteProps instanceof Function - ? columnDef.mantineFilterAutocompleteProps({ - column, - table, - rangeFilterIndex, - }) - : columnDef.mantineFilterAutocompleteProps; - const autoCompleteProps = { - ...mAutoCompleteProps, - ...mcAutoCompleteProps, + ...funcValue(mantineFilterAutocompleteProps, arg), + ...funcValue(columnDef.mantineFilterAutocompleteProps, arg), }; const isRangeFilter = diff --git a/packages/mantine-react-table/src/inputs/MRT_GlobalFilterTextInput.tsx b/packages/mantine-react-table/src/inputs/MRT_GlobalFilterTextInput.tsx index c6ce91ede..7fc100af8 100644 --- a/packages/mantine-react-table/src/inputs/MRT_GlobalFilterTextInput.tsx +++ b/packages/mantine-react-table/src/inputs/MRT_GlobalFilterTextInput.tsx @@ -4,6 +4,8 @@ import { ActionIcon, Collapse, Menu, TextInput, Tooltip } from '@mantine/core'; import { useDebouncedValue } from '@mantine/hooks'; import { MRT_FilterOptionMenu } from '../menus/MRT_FilterOptionMenu'; import { type MRT_TableInstance } from '../types'; +import { funcValue, styleValue } from '../funcValue'; + import classes from './MRT_GlobalFilterTextInput.module.css'; interface Props = {}> { @@ -29,10 +31,7 @@ export const MRT_GlobalFilterTextInput = < } = table; const { globalFilter, showGlobalFilter } = getState(); - const textFieldProps = - mantineSearchTextInputProps instanceof Function - ? mantineSearchTextInputProps({ table }) - : mantineSearchTextInputProps; + const textFieldProps = funcValue(mantineSearchTextInputProps, { table }); const isMounted = useRef(false); const [searchValue, setSearchValue] = useState(globalFilter ?? ''); @@ -112,9 +111,7 @@ export const MRT_GlobalFilterTextInput = < textFieldProps?.className, )} style={(theme) => ({ - ...(textFieldProps?.style instanceof Function - ? textFieldProps.style(theme) - : (textFieldProps?.style as any)), + ...styleValue(textFieldProps, theme), })} /> diff --git a/packages/mantine-react-table/src/inputs/MRT_SelectCheckbox.tsx b/packages/mantine-react-table/src/inputs/MRT_SelectCheckbox.tsx index 9f0298669..aabb90ffb 100644 --- a/packages/mantine-react-table/src/inputs/MRT_SelectCheckbox.tsx +++ b/packages/mantine-react-table/src/inputs/MRT_SelectCheckbox.tsx @@ -9,6 +9,7 @@ import { type SwitchProps, } from '@mantine/core'; import { type MRT_Row, type MRT_TableInstance } from '../types'; +import { funcValue } from '../funcValue'; interface Props = {}> { row?: MRT_Row; @@ -35,12 +36,8 @@ export const MRT_SelectCheckbox = = {}>({ const { density, isLoading } = getState(); const checkboxProps = !row - ? mantineSelectAllCheckboxProps instanceof Function - ? mantineSelectAllCheckboxProps({ table }) - : mantineSelectAllCheckboxProps - : mantineSelectCheckboxProps instanceof Function - ? mantineSelectCheckboxProps({ row, table }) - : mantineSelectCheckboxProps; + ? funcValue(mantineSelectAllCheckboxProps, { table }) + : funcValue(mantineSelectCheckboxProps, { row, table }); const allRowsSelected = selectAll ? selectAllMode === 'page' diff --git a/packages/mantine-react-table/src/menus/MRT_ColumnActionMenu.tsx b/packages/mantine-react-table/src/menus/MRT_ColumnActionMenu.tsx index 7d07f225f..96c2eef7f 100644 --- a/packages/mantine-react-table/src/menus/MRT_ColumnActionMenu.tsx +++ b/packages/mantine-react-table/src/menus/MRT_ColumnActionMenu.tsx @@ -1,5 +1,6 @@ import { ActionIcon, Menu, Tooltip } from '@mantine/core'; import { type MRT_Header, type MRT_TableInstance } from '../types'; +import { funcValue } from '../funcValue'; import classes from './MRT_ColumnActionMenu.module.css'; @@ -51,22 +52,10 @@ export const MRT_ColumnActionMenu = = {}>({ const { columnDef } = column; const { columnSizing, columnVisibility } = getState(); - const mTableHeadCellColumnActionsButtonProps = - mantineColumnActionsButtonProps instanceof Function - ? mantineColumnActionsButtonProps({ column, table }) - : mantineColumnActionsButtonProps; - - const mcTableHeadCellColumnActionsButtonProps = - columnDef.mantineColumnActionsButtonProps instanceof Function - ? columnDef.mantineColumnActionsButtonProps({ - column, - table, - }) - : columnDef.mantineColumnActionsButtonProps; - + const arg = { column, table }; const actionIconProps = { - ...mTableHeadCellColumnActionsButtonProps, - ...mcTableHeadCellColumnActionsButtonProps, + ...funcValue(mantineColumnActionsButtonProps, arg), + ...funcValue(columnDef.mantineColumnActionsButtonProps, arg), }; const handleClearSort = () => { diff --git a/packages/mantine-react-table/src/menus/MRT_FilterOptionMenu.tsx b/packages/mantine-react-table/src/menus/MRT_FilterOptionMenu.tsx index 111631a4a..7c57cd64a 100644 --- a/packages/mantine-react-table/src/menus/MRT_FilterOptionMenu.tsx +++ b/packages/mantine-react-table/src/menus/MRT_FilterOptionMenu.tsx @@ -246,13 +246,16 @@ export const MRT_FilterOptionMenu = = {}>({ handleSelectFilterMode(option as MRT_FilterOption) } color={option === filterOption ? 'blue' : undefined} - style={{ - '& > .mantine-Menu-itemLabel': { - display: 'flex', - flexWrap: 'nowrap', - gap: '1ch', - }, - }} + style={ + { + // TODO: move to module + // '& > .mantine-Menu-itemLabel': { + // display: 'flex', + // flexWrap: 'nowrap', + // gap: '1ch', + // }, + } + } value={option} > = {}> { open: boolean; @@ -28,19 +29,10 @@ export const MRT_EditRowModal = = {}>({ const { creatingRow, editingRow } = getState(); const row = (creatingRow ?? editingRow) as MRT_Row; - const createModalProps = - mantineCreateRowModalProps instanceof Function - ? mantineCreateRowModalProps({ row, table }) - : mantineCreateRowModalProps; - - const editModalProps = - mantineEditRowModalProps instanceof Function - ? mantineEditRowModalProps({ row, table }) - : mantineEditRowModalProps; - + const arg = { row, table }; const modalProps = { - ...editModalProps, - ...(creatingRow && createModalProps), + ...funcValue(mantineEditRowModalProps, arg), + ...(creatingRow && funcValue(mantineCreateRowModalProps, arg)), }; const internalEditComponents = row diff --git a/packages/mantine-react-table/src/table/MRT_Table.tsx b/packages/mantine-react-table/src/table/MRT_Table.tsx index 73618116f..2ea7e4dc9 100644 --- a/packages/mantine-react-table/src/table/MRT_Table.tsx +++ b/packages/mantine-react-table/src/table/MRT_Table.tsx @@ -10,6 +10,7 @@ import { Memo_MRT_TableBody, MRT_TableBody } from '../body/MRT_TableBody'; import { MRT_TableFooter } from '../footer/MRT_TableFooter'; import { parseCSSVarId } from '../column.utils'; import { type MRT_TableInstance, type MRT_Virtualizer } from '../types'; +import { funcValue, styleValue } from '../funcValue'; interface Props = {}> { table: MRT_TableInstance; @@ -44,15 +45,8 @@ export const MRT_Table = = {}>({ density, } = getState(); - const tableProps = - mantineTableProps instanceof Function - ? mantineTableProps({ table }) - : mantineTableProps; - - const vProps = - columnVirtualizerProps instanceof Function - ? columnVirtualizerProps({ table }) - : columnVirtualizerProps; + const tableProps = funcValue(mantineTableProps, { table }); + const vProps = funcValue(columnVirtualizerProps, { table }); const columnSizeVars = useMemo(() => { const headers = getFlatHeaders(); @@ -158,19 +152,17 @@ export const MRT_Table = = {}>({ layoutMode !== 'grid' && enableColumnResizing ? 'fixed' : undefined, '& tr:first-of-type td': { borderTop: `1px solid ${ - theme.colors.gray[theme.colorScheme === 'dark' ? 8 : 3] + theme.colors.gray[8] // TODO: [theme.colorScheme === 'dark' ? 8 : 3] }`, }, '& tr:last-of-type td': { borderBottom: `1px solid ${ - theme.colors.gray[theme.colorScheme === 'dark' ? 8 : 3] + theme.colors.gray[8] // TODO: [theme.colorScheme === 'dark' ? 8 : 3] }`, }, - ...(tableProps?.style instanceof Function - ? tableProps.style(theme) - : (tableProps?.style as any)), + ...columnSizeVars, + ...styleValue(tableProps, theme), })} - style={{ ...columnSizeVars, ...tableProps?.style }} > {enableTableHead && } {memoMode === 'table-body' || columnSizingInfo.isResizingColumn ? ( diff --git a/packages/mantine-react-table/src/table/MRT_TableContainer.tsx b/packages/mantine-react-table/src/table/MRT_TableContainer.tsx index 7d434401c..3a4c236cb 100644 --- a/packages/mantine-react-table/src/table/MRT_TableContainer.tsx +++ b/packages/mantine-react-table/src/table/MRT_TableContainer.tsx @@ -3,6 +3,7 @@ import { Box, LoadingOverlay } from '@mantine/core'; import { MRT_Table } from './MRT_Table'; import { MRT_EditRowModal } from '../modals'; import { type MRT_TableInstance } from '../types'; +import { funcValue, styleValue } from '../funcValue'; const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect; @@ -35,15 +36,8 @@ export const MRT_TableContainer = = {}>({ const [totalToolbarHeight, setTotalToolbarHeight] = useState(0); - const tableContainerProps = - mantineTableContainerProps instanceof Function - ? mantineTableContainerProps({ table }) - : mantineTableContainerProps; - - const loadingOverlayProps = - mantineLoadingOverlayProps instanceof Function - ? mantineLoadingOverlayProps({ table }) - : mantineLoadingOverlayProps; + const tableContainerProps = funcValue(mantineTableContainerProps, { table }); + const loadingOverlayProps = funcValue(mantineLoadingOverlayProps, { table }); useIsomorphicLayoutEffect(() => { const topToolbarHeight = @@ -78,19 +72,13 @@ export const MRT_TableContainer = = {}>({ maxWidth: '100%', maxHeight: enableStickyHeader ? `clamp(350px, calc(100vh - ${totalToolbarHeight}px), 9999px)` + : isFullScreen + ? `calc(100vh - ${totalToolbarHeight}px)` : undefined, overflow: 'auto', position: 'relative', - ...(tableContainerProps?.style instanceof Function - ? tableContainerProps.style(theme) - : (tableContainerProps?.style as any)), + ...styleValue(tableContainerProps, theme), })} - style={{ - maxHeight: isFullScreen - ? `calc(100vh - ${totalToolbarHeight}px)` - : undefined, - ...tableContainerProps?.style, - }} > = {}> { table: MRT_TableInstance; @@ -24,10 +25,7 @@ export const MRT_TablePaper = = {}>({ } = table; const { isFullScreen } = getState(); - const tablePaperProps = - mantinePaperProps instanceof Function - ? mantinePaperProps({ table }) - : mantinePaperProps; + const tablePaperProps = funcValue(mantinePaperProps, { table }); return ( = {}>({ style={(theme) => ({ overflow: 'hidden', transition: 'all 100ms ease-in-out', - ...(tablePaperProps?.style instanceof Function - ? tablePaperProps?.style(theme) - : (tablePaperProps?.style as any)), - })} - style={{ ...(isFullScreen ? { bottom: 0, @@ -63,19 +56,19 @@ export const MRT_TablePaper = = {}>({ width: '100vw', zIndex: 100, } - : {}), - ...tablePaperProps?.style, - }} + : null), + ...styleValue(tablePaperProps, theme), + })} > {enableTopToolbar && - (renderTopToolbar instanceof Function - ? renderTopToolbar({ table }) - : renderTopToolbar ?? )} + (funcValue(renderTopToolbar, { table }) ?? ( + + ))} {enableBottomToolbar && - (renderBottomToolbar instanceof Function - ? renderBottomToolbar({ table }) - : renderBottomToolbar ?? )} + (funcValue(renderBottomToolbar, { table }) ?? ( + + ))} ); }; diff --git a/packages/mantine-react-table/src/toolbar/MRT_BottomToolbar.tsx b/packages/mantine-react-table/src/toolbar/MRT_BottomToolbar.tsx index f52a765e6..b386b3eea 100644 --- a/packages/mantine-react-table/src/toolbar/MRT_BottomToolbar.tsx +++ b/packages/mantine-react-table/src/toolbar/MRT_BottomToolbar.tsx @@ -6,6 +6,7 @@ import { MRT_ProgressBar } from './MRT_ProgressBar'; import { commonToolbarStyles } from './MRT_TopToolbar'; import { MRT_ToolbarDropZone } from './MRT_ToolbarDropZone'; import { type MRT_TableInstance } from '../types'; +import { funcValue, styleValue } from '../funcValue'; interface Props = {}> { table: MRT_TableInstance; @@ -30,10 +31,7 @@ export const MRT_BottomToolbar = = {}>({ const isMobile = useMediaQuery('(max-width: 720px)'); - const toolbarProps = - mantineBottomToolbarProps instanceof Function - ? mantineBottomToolbarProps({ table }) - : mantineBottomToolbarProps; + const toolbarProps = funcValue(mantineBottomToolbarProps, { table }); const stackAlertBanner = isMobile || !!renderBottomToolbarCustomActions; @@ -55,9 +53,7 @@ export const MRT_BottomToolbar = = {}>({ left: 0, position: isFullScreen ? 'fixed' : 'relative', right: 0, - ...(toolbarProps?.style instanceof Function - ? toolbarProps.style(theme) - : (toolbarProps?.style as any)), + ...(styleValue(toolbarProps, theme) as any), })} > diff --git a/packages/mantine-react-table/src/toolbar/MRT_ProgressBar.tsx b/packages/mantine-react-table/src/toolbar/MRT_ProgressBar.tsx index 54b3efddc..9887920ca 100644 --- a/packages/mantine-react-table/src/toolbar/MRT_ProgressBar.tsx +++ b/packages/mantine-react-table/src/toolbar/MRT_ProgressBar.tsx @@ -1,5 +1,6 @@ import { Collapse, Progress } from '@mantine/core'; import { type MRT_TableInstance } from '../types'; +import { funcValue } from '../funcValue'; interface Props = {}> { isTopToolbar: boolean; @@ -16,10 +17,10 @@ export const MRT_ProgressBar = = {}>({ } = table; const { isSaving, showProgressBars } = getState(); - const linearProgressProps = - mantineProgressProps instanceof Function - ? mantineProgressProps({ isTopToolbar, table }) - : mantineProgressProps; + const linearProgressProps = funcValue(mantineProgressProps, { + isTopToolbar, + table, + }); return ( = {}>({ }} > = {}> { position?: 'top' | 'bottom'; table: MRT_TableInstance; } -const commonActionButtonStyles: Sx = { +const commonActionButtonStyles: MantineStyleProp = { userSelect: 'none', - '&:disabled': { - backgroundColor: 'transparent', - border: 'none', - }, + // TODO: move to module + // '&:disabled': { + // backgroundColor: 'transparent', + // border: 'none', + // }, }; export const MRT_TablePagination = = {}>({ @@ -49,10 +51,7 @@ export const MRT_TablePagination = = {}>({ showGlobalFilter, } = getState(); - const paginationProps = - mantinePaginationProps instanceof Function - ? mantinePaginationProps({ table }) - : mantinePaginationProps; + const paginationProps = funcValue(mantinePaginationProps, { table }); const totalRowCount = rowCount ?? getPrePaginationRowModel().rows.length; const numberOfPages = Math.ceil(totalRowCount / pageSize); @@ -93,17 +92,20 @@ export const MRT_TablePagination = = {}>({ label={localization.rowsPerPage} onChange={(value: string) => setPageSize(+value)} value={pageSize.toString()} - style={{ - '@media (min-width: 720px)': { - display: 'flex', - alignItems: 'center', - gap: '8px', - }, - '& .mantine-Select-input': { - width: '80px', - }, - }} - withinPortal + style={ + { + // TODO: move to module + // '@media (min-width: 720px)': { + // display: 'flex', + // alignItems: 'center', + // gap: '8px', + // }, + // '& .mantine-Select-input': { + // width: '80px', + // }, + } + } + // withinPortal // TODO: doesn't exist anymore. /> )} {paginationDisplayMode === 'pages' ? ( diff --git a/packages/mantine-react-table/src/toolbar/MRT_ToolbarAlertBanner.tsx b/packages/mantine-react-table/src/toolbar/MRT_ToolbarAlertBanner.tsx index acc869d78..a6a1bda49 100644 --- a/packages/mantine-react-table/src/toolbar/MRT_ToolbarAlertBanner.tsx +++ b/packages/mantine-react-table/src/toolbar/MRT_ToolbarAlertBanner.tsx @@ -2,6 +2,7 @@ import { Fragment } from 'react'; import { ActionIcon, Alert, Badge, Collapse, Flex, Stack } from '@mantine/core'; import { type MRT_TableInstance } from '../types'; import { MRT_SelectCheckbox } from '../inputs'; +import { funcValue, styleValue } from '../funcValue'; interface Props = {}> { stackAlertBanner?: boolean; @@ -30,15 +31,8 @@ export const MRT_ToolbarAlertBanner = = {}>({ } = table; const { grouping, showAlertBanner, density } = getState(); - const alertProps = - mantineToolbarAlertBannerProps instanceof Function - ? mantineToolbarAlertBannerProps({ table }) - : mantineToolbarAlertBannerProps; - - const badgeProps = - mantineToolbarAlertBannerBadgeProps instanceof Function - ? mantineToolbarAlertBannerBadgeProps({ table }) - : mantineToolbarAlertBannerBadgeProps; + const alertProps = funcValue(mantineToolbarAlertBannerProps, { table }); + const badgeProps = funcValue(mantineToolbarAlertBannerBadgeProps, { table }); const selectedAlert = getSelectedRowModel().rows.length > 0 @@ -104,9 +98,7 @@ export const MRT_ToolbarAlertBanner = = {}>({ top: 0, width: '100%', zIndex: 2, - ...(alertProps?.style instanceof Function - ? alertProps.style(theme) - : (alertProps?.style as any)), + ...styleValue(alertProps, theme), })} > {renderToolbarAlertBannerContent?.({ diff --git a/packages/mantine-react-table/src/toolbar/MRT_TopToolbar.tsx b/packages/mantine-react-table/src/toolbar/MRT_TopToolbar.tsx index 464136fdf..67f141e91 100644 --- a/packages/mantine-react-table/src/toolbar/MRT_TopToolbar.tsx +++ b/packages/mantine-react-table/src/toolbar/MRT_TopToolbar.tsx @@ -1,3 +1,4 @@ +import { type CSSProperties } from 'react'; import { Box, Flex, type MantineTheme } from '@mantine/core'; import { useMediaQuery } from '@mantine/hooks'; import { MRT_GlobalFilterTextInput } from '../inputs/MRT_GlobalFilterTextInput'; @@ -7,11 +8,16 @@ import { MRT_ToolbarAlertBanner } from './MRT_ToolbarAlertBanner'; import { MRT_ToolbarInternalButtons } from './MRT_ToolbarInternalButtons'; import { MRT_ToolbarDropZone } from './MRT_ToolbarDropZone'; import { type MRT_TableInstance } from '../types'; +import { funcValue, styleValue } from '../funcValue'; -export const commonToolbarStyles = ({ theme }: { theme: MantineTheme }) => ({ +export const commonToolbarStyles = ({ + theme, +}: { + theme: MantineTheme; +}): CSSProperties => ({ alignItems: 'flex-start', - backgroundColor: - theme.colorScheme === 'dark' ? theme.colors.dark[7] : theme.white, + backgroundColor: theme.colors.dark[7], + // TODO: theme.colorScheme === 'dark' ? theme.colors.dark[7] : theme.white, backgroundImage: 'none', display: 'grid', flexWrap: 'wrap-reverse', @@ -49,10 +55,7 @@ export const MRT_TopToolbar = = {}>({ const isMobile = useMediaQuery('(max-width: 720px)'); - const toolbarProps = - mantineTopToolbarProps instanceof Function - ? mantineTopToolbarProps({ table }) - : mantineTopToolbarProps; + const toolbarProps = funcValue(mantineTopToolbarProps, { table }); const stackAlertBanner = isMobile || !!renderTopToolbarCustomActions || showGlobalFilter; @@ -72,9 +75,7 @@ export const MRT_TopToolbar = = {}>({ position: isFullScreen ? 'sticky' : 'relative', top: isFullScreen ? '0' : undefined, ...commonToolbarStyles({ theme }), - ...(toolbarProps?.style instanceof Function - ? toolbarProps.style(theme) - : (toolbarProps?.style as any)), + ...styleValue(toolbarProps, theme), })} > {positionToolbarAlertBanner === 'top' && (