From 67c0a2c4236c0c33826e8ce3c6d25f49dddb2d75 Mon Sep 17 00:00:00 2001 From: theocrsb Date: Tue, 15 Oct 2024 14:07:08 +0200 Subject: [PATCH] front: change display when description is too long Signed-off-by: theocrsb --- .../Scenario/ScenarioDescription.tsx | 56 ++++++++++++++++++- .../operationalStudies/_scenario.scss | 48 ++++++++++++++-- 2 files changed, 96 insertions(+), 8 deletions(-) diff --git a/front/src/applications/operationalStudies/components/Scenario/ScenarioDescription.tsx b/front/src/applications/operationalStudies/components/Scenario/ScenarioDescription.tsx index c7d431d9e7f..47bdc27b87a 100644 --- a/front/src/applications/operationalStudies/components/Scenario/ScenarioDescription.tsx +++ b/front/src/applications/operationalStudies/components/Scenario/ScenarioDescription.tsx @@ -1,9 +1,12 @@ -import { Blocked, ChevronLeft, Pencil } from '@osrd-project/ui-icons'; +import { useEffect, useRef, useState } from 'react'; + +import { Blocked, ChevronLeft, Pencil, X } from '@osrd-project/ui-icons'; import { useTranslation } from 'react-i18next'; import type { InfraWithState, ScenarioResponse } from 'common/api/osrdEditoastApi'; import { useModal } from 'common/BootstrapSNCF/ModalSNCF'; import AddAndEditScenarioModal from 'modules/scenario/components/AddOrEditScenarioModal'; +import useOutsideClick from 'utils/hooks/useOutsideClick'; import InfraLoadingState from './InfraLoadingState'; @@ -22,6 +25,34 @@ const ScenarioDescription = ({ }: ScenarioDescriptionProps) => { const { t } = useTranslation('operationalStudies/scenario'); const { openModal } = useModal(); + const [isOpenedDescription, setIsOpenedDescription] = useState(false); + const expandedDescriptionRef = useRef(null); + const collapsedDescriptionRef = useRef(null); + const [isTooLongDescription, setIsTooLongDescription] = useState(false); + + const toggleDescription = () => { + setIsOpenedDescription(!isOpenedDescription); + }; + + const checkIfDescriptionIsTooLong = () => { + if (collapsedDescriptionRef.current) + setIsTooLongDescription( + collapsedDescriptionRef.current.scrollHeight > collapsedDescriptionRef.current.clientHeight + ); + }; + + useOutsideClick(expandedDescriptionRef, () => setIsOpenedDescription(false)); + + useEffect(() => { + checkIfDescriptionIsTooLong(); + window.addEventListener('resize', checkIfDescriptionIsTooLong); + + return () => { + window.removeEventListener('resize', checkIfDescriptionIsTooLong); + }; + }, []); + + useEffect(checkIfDescriptionIsTooLong, [scenario.description]); return (
@@ -30,10 +61,29 @@ const ScenarioDescription = ({ {scenario.name}
-
{scenario.description && ( -
{scenario.description}
+
+ {scenario.description} + {isTooLongDescription && ( + + (plus) + + )} +
+ )} + {isOpenedDescription && ( +
+ {scenario.description} + + + +
)}