|
5 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
6 | 6 | */
|
7 | 7 |
|
8 |
| -import React, { useCallback } from 'react'; |
9 |
| -import { AgGridReact, AgGridReactProps } from 'ag-grid-react'; |
10 |
| -import { useIntl } from 'react-intl'; |
| 8 | +import { forwardRef, useMemo } from 'react'; |
| 9 | +import { AgGridReact, type AgGridReactProps } from 'ag-grid-react'; |
11 | 10 | import 'ag-grid-community/styles/ag-grid.css';
|
12 | 11 | import 'ag-grid-community/styles/ag-theme-alpine.css';
|
13 |
| -import { ColumnResizedEvent, GetLocaleTextParams } from 'ag-grid-community'; |
14 |
| -import { Box, type SxProps, type Theme, useTheme } from '@mui/material'; |
| 12 | +import type { ColumnResizedEvent } from 'ag-grid-community'; |
| 13 | +import { AG_GRID_LOCALE_EN, AG_GRID_LOCALE_FR } from '@ag-grid-community/locale'; |
| 14 | +import { useIntl } from 'react-intl'; |
| 15 | +import { Box, type BoxProps, type Theme, useTheme } from '@mui/material'; |
15 | 16 | import { mergeSx } from '../../utils/styles';
|
16 | 17 | import { CUSTOM_AGGRID_THEME, styles } from './customAggrid.style';
|
| 18 | +import { type GsLangUser, LANG_ENGLISH, LANG_FRENCH } from '../../utils/langs'; |
| 19 | + |
| 20 | +export type AgGridLocale = Partial<Record<keyof typeof AG_GRID_LOCALE_EN, string>>; // using EN for keyof because it's the only who has more keys, so more complete |
| 21 | +export type AgGridLocales = Record<GsLangUser, AgGridLocale>; |
17 | 22 |
|
18 |
| -interface CustomAGGGridStyleProps { |
19 |
| - shouldHidePinnedHeaderRightBorder?: boolean; |
| 23 | +function useAgGridLocale(overrideLocales?: AgGridLocales) { |
| 24 | + const intl = useIntl(); |
| 25 | + return useMemo((): Record<string, string> => { |
| 26 | + switch ((intl.locale || intl.defaultLocale).toLowerCase().substring(0, 2)) { |
| 27 | + case LANG_FRENCH: |
| 28 | + return { |
| 29 | + ...AG_GRID_LOCALE_FR, |
| 30 | + thousandSeparator: ' ', |
| 31 | + decimalSeparator: ',', |
| 32 | + ...overrideLocales?.[LANG_FRENCH], |
| 33 | + }; |
| 34 | + case LANG_ENGLISH: |
| 35 | + default: |
| 36 | + return { ...AG_GRID_LOCALE_EN, ...overrideLocales?.[LANG_ENGLISH] }; |
| 37 | + } |
| 38 | + }, [intl.defaultLocale, intl.locale, overrideLocales]); |
20 | 39 | }
|
21 | 40 |
|
22 |
| -export interface CustomAGGridProps extends AgGridReactProps, CustomAGGGridStyleProps {} |
| 41 | +export type CustomAGGridProps<TData = any> = Omit<AgGridReactProps<TData>, 'localeText' | 'getLocaleText'> & |
| 42 | + Pick<BoxProps, 'sx'> & { |
| 43 | + overrideLocales?: AgGridLocales; |
| 44 | + }; |
23 | 45 |
|
24 | 46 | // We have to define a minWidth to column to activate this feature
|
25 |
| -const onColumnResized = (params: ColumnResizedEvent) => { |
26 |
| - const { column, finished } = params; |
27 |
| - const colDefinedMinWidth = column?.getColDef()?.minWidth; |
28 |
| - if (column && colDefinedMinWidth && finished) { |
29 |
| - const newWidth = column?.getActualWidth(); |
30 |
| - if (newWidth < colDefinedMinWidth) { |
31 |
| - params.api.setColumnWidths([{ key: column, newWidth: colDefinedMinWidth }], finished, params.source); |
| 47 | +function onColumnResized({ api, column, finished, source }: ColumnResizedEvent) { |
| 48 | + if (column) { |
| 49 | + const colDefinedMinWidth = column.getColDef().minWidth; |
| 50 | + if (colDefinedMinWidth && finished && column.getActualWidth() < colDefinedMinWidth) { |
| 51 | + api.setColumnWidths([{ key: column, newWidth: colDefinedMinWidth }], finished, source); |
32 | 52 | }
|
33 | 53 | }
|
34 |
| -}; |
35 |
| - |
36 |
| -export const CustomAGGrid = React.forwardRef<AgGridReact, CustomAGGridProps>((props, ref) => { |
37 |
| - const { shouldHidePinnedHeaderRightBorder = false, ...agGridReactProps } = props; |
38 |
| - const theme = useTheme<Theme>(); |
39 |
| - const intl = useIntl(); |
40 |
| - |
41 |
| - const GRID_PREFIX = 'grid.'; |
| 54 | +} |
42 | 55 |
|
43 |
| - const getLocaleText = useCallback( |
44 |
| - (params: GetLocaleTextParams) => { |
45 |
| - const key = GRID_PREFIX + params.key; |
46 |
| - return intl.formatMessage({ |
47 |
| - id: key, |
48 |
| - defaultMessage: params.defaultValue, |
49 |
| - }); |
50 |
| - }, |
51 |
| - [intl] |
52 |
| - ); |
| 56 | +export const CustomAGGrid = forwardRef<AgGridReact, CustomAGGridProps>( |
| 57 | + ({ overrideLocales, sx, ...agGridReactProps }, ref) => { |
| 58 | + const theme = useTheme<Theme>(); |
53 | 59 |
|
54 |
| - return ( |
55 |
| - <Box |
56 |
| - sx={mergeSx( |
57 |
| - styles.grid as SxProps | undefined, |
58 |
| - shouldHidePinnedHeaderRightBorder ? styles.noBorderRight : undefined |
59 |
| - )} |
60 |
| - className={`${theme.aggrid.theme} ${CUSTOM_AGGRID_THEME}`} |
61 |
| - > |
62 |
| - <AgGridReact |
63 |
| - ref={ref} |
64 |
| - getLocaleText={getLocaleText} |
65 |
| - onColumnResized={onColumnResized} |
66 |
| - enableCellTextSelection |
67 |
| - theme="legacy" |
68 |
| - {...agGridReactProps} |
69 |
| - /> |
70 |
| - </Box> |
71 |
| - ); |
72 |
| -}); |
| 60 | + return ( |
| 61 | + <Box |
| 62 | + component="div" |
| 63 | + sx={mergeSx(styles.grid, sx)} |
| 64 | + className={`${theme.aggrid.theme} ${CUSTOM_AGGRID_THEME}`} |
| 65 | + > |
| 66 | + <AgGridReact |
| 67 | + ref={ref} |
| 68 | + localeText={useAgGridLocale(overrideLocales)} |
| 69 | + onColumnResized={onColumnResized} |
| 70 | + enableCellTextSelection |
| 71 | + theme="legacy" |
| 72 | + {...agGridReactProps} |
| 73 | + /> |
| 74 | + </Box> |
| 75 | + ); |
| 76 | + } |
| 77 | +); |
0 commit comments