@@ -26,38 +26,63 @@ const ScenarioDescription = ({
26
26
const { t } = useTranslation ( 'operationalStudies/scenario' ) ;
27
27
const { openModal } = useModal ( ) ;
28
28
const [ isOpenedDescription , setIsOpenedDescription ] = useState < boolean > ( false ) ;
29
- const ref = useRef < HTMLInputElement | null > ( null ) ;
30
29
const descriptionRef = useRef < HTMLDivElement | null > ( null ) ;
30
+ const ref = useRef < HTMLDivElement | null > ( null ) ;
31
+ const [ isTooLongDescription , setIsTooLongDescription ] = useState < boolean > ( false ) ;
31
32
32
33
const toggleDescription = ( ) => {
33
34
setIsOpenedDescription ( ! isOpenedDescription ) ;
34
35
} ;
35
36
37
+ const checkIfTooLong = ( ) => {
38
+ if ( ref && ref . current ) {
39
+ ref . current . scrollHeight > 60
40
+ ? setIsTooLongDescription ( true )
41
+ : setIsTooLongDescription ( false ) ;
42
+ }
43
+ } ;
44
+
36
45
useOutsideClick ( descriptionRef , ( ) => setIsOpenedDescription ( false ) ) ;
37
46
47
+ useEffect ( ( ) => {
48
+ checkIfTooLong ( ) ;
49
+ window . addEventListener ( 'resize' , checkIfTooLong ) ;
50
+
51
+ return ( ) => {
52
+ window . removeEventListener ( 'resize' , checkIfTooLong ) ;
53
+ } ;
54
+ } , [ ] ) ;
55
+
56
+ useEffect ( checkIfTooLong , [ scenario . description ] ) ;
57
+
38
58
return (
39
- < div ref = { ref } >
59
+ < div >
40
60
< div className = "scenario-details-name" >
41
61
< span className = "flex-grow-1 scenario-name text-truncate" title = { scenario . name } >
42
62
{ scenario . name }
43
63
</ span >
44
64
</ div >
45
-
46
65
< div className = "scenario-description" >
47
66
{ scenario . description && (
48
- < div
49
- ref = { descriptionRef }
50
- title = { isOpenedDescription ? '' : scenario . description }
51
- className = { `scenario-details-description ${ isOpenedDescription ? 'opened' : 'not-opened' } ` }
52
- >
67
+ < div ref = { ref } className = "scenario-details-description not-opened" >
68
+ { scenario . description }
69
+ { isTooLongDescription && (
70
+ < span role = "button" onClick = { toggleDescription } tabIndex = { 0 } >
71
+ (plus)
72
+ </ span >
73
+ ) }
74
+ </ div >
75
+ ) }
76
+ { isOpenedDescription && (
77
+ < div ref = { descriptionRef } className = "scenario-details-description opened" >
53
78
{ scenario . description }
54
79
< span
55
80
onClick = { toggleDescription }
56
81
role = "button"
57
82
tabIndex = { 0 }
58
- className = { isOpenedDescription ? ' displayed-description' : 'display-description' }
83
+ className = " displayed-description"
59
84
>
60
- { isOpenedDescription ? < X /> : '(plus)' }
85
+ < X />
61
86
</ span >
62
87
</ div >
63
88
) }
0 commit comments