|
| 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 | + |
| 8 | +import { useState } from 'react'; |
| 9 | +import { FormattedMessage } from 'react-intl'; |
| 10 | +import { Box, Button } from '@mui/material'; |
| 11 | +import AddIcon from '@mui/icons-material/Add'; |
| 12 | +import DeleteIcon from '@mui/icons-material/Delete'; |
| 13 | +import { useFormContext } from 'react-hook-form'; // Import useFormContext |
| 14 | +import FieldConstants from '../../../utils/field-constants'; |
| 15 | +import ExpandingTextField from './ExpandingTextField'; |
| 16 | + |
| 17 | +function DescriptionField() { |
| 18 | + const [isDescriptionFieldVisible, setIsDescriptionFieldVisible] = useState(false); |
| 19 | + const { setValue } = useFormContext(); |
| 20 | + |
| 21 | + const handleOpenDescription = () => { |
| 22 | + setIsDescriptionFieldVisible(true); |
| 23 | + setValue(FieldConstants.DESCRIPTION, ''); |
| 24 | + }; |
| 25 | + |
| 26 | + const handleCloseDescription = () => { |
| 27 | + setIsDescriptionFieldVisible(false); |
| 28 | + }; |
| 29 | + |
| 30 | + return ( |
| 31 | + <Box> |
| 32 | + {!isDescriptionFieldVisible ? ( |
| 33 | + <Button startIcon={<AddIcon />} onClick={handleOpenDescription}> |
| 34 | + <FormattedMessage id="AddDescription" /> |
| 35 | + </Button> |
| 36 | + ) : ( |
| 37 | + <Box sx={{ display: 'flex', alignItems: 'flex-start' }}> |
| 38 | + <ExpandingTextField |
| 39 | + name={FieldConstants.DESCRIPTION} |
| 40 | + label="descriptionProperty" |
| 41 | + minRows={3} |
| 42 | + rows={3} |
| 43 | + sx={{ flexGrow: 1 }} |
| 44 | + /> |
| 45 | + <Button |
| 46 | + sx={{ |
| 47 | + alignSelf: 'flex-end', |
| 48 | + marginLeft: 1, |
| 49 | + padding: 1, |
| 50 | + marginBottom: 2, |
| 51 | + }} |
| 52 | + onClick={handleCloseDescription} |
| 53 | + > |
| 54 | + <DeleteIcon /> |
| 55 | + </Button> |
| 56 | + </Box> |
| 57 | + )} |
| 58 | + </Box> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +export default DescriptionField; |
0 commit comments