1
1
import classNames from "classnames" ;
2
- import { FC , useEffect , useState } from "react" ;
2
+ import { FC , useEffect , useRef , useState } from "react" ;
3
3
4
4
import DraggableList from "Components/DraggableList" ;
5
5
import { IDraggableListItem } from "Components/DraggableList/types" ;
@@ -26,8 +26,10 @@ const Accordion: FC<IAccordionProps> = ({
26
26
setExpanded,
27
27
selectedId,
28
28
hasDraggableSections,
29
- hasDraggableSectionItems
29
+ hasDraggableSectionItems,
30
+ defaultExpanded
30
31
} ) => {
32
+ const hasInitialized = useRef ( false ) ;
31
33
const [ selectedItemId , setSelectedItemId ] = useState <
32
34
number | null | undefined
33
35
> ( selectedId ) ;
@@ -50,6 +52,13 @@ const Accordion: FC<IAccordionProps> = ({
50
52
51
53
useEffect ( ( ) => setSelectedItemId ( selectedId ) , [ selectedId ] ) ;
52
54
55
+ useEffect ( ( ) => {
56
+ if ( defaultExpanded && sections . length > 0 && ! hasInitialized . current ) {
57
+ setExpanded ( sections . map ( section => section . id ) ) ;
58
+ hasInitialized . current = true ;
59
+ }
60
+ } , [ defaultExpanded , sections , setExpanded ] ) ;
61
+
53
62
const handleDrag = (
54
63
_ : React . DragEvent < Element > ,
55
64
item : IDraggableListItem
@@ -113,12 +122,14 @@ const Accordion: FC<IAccordionProps> = ({
113
122
e . preventDefault ( ) ;
114
123
onSectionClick ?.( section ) ;
115
124
setSelectedSectionId ( section . id ) ;
125
+ setSelectedItemId ( null ) ;
126
+ toggleExpand ( section . id ) ;
116
127
} }
117
128
className = { classNames (
118
- "yl:relative yl:flex yl:cursor-default yl:select-none yl:items-center yl:font-semibold yl:capitalize yl:p-4" ,
129
+ "yl:relative yl:flex yl:cursor-default yl:select-none yl:items-center yl:font-semibold yl:capitalize yl:p-4 yl:hover:text-primary yl:transition-colors yl:duration-400 yl:ease-in-out yl:hover:cursor-pointer " ,
119
130
sectionTitleClassName ,
120
131
{
121
- "yl:bg-text/5 " : expanded . includes ( section . id ) ,
132
+ "yl:bg-text/2 " : expanded . includes ( section . id ) ,
122
133
"yl:cursor-pointer" : onSectionItemClick ,
123
134
"yl:text-text" :
124
135
selectedSectionId !== section . id || ! onSectionItemClick ,
@@ -147,22 +158,12 @@ const Accordion: FC<IAccordionProps> = ({
147
158
< Icon
148
159
iconName = 'IconExpandLess'
149
160
dataTestId = 'icon-expand-less'
150
- onClick = { e => {
151
- e . stopPropagation ( ) ;
152
- e . preventDefault ( ) ;
153
- toggleExpand ( section . id ) ;
154
- } }
155
161
className = 'yl:absolute yl:right-2 yl:w-6 yl:cursor-pointer yl:fill-primary yl:hover:fill-primary'
156
162
/>
157
163
) : (
158
164
< Icon
159
165
iconName = 'IconExpandMore'
160
166
dataTestId = 'icon-expand-more'
161
- onClick = { e => {
162
- e . stopPropagation ( ) ;
163
- e . preventDefault ( ) ;
164
- toggleExpand ( section . id ) ;
165
- } }
166
167
className = 'yl:absolute yl:right-2 yl:w-6 yl:cursor-pointer yl:fill-primary yl:hover:fill-primary'
167
168
/>
168
169
) }
@@ -183,6 +184,7 @@ const Accordion: FC<IAccordionProps> = ({
183
184
onSectionItemClick ?.( item ) ;
184
185
onSelected ?.( item ) ;
185
186
setSelectedItemId ( item . id ) ;
187
+ setSelectedSectionId ( null ) ;
186
188
} }
187
189
onDragged = { ( items : IDraggableListItem [ ] ) => {
188
190
onSorted ?.(
@@ -200,12 +202,12 @@ const Accordion: FC<IAccordionProps> = ({
200
202
draggedOverClassName = 'yl:border-t-2 yl:border-border'
201
203
liClassName = { ( item : IDraggableListItem ) =>
202
204
classNames (
203
- "yl:cursor-default hover:text-text break-words leading-normal yl:px-4" ,
205
+ "yl:cursor-default yl: break-words yl: leading-normal yl:px-4" ,
204
206
{
205
- "yl:cursor-pointer" : onSectionItemClick ,
206
- "yl:text-text" : selectedItemId !== item . id ,
207
- // "yl:text-text ":
208
- // onSectionItemClick && selectedItemId === item.id,
207
+ "yl:hover: cursor-pointer yl:hover:text-primary" :
208
+ onSectionItemClick ,
209
+ "yl:text-primary " :
210
+ onSectionItemClick && selectedItemId === item . id ,
209
211
"yl:pl-5" : section . items . length === 1
210
212
}
211
213
)
@@ -215,17 +217,29 @@ const Accordion: FC<IAccordionProps> = ({
215
217
< div
216
218
onClick = { ( ) => onAddSectionItem ( section ) }
217
219
className = { classNames (
218
- "yl:cursor-pointer yl:hover:text-text yl:pb-4 yl:text -text yl:text-center yl:flex yl:items-center yl:justify-center yl:gap-1 "
220
+ "yl:group yl:border-border yl:hover:bg -text/2 yl:cursor-pointer "
219
221
) }
222
+ role = 'presentation'
220
223
>
221
- < Icon
222
- iconName = 'IconAddCircle'
223
- dataTestId = 'icon-add-circle'
224
- className = 'yl:w-6'
225
- />
226
- { addSectionItemLabel && (
227
- < span className = 'yl:text-sm' > { addSectionItemLabel } </ span >
228
- ) }
224
+ < h3
225
+ className = { classNames (
226
+ "yl:flex yl:flex-col yl:items-center yl:justify-center yl:select-none yl:font-semibold yl:p-4 yl:text-center"
227
+ ) }
228
+ role = 'button'
229
+ >
230
+ < Paragraph className = 'yl:flex yl:items-center yl:gap-1' >
231
+ < Icon
232
+ iconName = 'IconAddCircle'
233
+ dataTestId = 'icon-add-circle'
234
+ className = 'yl:w-6 yl:text-primary'
235
+ />
236
+ { addSectionItemLabel && (
237
+ < span className = 'yl:text-sm yl:text-primary' >
238
+ { addSectionItemLabel }
239
+ </ span >
240
+ ) }
241
+ </ Paragraph >
242
+ </ h3 >
229
243
</ div >
230
244
) }
231
245
</ >
@@ -236,7 +250,7 @@ const Accordion: FC<IAccordionProps> = ({
236
250
< div
237
251
onClick = { onAddSection }
238
252
className = { classNames (
239
- "yl:group yl:border-2 yl:border-t-0 yl:border-border yl:hover:bg-text/5 yl:rounded-bl-md yl:rounded-br-md yl:cursor-pointer"
253
+ "yl:group yl:border-2 yl:border-t-0 yl:border-border yl:hover:bg-text/2 yl:rounded-bl-md yl:rounded-br-md yl:cursor-pointer"
240
254
) }
241
255
role = 'presentation'
242
256
>
@@ -246,14 +260,16 @@ const Accordion: FC<IAccordionProps> = ({
246
260
) }
247
261
role = 'button'
248
262
>
249
- < Paragraph className = 'yl:text-text/70 yl:group-hover:text-text yl: flex yl:items-center yl:gap-1' >
263
+ < Paragraph className = 'yl:flex yl:items-center yl:gap-1' >
250
264
< Icon
251
265
iconName = 'IconAddCircle'
252
266
dataTestId = 'icon-add-circle'
253
- className = 'yl:w-6 yl:group-hover: text-text '
267
+ className = 'yl:w-6 yl:text-primary '
254
268
/>
255
269
{ addSectionLabel && (
256
- < span className = 'yl:text-sm' > { addSectionLabel } </ span >
270
+ < span className = 'yl:text-sm yl:text-primary' >
271
+ { addSectionLabel }
272
+ </ span >
257
273
) }
258
274
</ Paragraph >
259
275
</ h3 >
0 commit comments