|
| 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 | +import { LimitsTagChip } from './limits-tag-chip'; |
| 8 | +import { Autocomplete, AutocompleteRenderInputParams, Box, Stack, TextField, IconButton } from '@mui/material'; |
| 9 | +import { useCallback, useState } from 'react'; |
| 10 | +import { AddCircle, Delete } from '@mui/icons-material'; |
| 11 | +import { LimitsPropertyName } from './limits-constants'; |
| 12 | +import { useIntl } from 'react-intl'; |
| 13 | +import { LimitsProperty } from '../../../services/network-modification-types'; |
| 14 | +import { useFormContext, useWatch } from 'react-hook-form'; |
| 15 | + |
| 16 | +export interface LimitsPropertiesSideStackProps { |
| 17 | + formName: string; |
| 18 | +} |
| 19 | +export function LimitsPropertiesSideStack({ formName, ...props }: Readonly<LimitsPropertiesSideStackProps>) { |
| 20 | + const { setValue } = useFormContext(); |
| 21 | + const limitsProperties: LimitsProperty[] = useWatch({ name: formName }); |
| 22 | + |
| 23 | + const [isEditing, setIsEditing] = useState<boolean>(false); |
| 24 | + const [hovered, setHovered] = useState<boolean>(false); |
| 25 | + const [propertyName, setPropertyName] = useState<string>(''); |
| 26 | + const [propertyValue, setPropertyValue] = useState<string>(''); |
| 27 | + const [editorError, setEditorError] = useState<string>(''); |
| 28 | + const intl = useIntl(); |
| 29 | + |
| 30 | + const handleKeyPress = useCallback( |
| 31 | + (event: React.KeyboardEvent<HTMLInputElement>) => { |
| 32 | + if (event.key === 'Enter') { |
| 33 | + if (!propertyName.trim() || !propertyValue.trim()) { |
| 34 | + return; |
| 35 | + } |
| 36 | + if (!limitsProperties?.length) { |
| 37 | + setValue(formName, [{ name: propertyName, value: propertyValue }]); |
| 38 | + } else if (limitsProperties.find((l) => l.name === propertyName)) { |
| 39 | + setEditorError(intl.formatMessage({ id: 'UsedPropertyName' })); |
| 40 | + return; |
| 41 | + } else { |
| 42 | + setValue(formName, [...limitsProperties, { name: propertyName, value: propertyValue }]); |
| 43 | + } |
| 44 | + setIsEditing(false); |
| 45 | + } |
| 46 | + }, |
| 47 | + [formName, intl, limitsProperties, propertyName, propertyValue, setValue] |
| 48 | + ); |
| 49 | + |
| 50 | + const handleOnChange = useCallback((value: string) => { |
| 51 | + setPropertyName(value); |
| 52 | + setEditorError(''); |
| 53 | + }, []); |
| 54 | + |
| 55 | + const handleDelete = useCallback( |
| 56 | + (index: number) => { |
| 57 | + if (limitsProperties?.length <= index) { |
| 58 | + return; |
| 59 | + } |
| 60 | + setValue(formName, [...limitsProperties.slice(0, index), ...limitsProperties.slice(index + 1)]); |
| 61 | + }, |
| 62 | + [formName, limitsProperties, setValue] |
| 63 | + ); |
| 64 | + |
| 65 | + return ( |
| 66 | + <Stack direction="column" spacing={2} paddingBottom={2} flexWrap="wrap"> |
| 67 | + <Stack direction="row" {...props} sx={{ display: 'flex', flexWrap: 'wrap' }}> |
| 68 | + {limitsProperties?.map((property: LimitsProperty, index: number) => ( |
| 69 | + <LimitsTagChip |
| 70 | + key={`${property.name}`} |
| 71 | + limitsProperty={property} |
| 72 | + onDelete={() => handleDelete(index)} |
| 73 | + /> |
| 74 | + ))} |
| 75 | + {!isEditing ? ( |
| 76 | + <IconButton color="primary" sx={{ verticalAlign: 'center' }} onClick={() => setIsEditing(true)}> |
| 77 | + <AddCircle /> |
| 78 | + </IconButton> |
| 79 | + ) : ( |
| 80 | + '' |
| 81 | + )} |
| 82 | + </Stack> |
| 83 | + {isEditing ? ( |
| 84 | + <Box |
| 85 | + display="flex" |
| 86 | + gap={2} |
| 87 | + width="100%" |
| 88 | + onMouseEnter={() => setHovered(true)} |
| 89 | + onMouseLeave={() => setHovered(false)} |
| 90 | + > |
| 91 | + <Autocomplete |
| 92 | + options={Object.values(LimitsPropertyName)} |
| 93 | + size="small" |
| 94 | + onChange={(event, value) => handleOnChange(value ?? '')} |
| 95 | + renderInput={(params: AutocompleteRenderInputParams) => ( |
| 96 | + <TextField |
| 97 | + label={intl.formatMessage({ id: 'PropertyName' })} |
| 98 | + variant="outlined" |
| 99 | + {...params} |
| 100 | + onChange={(event) => handleOnChange(event.target.value)} |
| 101 | + fullWidth |
| 102 | + error={!!editorError?.length} |
| 103 | + helperText={editorError} |
| 104 | + onKeyDown={handleKeyPress} |
| 105 | + /> |
| 106 | + )} |
| 107 | + sx={{ flex: 1 }} |
| 108 | + freeSolo={true} |
| 109 | + /> |
| 110 | + <TextField |
| 111 | + size="small" |
| 112 | + label={intl.formatMessage({ id: 'PropertyValue' })} |
| 113 | + sx={{ flex: 1 }} |
| 114 | + onKeyDown={handleKeyPress} |
| 115 | + onChange={(event) => setPropertyValue(event.target.value)} |
| 116 | + /> |
| 117 | + {hovered && ( |
| 118 | + <IconButton sx={{ verticalAlign: 'center' }} onClick={() => setIsEditing(false)}> |
| 119 | + <Delete /> |
| 120 | + </IconButton> |
| 121 | + )} |
| 122 | + </Box> |
| 123 | + ) : ( |
| 124 | + '' |
| 125 | + )} |
| 126 | + </Stack> |
| 127 | + ); |
| 128 | +} |
0 commit comments