Skip to content

Commit f14f8b5

Browse files
Check styles declaration against MUI Theme with TypeScript (#721)
1 parent 3e85f02 commit f14f8b5

19 files changed

+135
-151
lines changed

src/components/custom-tree-item.tsx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,20 @@
77

88
import { forwardRef, MouseEvent, MouseEventHandler, useCallback, useEffect } from 'react';
99
import { UUID } from 'crypto';
10-
import {
11-
Box,
12-
BoxProps,
13-
IconButton,
14-
PopoverReference,
15-
SxProps,
16-
Theme,
17-
Typography,
18-
TypographyProps,
19-
} from '@mui/material';
10+
import { Box, BoxProps, IconButton, PopoverReference, Typography, TypographyProps } from '@mui/material';
2011
import { Add as AddIcon, AddBoxOutlined as AddBoxOutlinedIcon } from '@mui/icons-material';
2112
import { TreeItem, TreeItemContentProps, TreeItemProps, useTreeItemState } from '@mui/x-tree-view';
22-
import { mergeSx, useStateBoolean } from '@gridsuite/commons-ui';
13+
import { mergeSx, type SxStyle, useStateBoolean } from '@gridsuite/commons-ui';
2314
import { useSelector } from 'react-redux';
2415
import { AppState } from '../redux/types';
2516

2617
export interface TreeItemCustomContentProps {
2718
styles?: {
28-
root?: SxProps<Theme>;
29-
selected?: SxProps<Theme>;
30-
hovered?: SxProps<Theme>;
31-
label?: SxProps<Theme>;
32-
iconContainer?: SxProps<Theme>;
19+
root?: SxStyle;
20+
selected?: SxStyle;
21+
hovered?: SxStyle;
22+
label?: SxStyle;
23+
iconContainer?: SxStyle;
3324
};
3425
onExpand: (itemId: UUID) => void;
3526
onSelect: (itemId: UUID) => void;

src/components/dialogs/delete-dialog.tsx

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import {
1414
DialogTitle,
1515
Grid,
1616
} from '@mui/material';
17-
import { FormattedMessage, useIntl } from 'react-intl';
18-
import { SyntheticEvent, useEffect, useRef, useState } from 'react';
19-
import { CancelButton, ElementAttributes, OverflowableText } from '@gridsuite/commons-ui';
17+
import { FormattedMessage } from 'react-intl';
18+
import { type CSSProperties, type SyntheticEvent, useEffect, useRef, useState } from 'react';
19+
import { CancelButton, type ElementAttributes, type MuiStyles, OverflowableText } from '@gridsuite/commons-ui';
2020

2121
export interface DeleteDialogProps {
2222
open: boolean;
@@ -32,7 +32,7 @@ const styles = {
3232
tooltip: {
3333
maxWidth: '1000px',
3434
},
35-
};
35+
} as const satisfies MuiStyles;
3636

3737
/**
3838
* Dialog to delete an element
@@ -53,8 +53,6 @@ export default function DeleteDialog({
5353
simpleDeleteFormatMessageId,
5454
error,
5555
}: Readonly<DeleteDialogProps>) {
56-
const intl = useIntl();
57-
5856
const [itemsState, setItemsState] = useState<ElementAttributes[]>([]);
5957

6058
const [loadingState, setLoadingState] = useState(false);
@@ -82,20 +80,20 @@ export default function DeleteDialog({
8280
onClick();
8381
};
8482

85-
const buildTitle = () => intl.formatMessage({ id: 'deleteDialogTitle' });
86-
8783
const renderElement = (renderItems: ElementAttributes[]) => {
8884
const isBig = renderItems[0].elementName?.length > 72;
8985

90-
const style = isBig
91-
? { width: '100%', fontWeight: 'bold' }
92-
: {
93-
fontWeight: 'bold',
94-
marginLeft: 'initial',
95-
marginRight: 'initial',
96-
verticalAlign: 'middle',
97-
display: 'inline-block',
98-
};
86+
const style = (
87+
isBig
88+
? ({ width: '100%', fontWeight: 'bold' } as const)
89+
: ({
90+
fontWeight: 'bold',
91+
marginLeft: 'initial',
92+
marginRight: 'initial',
93+
verticalAlign: 'middle',
94+
display: 'inline-block',
95+
} as const)
96+
) satisfies CSSProperties;
9997
return <OverflowableText text={renderItems[0].elementName} style={style} tooltipSx={styles.tooltip} />;
10098
};
10199

@@ -108,34 +106,25 @@ export default function DeleteDialog({
108106
(gridItems.length > 1 ? (
109107
<Grid>
110108
<Grid item>
111-
<span>
112-
{intl.formatMessage({
113-
id: gridMultipleDeleteFormatMessageId,
114-
})}
115-
</span>
109+
<FormattedMessage tagName="span" id={gridMultipleDeleteFormatMessageId} />
116110
</Grid>
117111
</Grid>
118112
) : (
119113
<Grid>
120114
<Grid item>
121-
<span>
122-
{intl.formatMessage(
123-
{
124-
id: gridSimpleDeleteFormatMessageId,
125-
},
126-
{
127-
itemName: <span>{gridItems.length === 1 && renderElement(gridItems)}</span>,
128-
}
129-
)}
130-
</span>
115+
<FormattedMessage
116+
tagName="span"
117+
id={gridSimpleDeleteFormatMessageId}
118+
values={{ itemName: <span>{gridItems.length === 1 && renderElement(gridItems)}</span> }}
119+
/>
131120
</Grid>
132121
</Grid>
133122
));
134123

135124
return (
136125
<Dialog open={open} onClose={handleClose} aria-labelledby="dialog-title-delete">
137126
<DialogTitle style={{ display: 'flex' }} data-testid="DialogTitle">
138-
{buildTitle()}
127+
<FormattedMessage id="deleteDialogTitle" />
139128
</DialogTitle>
140129
<DialogContent>
141130
{buildItemsToDeleteGrid(itemsState, multipleDeleteFormatMessageId, simpleDeleteFormatMessageId)}

src/components/dialogs/field-hook.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ChangeEvent, ReactNode, useCallback, useEffect, useMemo, useState } fro
99
import { FormattedMessage, useIntl } from 'react-intl';
1010
import { CircularProgress, InputAdornment, TextField, TextFieldProps } from '@mui/material';
1111
import { Check as CheckIcon } from '@mui/icons-material';
12-
import { ElementType, useDebounce } from '@gridsuite/commons-ui';
12+
import { ElementType, type MuiStyles, useDebounce } from '@gridsuite/commons-ui';
1313
import { UUID } from 'crypto';
1414
import { elementExists, rootDirectoryExists } from '../../utils/rest-api';
1515

@@ -18,7 +18,7 @@ const styles = {
1818
margin: 0,
1919
marginTop: 4,
2020
},
21-
};
21+
} as const satisfies MuiStyles;
2222

2323
export interface UseTextValueProps extends Omit<TextFieldProps, 'label' | 'defaultValue'> {
2424
label: string;

src/components/directory-breadcrumbs.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,38 @@
66
*/
77

88
import { useDispatch, useSelector } from 'react-redux';
9-
import { Box, Breadcrumbs, emphasize, Link, SxProps, Theme, Tooltip, Typography } from '@mui/material';
9+
import { Box, Breadcrumbs, emphasize, Link, Tooltip, Typography } from '@mui/material';
1010
import { FolderOpen as FolderOpenIcon } from '@mui/icons-material';
11-
import { ElementAttributes, mergeSx } from '@gridsuite/commons-ui';
11+
import { type ElementAttributes, mergeSx, type MuiStyles } from '@gridsuite/commons-ui';
1212
import { MouseEvent } from 'react';
1313
import { setSelectedDirectory } from '../redux/actions';
1414
import { AppState } from '../redux/types';
1515

1616
const styles = {
17-
link: (theme: Theme) => ({
17+
link: (theme) => ({
1818
display: 'inline-grid',
1919
alignItems: 'center',
2020
textAlign: 'center',
2121
color: theme.link.color,
2222

23-
backgroundColor: theme.row.primary,
23+
backgroundColor: theme.row.primary as string,
2424
padding: theme.spacing(0.5, 2, 0.5),
2525
borderRadius: theme.spacing(2),
2626

2727
'&:hover, &:focus': {
28-
backgroundColor: theme.row.hover,
28+
backgroundColor: theme.row.hover as string,
2929
textDecoration: 'none',
3030
},
3131
'&:active': {
3232
backgroundColor: emphasize(theme.row.hover as string, 0.15),
3333
},
3434
}),
35-
directory: (theme: Theme) => ({
35+
directory: (theme) => ({
3636
display: 'inline-grid',
3737
alignItems: 'center',
3838
textAlign: 'center',
3939

40-
backgroundColor: theme.row.hover,
40+
backgroundColor: theme.row.hover as string,
4141
padding: theme.spacing(0.5, 2, 0.5),
4242
borderRadius: theme.spacing(2),
4343

@@ -46,14 +46,14 @@ const styles = {
4646
selectedLabel: {
4747
fontWeight: 'bold',
4848
},
49-
icon: (theme: Theme) => ({
49+
icon: (theme) => ({
5050
marginRight: theme.spacing(1),
5151
width: theme.spacing(2.25),
5252
height: theme.spacing(2.25),
5353
position: 'relative',
5454
top: theme.spacing(0.5),
5555
}),
56-
breadcrumbs: (theme: Theme) => ({
56+
breadcrumbs: (theme) => ({
5757
padding: theme.spacing(0.5, 0, 0.5),
5858
marginLeft: theme.spacing(1),
5959
}),
@@ -62,7 +62,7 @@ const styles = {
6262
whiteSpace: 'nowrap',
6363
overflow: 'hidden',
6464
},
65-
};
65+
} as const satisfies MuiStyles;
6666

6767
export default function DirectoryBreadcrumbs() {
6868
const dispatch = useDispatch();
@@ -81,7 +81,7 @@ export default function DirectoryBreadcrumbs() {
8181
if (selectedDirectory !== null && currentPath !== null && currentPath.length > 1) {
8282
return currentPath.slice(0, currentPath.length - 1).map((dir, index) => (
8383
<Link
84-
sx={styles.link as SxProps}
84+
sx={styles.link}
8585
key={dir.elementUuid}
8686
href="/"
8787
onClick={(event: MouseEvent<HTMLElement>) => handleSelect(event, dir)}
@@ -106,7 +106,7 @@ export default function DirectoryBreadcrumbs() {
106106
if (selectedDirectory !== null && currentPath !== null && currentPath.length > 0) {
107107
return (
108108
<Tooltip title={currentPath[currentPath.length - 1].elementName}>
109-
<Box sx={styles.directory as SxProps}>
109+
<Box sx={styles.directory}>
110110
<Typography sx={mergeSx(styles.selectedLabel, styles.limitTextSize)} color="textPrimary">
111111
{currentPath.length === 1 && <FolderOpenIcon sx={styles.icon} />}
112112
{currentPath[currentPath.length - 1].elementName}

src/components/directory-content-table.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77

88
import { CustomAGGrid, ElementAttributes, ElementType } from '@gridsuite/commons-ui';
99
import { AgGridReact, AgGridReactProps } from 'ag-grid-react';
10-
import {
10+
import type {
1111
AgGridEvent,
1212
CellClickedEvent,
1313
CellContextMenuEvent,
1414
ColDef,
1515
GetRowIdParams,
1616
RowClassParams,
17+
RowStyle,
1718
} from 'ag-grid-community';
1819
import { RefObject, useCallback, useEffect, useState } from 'react';
1920
import { useDispatch, useSelector } from 'react-redux';
@@ -40,7 +41,7 @@ const recomputeOverFlowableCells = ({ api }: AgGridEvent) =>
4041
export const CUSTOM_ROW_CLASS = 'custom-row-class';
4142

4243
const getClickableRowStyle = (cellData: RowClassParams<ElementAttributes>) => {
43-
const style: Record<string, string> = { fontSize: '1rem' };
44+
const style: RowStyle = { fontSize: '1rem' };
4445
if (
4546
cellData.data &&
4647
![

src/components/directory-content.tsx

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
import { type MouseEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react';
99
import { useDispatch, useSelector } from 'react-redux';
1010
import { FormattedMessage, useIntl } from 'react-intl';
11+
import { Box, type BoxProps, Button, type ButtonProps, CircularProgress } from '@mui/material';
1112
import {
12-
Box,
13-
type BoxProps,
14-
Button,
15-
type ButtonProps,
16-
CircularProgress,
17-
type SxProps,
18-
type Theme,
19-
} from '@mui/material';
20-
import { type ElementAttributes, type ItemSelectionForCopy, NO_ITEM_SELECTION_FOR_COPY } from '@gridsuite/commons-ui';
13+
type ElementAttributes,
14+
type ItemSelectionForCopy,
15+
type MuiStyles,
16+
NO_ITEM_SELECTION_FOR_COPY,
17+
} from '@gridsuite/commons-ui';
2118
import { Add as AddIcon } from '@mui/icons-material';
2219
import { AgGridReact } from 'ag-grid-react';
2320
import * as constants from '../utils/UIconstants';
@@ -43,7 +40,7 @@ import { AnchorStatesType, defaultAnchorStates } from './menus/anchor-utils';
4340
const circularProgressSize = '70px';
4441

4542
const styles = {
46-
link: (theme: Theme) => ({
43+
link: (theme) => ({
4744
color: theme.link.color,
4845
textDecoration: 'none',
4946
}),
@@ -59,17 +56,17 @@ const styles = {
5956
centeredCircularProgress: {
6057
alignSelf: 'center',
6158
},
62-
highlightedElementAnimation: (theme: Theme) => ({
59+
highlightedElementAnimation: (theme) => ({
6360
'@keyframes highlighted-element': {
6461
'from, 24%': {
6562
backgroundColor: 'inherit',
6663
},
6764
'12%, 36%, to': {
68-
backgroundColor: theme.row.hover,
65+
backgroundColor: theme.row.hover as string,
6966
},
7067
},
7168
}),
72-
button: (theme: Theme) => ({
69+
button: (theme) => ({
7370
marginRight: theme.spacing(9),
7471
borderRadius: '20px',
7572
}),
@@ -79,12 +76,12 @@ const styles = {
7976
justifyContent: 'space-between',
8077
alignItems: 'center',
8178
},
82-
};
79+
} as const satisfies MuiStyles;
8380

8481
export default function DirectoryContent() {
8582
const treeData = useSelector((state: AppState) => state.treeData);
8683
const dispatch = useDispatch();
87-
const gridRef = useRef<AgGridReact | null>(null);
84+
const gridRef = useRef<AgGridReact<ElementAttributes> | null>(null);
8885
const [onGridReady, getRowStyle] = useHighlightSearchedElement(gridRef?.current?.api ?? null);
8986

9087
const [broadcastChannel] = useState(() => {
@@ -285,7 +282,7 @@ export default function DirectoryContent() {
285282
flexGrow={1}
286283
minHeight={0}
287284
overflow="auto"
288-
sx={styles.highlightedElementAnimation as SxProps}
285+
sx={styles.highlightedElementAnimation}
289286
onContextMenu={onContextMenu}
290287
data-testid="DirectoryContent"
291288
>

src/components/directory-tree-view.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
import { MouseEvent as ReactMouseEvent, useCallback, useEffect, useRef, useState } from 'react';
99
import { ChevronRight as ChevronRightIcon, ExpandMore as ExpandMoreIcon } from '@mui/icons-material';
10-
import { Box, PopoverReference, SxProps, Theme, Tooltip, Typography, Zoom } from '@mui/material';
10+
import { Box, type PopoverReference, Tooltip, Typography, Zoom } from '@mui/material';
1111
import { useDispatch, useSelector } from 'react-redux';
1212
import { SimpleTreeView } from '@mui/x-tree-view';
13-
import { ElementAttributes } from '@gridsuite/commons-ui';
13+
import { type ElementAttributes, type MuiStyles } from '@gridsuite/commons-ui';
1414
import { UUID } from 'crypto';
1515
// eslint-disable-next-line import/no-extraneous-dependencies -- lib from MUI
1616
import CustomTreeItem from './custom-tree-item';
@@ -71,7 +71,7 @@ const styles = {
7171
width: '18px',
7272
height: '18px',
7373
}),
74-
} satisfies Record<string, SxProps<Theme>>;
74+
} as const satisfies MuiStyles;
7575

7676
function CustomEndIcon() {
7777
return <ChevronRightIcon sx={styles.icon} />;

0 commit comments

Comments
 (0)