-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathGridColumnHeaderTitle.tsx
98 lines (85 loc) · 3.01 KB
/
GridColumnHeaderTitle.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import { styled } from '@mui/system';
import { isOverflown } from '../../utils/domUtils';
import { getDataGridUtilityClass } from '../../constants/gridClasses';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
import { DataGridProcessedProps } from '../../models/props/DataGridProps';
type OwnerState = DataGridProcessedProps;
const useUtilityClasses = (ownerState: OwnerState) => {
const { classes } = ownerState;
const slots = {
root: ['columnHeaderTitle'],
};
return composeClasses(slots, getDataGridUtilityClass, classes);
};
const GridColumnHeaderTitleRoot = styled('div', {
name: 'MuiDataGrid',
slot: 'ColumnHeaderTitle',
overridesResolver: (props, styles) => styles.columnHeaderTitle,
})<{ ownerState: OwnerState }>({
textOverflow: 'ellipsis',
overflow: 'hidden',
whiteSpace: 'nowrap',
fontWeight: 'var(--unstable_DataGrid-headWeight)',
});
const ColumnHeaderInnerTitle = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(function ColumnHeaderInnerTitle(props, ref) {
const { className, ...other } = props;
const rootProps = useGridRootProps();
const classes = useUtilityClasses(rootProps);
return (
<GridColumnHeaderTitleRoot
ref={ref}
className={clsx(classes.root, className)}
ownerState={rootProps}
{...other}
/>
);
});
export interface GridColumnHeaderTitleProps {
label: string;
columnWidth: number;
description?: React.ReactNode;
}
// No React.memo here as if we display the sort icon, we need to recalculate the isOver
function GridColumnHeaderTitle(props: GridColumnHeaderTitleProps) {
const { label, description } = props;
const rootProps = useGridRootProps();
const titleRef = React.useRef<HTMLDivElement>(null);
const [tooltip, setTooltip] = React.useState('');
const handleMouseOver = React.useCallback<React.MouseEventHandler<HTMLDivElement>>(() => {
if (!description && titleRef?.current) {
const isOver = isOverflown(titleRef.current);
if (isOver) {
setTooltip(label);
} else {
setTooltip('');
}
}
}, [description, label]);
return (
<rootProps.slots.baseTooltip
title={description || tooltip}
{...rootProps.slotProps?.baseTooltip}
>
<ColumnHeaderInnerTitle onMouseOver={handleMouseOver} ref={titleRef}>
{label}
</ColumnHeaderInnerTitle>
</rootProps.slots.baseTooltip>
);
}
GridColumnHeaderTitle.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "pnpm proptypes" |
// ----------------------------------------------------------------------
columnWidth: PropTypes.number.isRequired,
description: PropTypes.node,
label: PropTypes.string.isRequired,
} as any;
export { GridColumnHeaderTitle };