Skip to content

Commit 40316ef

Browse files
authored
Clean up custom-aggrid-header (#2434)
authored-by: Hugo Marcellin <[email protected]>
1 parent 6c3832f commit 40316ef

30 files changed

+1513
-958
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
import React, { FunctionComponent, SyntheticEvent, useMemo } from 'react';
8+
import { Autocomplete, TextField } from '@mui/material';
9+
import { useIntl } from 'react-intl';
10+
import { useCustomAggridFilter } from '../hooks/use-custom-aggrid-filter';
11+
import { isStringOrNonEmptyArray } from '../custom-aggrid-header-utils';
12+
import { CustomAggridFilterParams, FILTER_TEXT_COMPARATORS, FilterEnumsType } from '../custom-aggrid-header.type';
13+
14+
export interface CustomAggridAutocompleteFilterParams extends CustomAggridFilterParams {
15+
filterEnums?: FilterEnumsType;
16+
getEnumLabel?: (value: string) => string; // Used for translation of enum values in the filter
17+
}
18+
19+
export const CustomAggridAutocompleteFilter: FunctionComponent<CustomAggridAutocompleteFilterParams> = ({
20+
field,
21+
filterParams,
22+
filterEnums,
23+
getEnumLabel,
24+
}) => {
25+
const intl = useIntl();
26+
const { selectedFilterData, handleChangeFilterValue } = useCustomAggridFilter(field, filterParams);
27+
28+
const handleFilterAutoCompleteChange = (_: SyntheticEvent, data: string[]) => {
29+
handleChangeFilterValue({ value: data, type: FILTER_TEXT_COMPARATORS.EQUALS });
30+
};
31+
32+
const filterOption = useMemo(() => filterEnums?.[field] ?? [], [field, filterEnums]);
33+
34+
return (
35+
<Autocomplete
36+
multiple
37+
value={Array.isArray(selectedFilterData) ? selectedFilterData : []}
38+
options={filterOption}
39+
getOptionLabel={getEnumLabel}
40+
onChange={handleFilterAutoCompleteChange}
41+
size="small"
42+
disableCloseOnSelect
43+
renderInput={(params) => (
44+
<TextField
45+
{...params}
46+
placeholder={
47+
!isStringOrNonEmptyArray(selectedFilterData)
48+
? intl.formatMessage({
49+
id: 'filter.filterOoo',
50+
})
51+
: ''
52+
}
53+
/>
54+
)}
55+
fullWidth
56+
/>
57+
);
58+
};

src/components/custom-aggrid/custom-aggrid-filters/custom-aggrid-boolean-filter.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import ClearIcon from '@mui/icons-material/Clear';
1111
import { useIntl } from 'react-intl';
1212
import { SelectChangeEvent } from '@mui/material/Select/SelectInput';
1313
import { mergeSx } from 'components/utils/functions';
14+
import { useCustomAggridFilter } from '../hooks/use-custom-aggrid-filter';
15+
import { isStringOrNonEmptyArray } from '../custom-aggrid-header-utils';
16+
import { CustomAggridFilterParams, FILTER_TEXT_COMPARATORS } from '../custom-aggrid-header.type';
1417

1518
export enum BooleanFilterValue {
1619
TRUE = 'true',
@@ -26,33 +29,36 @@ const styles = {
2629
},
2730
};
2831

29-
interface ICustomAggridBooleanFilter {
30-
value: string;
31-
onChange: (value: string) => void;
32-
}
33-
34-
const CustomAggridBooleanFilter: FunctionComponent<ICustomAggridBooleanFilter> = ({ value, onChange }) => {
32+
export const CustomAggridBooleanFilter: FunctionComponent<CustomAggridFilterParams> = ({ field, filterParams }) => {
3533
const intl = useIntl();
3634

35+
const { selectedFilterData, handleChangeFilterValue } = useCustomAggridFilter(field, filterParams);
36+
3737
const handleValueChange = (event: SelectChangeEvent) => {
3838
const newValue = event.target.value;
39-
onChange && onChange(newValue);
39+
handleChangeFilterValue({ value: newValue, type: FILTER_TEXT_COMPARATORS.EQUALS });
40+
};
41+
42+
const handleClearFilter = () => {
43+
handleChangeFilterValue({
44+
value: undefined,
45+
});
4046
};
4147

4248
return (
4349
<Select
4450
fullWidth
4551
size={'small'}
46-
value={value || ''}
52+
value={typeof selectedFilterData === 'string' ? selectedFilterData : ''}
4753
onChange={handleValueChange}
4854
sx={mergeSx(styles.input, {
4955
'& .MuiSelect-iconOutlined': {
50-
display: value ? 'none' : '',
56+
display: selectedFilterData ? 'none' : '',
5157
},
5258
})}
5359
endAdornment={
54-
value && (
55-
<IconButton onClick={() => onChange('')}>
60+
isStringOrNonEmptyArray(selectedFilterData) && (
61+
<IconButton onClick={handleClearFilter}>
5662
<ClearIcon fontSize={'small'} />
5763
</IconButton>
5864
)
@@ -68,5 +74,3 @@ const CustomAggridBooleanFilter: FunctionComponent<ICustomAggridBooleanFilter> =
6874
</Select>
6975
);
7076
};
71-
72-
export default CustomAggridBooleanFilter;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
import { CustomAggridComparatorSelector } from './custom-aggrid-comparator-selector';
8+
import { CustomAggridTextFilter } from './custom-aggrid-text-filter';
9+
import { Grid } from '@mui/material';
10+
import { useCustomAggridComparatorFilter } from '../hooks/use-custom-aggrid-comparator-filter';
11+
import { CustomAggridFilterParams } from '../custom-aggrid-header.type';
12+
13+
export const CustomAggridComparatorFilter = ({ field, filterParams }: CustomAggridFilterParams) => {
14+
const {
15+
selectedFilterData,
16+
selectedFilterComparator,
17+
decimalAfterDot,
18+
isNumberInput,
19+
handleFilterComparatorChange,
20+
handleFilterTextChange,
21+
handleClearFilter,
22+
} = useCustomAggridComparatorFilter(field, filterParams);
23+
24+
const {
25+
filterComparators = [], // used for text filter as a UI type (examples: contains, startsWith..)
26+
} = filterParams;
27+
28+
return (
29+
<Grid container direction={'column'} gap={0.8} sx={{ padding: '8px' }}>
30+
<CustomAggridComparatorSelector
31+
value={selectedFilterComparator}
32+
onChange={handleFilterComparatorChange}
33+
options={filterComparators}
34+
/>
35+
<CustomAggridTextFilter
36+
value={selectedFilterData}
37+
onChange={handleFilterTextChange}
38+
onClear={handleClearFilter}
39+
isNumberInput={isNumberInput}
40+
decimalAfterDot={decimalAfterDot}
41+
/>
42+
</Grid>
43+
);
44+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
import React from 'react';
8+
import { Select, MenuItem } from '@mui/material';
9+
import { SelectChangeEvent } from '@mui/material/Select/SelectInput';
10+
import { useIntl } from 'react-intl';
11+
12+
const styles = {
13+
input: {
14+
minWidth: '250px',
15+
maxWidth: '40%',
16+
},
17+
};
18+
19+
interface CustomAggridComparatorSelectorProps {
20+
value: string;
21+
onChange: (event: SelectChangeEvent) => void;
22+
options: string[];
23+
}
24+
25+
export const CustomAggridComparatorSelector: React.FC<CustomAggridComparatorSelectorProps> = ({
26+
value,
27+
onChange,
28+
options,
29+
}) => {
30+
const intl = useIntl();
31+
32+
return (
33+
<Select value={value} onChange={onChange} displayEmpty size={'small'} sx={styles.input}>
34+
{options.map((filterComparator) => (
35+
<MenuItem key={filterComparator} value={filterComparator}>
36+
{intl.formatMessage({
37+
id: `filter.${filterComparator}`,
38+
})}
39+
</MenuItem>
40+
))}
41+
</Select>
42+
);
43+
};

0 commit comments

Comments
 (0)