1
- import React , { memo } from "react"
2
- import "./dropdownItems.scss"
3
- import CustomImage from "../../../CustomImage/CustomImage"
4
- import CustomLink from "../../../CustomLink/CustomLink"
5
- import PropTypes from "prop-types"
1
+ import React , { memo , useState , useEffect } from "react" ;
2
+ import "./dropdownItems.scss" ;
3
+ import CustomImage from "../../../CustomImage/CustomImage" ;
4
+ import CustomLink from "../../../CustomLink/CustomLink" ;
5
+ import PropTypes from "prop-types" ;
6
+ import { FaAngleDown } from "react-icons/fa" ;
7
+
8
+ const RenderSection = ( { section, className, isOpen, toggleSubLandingPages, isMobileView } ) => {
9
+ const { icon, label, url, landing_page, sub_landing_pages = [ ] } = section || { } ;
10
+ const hasSubLandingPages = sub_landing_pages . length > 0 ;
6
11
7
- const RenderSection = ( { section, className } ) => {
8
12
return (
9
13
< >
10
14
< div className = { className } >
11
15
< CustomImage
12
- image = { section ?. icon }
13
- alt = { section ?. icon ?. alternativeText || "NavLink icon" }
16
+ image = { icon }
17
+ alt = { icon ?. alternativeText || "NavLink icon" }
14
18
className = "navbarItemIcon"
15
19
width = { 28 }
16
20
height = { 28 }
17
21
/>
18
22
< CustomLink
19
- content = { section . label }
20
- url = { section ?. url }
21
- landing = { section ?. landing_page }
23
+ content = { label }
24
+ url = { url }
25
+ landing = { landing_page }
22
26
className = "dropdownItem_link-inner"
23
27
/>
28
+ { hasSubLandingPages && (
29
+ < FaAngleDown
30
+ className = { `dropdownItem_icon ${ isOpen ? "open" : "" } ` }
31
+ onClick = { ( ) => toggleSubLandingPages ( section . id ) }
32
+ />
33
+ ) }
24
34
</ div >
25
35
{ section ?. text && < p className = "navItemP" > { section . text } </ p > }
26
- { section ?. sub_landing_pages && section . sub_landing_pages . length > 0 && (
27
- < ul className = "subLandingPages" >
28
- { section . sub_landing_pages . map ( subItem => (
29
- < li key = { subItem . id } >
36
+
37
+ { ( hasSubLandingPages && ( isOpen || ! isMobileView ) ) && (
38
+ < ul className = { `subLandingPages ${ sub_landing_pages . length > 5 ? "two-column-list" : "" } ` } >
39
+ { sub_landing_pages . map ( ( { id, name, slug } ) => (
40
+ < li key = { id } className = "subLandingPages-item" >
30
41
< CustomLink
31
- content = { subItem . name }
32
- url = { `/${ subItem . slug } ` }
42
+ content = { name }
43
+ url = { `/${ slug } ` }
33
44
className = "dropdownItem_link-subLanding"
34
45
/>
35
46
</ li >
36
47
) ) }
37
48
</ ul >
38
49
) }
39
50
</ >
40
- )
41
- }
51
+ ) ;
52
+ } ;
42
53
43
54
RenderSection . propTypes = {
44
55
className : PropTypes . string ,
45
56
section : PropTypes . shape ( {
57
+ id : PropTypes . number . isRequired ,
46
58
label : PropTypes . string . isRequired ,
47
59
text : PropTypes . string ,
48
60
url : PropTypes . string ,
@@ -51,6 +63,7 @@ RenderSection.propTypes = {
51
63
} ) ,
52
64
sub_landing_pages : PropTypes . arrayOf (
53
65
PropTypes . shape ( {
66
+ id : PropTypes . number . isRequired ,
54
67
slug : PropTypes . string . isRequired ,
55
68
name : PropTypes . string . isRequired ,
56
69
} )
@@ -64,49 +77,77 @@ RenderSection.propTypes = {
64
77
} ) ,
65
78
} ) ,
66
79
} ) ,
67
- } ) ,
68
- }
80
+ } ) . isRequired ,
81
+ isOpen : PropTypes . bool . isRequired ,
82
+ toggleSubLandingPages : PropTypes . func . isRequired ,
83
+ isMobileView : PropTypes . bool . isRequired ,
84
+ } ;
85
+
69
86
70
87
const DropdownItems = memo ( ( { sections, topLevel } ) => {
71
- return (
72
- < div className = "dropdownItem_container" style = { ! sections ? { maxHeight : "0" } : { } } >
73
- < div className = "dropdownItem_section" data-first-dropdown-section >
74
- { topLevel && (
75
- < div
76
- className = "dropdownItem_topLevel"
77
- style = { {
78
- marginRight : "15px" , // Espacio entre topLevel y dropdown
79
- paddingBottom : "8px" ,
80
- } }
81
- >
88
+ const [ openSectionId , setOpenSectionId ] = useState ( null ) ;
89
+ const [ isMobileView , setIsMobileView ] = useState ( window . innerWidth < 1200 ) ;
90
+
91
+ // Actualizar el estado `isMobileView` según el tamaño de la pantalla
92
+ useEffect ( ( ) => {
93
+ const handleResize = ( ) => {
94
+ setIsMobileView ( window . innerWidth < 1200 ) ;
95
+ } ;
96
+ window . addEventListener ( "resize" , handleResize ) ;
97
+ return ( ) => window . removeEventListener ( "resize" , handleResize ) ;
98
+ } , [ ] ) ;
99
+
100
+ const toggleSubLandingPages = ( sectionId ) => {
101
+ if ( isMobileView ) {
102
+ setOpenSectionId ( ( prevId ) => ( prevId === sectionId ? null : sectionId ) ) ;
103
+ }
104
+ } ;
105
+
106
+ return (
107
+ < div className = "dropdownItem_container" style = { ! sections ? { maxHeight : "0" } : { } } >
108
+ < div className = "dropdownItem_section" data-first-dropdown-section >
109
+ { topLevel && (
110
+ < div
111
+ className = "dropdownItem_topLevel"
112
+ style = { {
113
+ marginRight : "15px" , // Espacio entre topLevel y dropdown
114
+ paddingBottom : "8px" ,
115
+ } }
116
+ >
117
+ < RenderSection
118
+ section = { topLevel }
119
+ className = { "dropdownItem_link-topLevel" }
120
+ isOpen = { openSectionId === topLevel . id }
121
+ toggleSubLandingPages = { toggleSubLandingPages }
122
+ isMobileView = { isMobileView }
123
+ />
124
+ </ div >
125
+ ) }
126
+ < div className = "dropdownItem_content" >
127
+ { sections ?. map ( ( section ) => (
128
+ < div key = { section . id } className = "dropdownItem" >
82
129
< RenderSection
83
- section = { topLevel }
84
- className = { "dropdownItem_link-topLevel" }
130
+ section = { section }
131
+ className = { "dropdownItem_link" }
132
+ isOpen = { openSectionId === section . id }
133
+ toggleSubLandingPages = { toggleSubLandingPages }
134
+ isMobileView = { isMobileView }
85
135
/>
86
136
</ div >
87
- ) }
88
- < div className = "dropdownItem_content" >
89
- { sections ?. map ( section => (
90
- < div key = { section . id } className = "dropdownItem" >
91
- < RenderSection
92
- section = { section }
93
- className = { "dropdownItem_link" }
94
- />
95
- </ div >
96
- ) ) }
97
- </ div >
137
+ ) ) }
98
138
</ div >
99
139
</ div >
100
- )
101
- } )
102
-
140
+ </ div >
141
+ ) ;
142
+ } ) ;
143
+
103
144
DropdownItems . propTypes = {
104
145
topLevel : PropTypes . object ,
105
146
sections : PropTypes . arrayOf (
106
147
PropTypes . shape ( {
107
148
id : PropTypes . number . isRequired ,
108
149
} )
109
150
) ,
110
- }
151
+ } ;
111
152
112
- export default DropdownItems
153
+ export default DropdownItems ;
0 commit comments