|
| 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 { useCallback, useMemo } from 'react'; |
| 9 | +import { Box, IconButton, ToggleButton } from '@mui/material'; |
| 10 | +import ArrowUpwardIcon from '@mui/icons-material/ArrowUpward'; |
| 11 | +import ArrowDownwardIcon from '@mui/icons-material/ArrowDownward'; |
| 12 | +import { useController, useFormContext } from 'react-hook-form'; |
| 13 | +import { useIntl } from 'react-intl'; |
| 14 | +import type { MuiStyles } from '@gridsuite/commons-ui'; |
| 15 | + |
| 16 | +const styles = { |
| 17 | + container: { |
| 18 | + display: 'flex', |
| 19 | + alignItems: 'center', |
| 20 | + gap: 1, |
| 21 | + marginTop: 1.75, |
| 22 | + }, |
| 23 | + button: { |
| 24 | + width: 100, |
| 25 | + whiteSpace: 'nowrap', |
| 26 | + }, |
| 27 | +} as const satisfies MuiStyles; |
| 28 | + |
| 29 | +const CONNECTION_DIRECTIONS_VALUES = { |
| 30 | + TOP: { id: 'TOP', label: 'Top' }, |
| 31 | + BOTTOM: { id: 'BOTTOM', label: 'Bottom' }, |
| 32 | +} as const; |
| 33 | + |
| 34 | +type FeederBayDirectionCellRendererProps = { |
| 35 | + name: string; |
| 36 | + disabled: boolean; |
| 37 | +}; |
| 38 | + |
| 39 | +export default function FeederBayDirectionCellRenderer({ |
| 40 | + name, |
| 41 | + disabled, |
| 42 | +}: Readonly<FeederBayDirectionCellRendererProps>) { |
| 43 | + const { setValue } = useFormContext(); |
| 44 | + const { |
| 45 | + field: { value }, |
| 46 | + } = useController({ name }); |
| 47 | + const intl = useIntl(); |
| 48 | + |
| 49 | + const translatedLabel = useMemo(() => { |
| 50 | + const direction = Object.values(CONNECTION_DIRECTIONS_VALUES).find((dir) => dir.id === value); |
| 51 | + return direction ? intl.formatMessage({ id: direction.label }) : ''; |
| 52 | + }, [intl, value]); |
| 53 | + |
| 54 | + const handleClick = useCallback(() => { |
| 55 | + if (value) { |
| 56 | + const newValue = |
| 57 | + value === CONNECTION_DIRECTIONS_VALUES.TOP.id |
| 58 | + ? CONNECTION_DIRECTIONS_VALUES.BOTTOM.id |
| 59 | + : CONNECTION_DIRECTIONS_VALUES.TOP.id; |
| 60 | + setValue(name, newValue, { |
| 61 | + shouldDirty: true, |
| 62 | + shouldTouch: true, |
| 63 | + }); |
| 64 | + } |
| 65 | + }, [value, setValue, name]); |
| 66 | + |
| 67 | + return ( |
| 68 | + <Box sx={styles.container}> |
| 69 | + <IconButton onClick={handleClick} size="small" disabled={disabled}> |
| 70 | + {value === CONNECTION_DIRECTIONS_VALUES.TOP.id ? <ArrowUpwardIcon /> : <ArrowDownwardIcon />} |
| 71 | + </IconButton> |
| 72 | + <ToggleButton value={value} onClick={handleClick} disabled={disabled} size="small" sx={styles.button}> |
| 73 | + {translatedLabel} |
| 74 | + </ToggleButton> |
| 75 | + </Box> |
| 76 | + ); |
| 77 | +} |
0 commit comments