Skip to content

Commit 9d2d4df

Browse files
authored
Add possibility to associate a color by type and HelperText in DirectoryItemsInput (#839)
Signed-off-by: basseche <[email protected]>
1 parent 83ceb6a commit 9d2d4df

File tree

8 files changed

+95
-24
lines changed

8 files changed

+95
-24
lines changed

src/components/customAGGrid/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77

88
export * from './customAggrid.style';
99
export * from './customAggrid';
10+
export * from './separatorCellRenderer';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2025, 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+
8+
import { Theme, Typography } from '@mui/material';
9+
10+
const styles = {
11+
separator: (theme: Theme) => ({
12+
fontWeight: 'bold',
13+
fontSize: '1rem',
14+
width: '100%',
15+
marginTop: theme.spacing(1),
16+
}),
17+
};
18+
19+
type SeparatorCellRendererProps = {
20+
value: string;
21+
};
22+
23+
export function SeparatorCellRenderer({ value }: Readonly<SeparatorCellRendererProps>) {
24+
return (
25+
<Typography variant="subtitle1" color="primary" sx={styles.separator}>
26+
{value}
27+
</Typography>
28+
);
29+
}

src/components/filter/expert/expertFilterUtils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
RuleGroupTypeExport,
3030
RuleTypeExport,
3131
} from './expertFilter.type';
32-
import { FIELDS_OPTIONS, OPERATOR_OPTIONS, RULES } from './expertFilterConstants';
32+
import { EXPERT_FILTER_EQUIPMENTS, FIELDS_OPTIONS, OPERATOR_OPTIONS, RULES } from './expertFilterConstants';
3333
import { convertInputValue, convertOutputValue, isBlankOrEmpty } from '../../../utils/conversionUtils';
3434
import { FieldType } from '../../../utils/types/fieldType';
3535

@@ -524,3 +524,10 @@ export function recursiveRemove(query: RuleGroupTypeAny, path: number[]): RuleGr
524524

525525
return remove(query, path);
526526
}
527+
528+
export function getFilterEquipmentTypeLabel(equipmentType: string | undefined): string {
529+
if (!equipmentType) {
530+
return '';
531+
}
532+
return EXPERT_FILTER_EQUIPMENTS[equipmentType as keyof typeof EXPERT_FILTER_EQUIPMENTS]?.label ?? '';
533+
}

src/components/inputs/reactHookForm/DirectoryItemsInput.tsx

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import { Chip, FormControl, Grid, IconButton, Tooltip } from '@mui/material';
8+
import { Box, Chip, FormControl, FormHelperText, Grid, IconButton, Tooltip } from '@mui/material';
99
import { Folder as FolderIcon } from '@mui/icons-material';
1010
import { useCallback, useMemo, useState } from 'react';
1111
import { FieldValues, useController, useFieldArray } from 'react-hook-form';
12-
import { useIntl } from 'react-intl';
12+
import { FormattedMessage, useIntl } from 'react-intl';
1313
import { UUID } from 'crypto';
1414
import { RawReadOnlyInput } from './RawReadOnlyInput';
15-
import { FieldLabel } from './utils/FieldLabel';
16-
import { useCustomFormContext } from './provider/useCustomFormContext';
17-
import { isFieldRequired } from './utils/functions';
18-
import { ErrorInput } from './errorManagement/ErrorInput';
19-
import { useSnackMessage } from '../../../hooks/useSnackMessage';
15+
import { FieldLabel, isFieldRequired } from './utils';
16+
import { useCustomFormContext } from './provider';
17+
import { ErrorInput, MidFormError } from './errorManagement';
18+
import { useSnackMessage } from '../../../hooks';
2019
import { TreeViewFinderNodeProps } from '../../treeViewFinder';
21-
import { mergeSx, type MuiStyles } from '../../../utils/styles';
20+
import { type MuiStyles } from '../../../utils/styles';
2221
import { OverflowableText } from '../../overflowableText';
23-
import { MidFormError } from './errorManagement/MidFormError';
24-
import { DirectoryItemSelector } from '../../directoryItemSelector/DirectoryItemSelector';
22+
import { DirectoryItemSelector } from '../../directoryItemSelector';
2523
import { fetchDirectoryElementPath } from '../../../services';
26-
import { ElementAttributes } from '../../../utils';
24+
import { ElementAttributes, mergeSx } from '../../../utils';
2725
import { NAME } from './constants';
26+
import { getFilterEquipmentTypeLabel } from '../../filter/expert/expertFilterUtils';
2827

2928
const styles = {
3029
formDirectoryElements1: {
@@ -67,6 +66,7 @@ export interface DirectoryItemsInputProps {
6766
disable?: boolean;
6867
allowMultiSelect?: boolean;
6968
labelRequiredFromContext?: boolean;
69+
equipmentColorsMap?: Map<string, string>;
7070
}
7171

7272
export function DirectoryItemsInput({
@@ -82,6 +82,7 @@ export function DirectoryItemsInput({
8282
disable = false,
8383
allowMultiSelect = true,
8484
labelRequiredFromContext = true,
85+
equipmentColorsMap,
8586
}: Readonly<DirectoryItemsInputProps>) {
8687
const { snackError } = useSnackMessage();
8788
const intl = useIntl();
@@ -94,7 +95,7 @@ export function DirectoryItemsInput({
9495
fields: elements,
9596
append,
9697
remove,
97-
} = useFieldArray({
98+
} = useFieldArray<{ [key: string]: TreeViewFinderNodeProps[] }>({
9899
name,
99100
});
100101

@@ -189,18 +190,44 @@ export function DirectoryItemsInput({
189190
{elements?.length > 0 && (
190191
<FormControl sx={styles.formDirectoryElements2}>
191192
{elements.map((item, index) => (
192-
<Chip
193+
<Box
193194
key={item.id}
194-
size="small"
195-
onDelete={() => removeElements(index)}
196-
onClick={() => handleChipClick(index)}
197-
label={
198-
<OverflowableText
199-
text={<RawReadOnlyInput name={`${name}.${index}.${NAME}`} />}
200-
sx={{ width: '100%' }}
201-
/>
202-
}
203-
/>
195+
sx={{ display: 'flex', alignItems: 'center', flexDirection: 'column', gap: 1 }}
196+
>
197+
<Chip
198+
size="small"
199+
sx={{
200+
backgroundColor:
201+
item?.specificMetadata?.equipmentType &&
202+
equipmentColorsMap?.get(item?.specificMetadata?.equipmentType),
203+
}}
204+
onDelete={() => removeElements(index)}
205+
onClick={() => handleChipClick(index)}
206+
label={
207+
<OverflowableText
208+
text={
209+
getValues(`${name}.${index}.${NAME}`) ? (
210+
<RawReadOnlyInput name={`${name}.${index}.${NAME}`} />
211+
) : (
212+
intl.formatMessage({ id: 'elementNotFound' })
213+
)
214+
}
215+
sx={{ width: '100%' }}
216+
/>
217+
}
218+
/>
219+
{equipmentColorsMap && (
220+
<FormHelperText>
221+
{item?.specificMetadata?.equipmentType ? (
222+
<FormattedMessage
223+
id={getFilterEquipmentTypeLabel(item.specificMetadata.equipmentType)}
224+
/>
225+
) : (
226+
''
227+
)}
228+
</FormHelperText>
229+
)}
230+
</Box>
204231
))}
205232
</FormControl>
206233
)}

src/components/treeViewFinder/TreeViewFinder.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ export interface TreeViewFinderNodeProps {
8282
childrenCount?: number;
8383
children?: TreeViewFinderNodeProps[];
8484
parents?: TreeViewFinderNodeProps[];
85+
specificMetadata?: {
86+
equipmentType: string;
87+
};
8588
}
8689

8790
interface TreeViewFinderNodeMapProps {

src/translations/en/filterEn.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ export const filterEn = {
5757
nameAlreadyUsed: 'This name is already used',
5858
nameValidityCheckErrorMsg: 'Error while checking name validity',
5959
cantSubmitWhileValidating: 'Impossible to submit the form while validating a field',
60+
elementNotFound: 'element not found',
6061
};

src/translations/fr/filterFr.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ export const filterFr = {
5757
nameAlreadyUsed: 'Ce nom est déjà utilisé',
5858
nameValidityCheckErrorMsg: 'Erreur lors de la vérification de la validité du nom',
5959
cantSubmitWhileValidating: "Impossible de soumettre le formulaire durant la validation d'un champ",
60+
elementNotFound: 'Élément inexistant',
6061
};

src/utils/constants/fieldConstants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export enum FieldConstants {
2727
EQUIPMENT_TABLE = 'equipmentTable',
2828
EQUIPMENT_TYPE = 'equipmentType',
2929
FILTER_TYPE = 'filterType',
30+
FILTERS = 'filters',
3031
FOLDER_ID = 'folderId',
3132
FOLDER_NAME = 'folderName',
3233
FORMATTED_CASE_PARAMETERS = 'formattedCaseParameters',
@@ -37,6 +38,7 @@ export enum FieldConstants {
3738
NOMINAL_VOLTAGE_3 = 'nominalVoltage3',
3839
NOMINAL_VOLTAGE = 'nominalVoltage',
3940
OPERATION_TYPE = 'type',
41+
TYPE = 'type',
4042
PROPERTY_NAME = 'propertyName',
4143
PROPERTY_OPERATOR = 'propertyOperator',
4244
PROPERTY = 'PROPERTY',

0 commit comments

Comments
 (0)