1
- import React , { useEffect , useRef , useState } from 'react' ;
1
+ import React , { useRef , useState } from 'react' ;
2
2
import { Accordion as DSAccordion } from '@navikt/ds-react' ;
3
3
import { ParsedHtml } from 'components/_common/parsedHtml/ParsedHtml' ;
4
4
import { AnalyticsEvents , logAnalyticsEvent } from 'utils/analytics' ;
@@ -12,32 +12,29 @@ import { classNames } from 'utils/classnames';
12
12
import { handleStickyScrollOffset } from 'utils/scroll-to' ;
13
13
14
14
import defaultHtml from 'components/_common/parsedHtml/DefaultHtmlStyling.module.scss' ;
15
+ import { useCheckAndOpenAccordionPanel } from 'store/hooks/useCheckAndOpenAccordionPanel' ;
15
16
import styles from './Accordion.module.scss' ;
16
17
17
18
type AccordionProps = PartConfigAccordion ;
18
19
type PanelItem = AccordionProps [ 'accordion' ] [ number ] ;
19
20
20
21
export const Accordion = ( { accordion } : AccordionProps ) => {
21
- const itemRefs = useRef < ( HTMLDivElement | null ) [ ] > ( [ ] ) ;
22
+ const itemRefs = useRef ( accordion . map ( ( ) => React . createRef < HTMLDivElement > ( ) ) ) ;
22
23
const contentProps = usePageContentProps ( ) ;
23
24
const { context } = getDecoratorParams ( contentProps ) ;
24
25
const { editorView, type } = contentProps ;
25
26
const [ openAccordions , setOpenAccordions ] = useState < number [ ] > ( [ ] ) ;
26
- const expandAll = ( ) => {
27
- setOpenAccordions ( accordion . map ( ( _ , index ) => index ) ) ;
28
- } ;
29
- const validatePanel = ( item : PanelItem ) => ! ! ( item . title && item . html ) ;
30
27
31
- useShortcuts ( { shortcut : Shortcuts . SEARCH , callback : expandAll } ) ;
28
+ const expandAll = ( ) => setOpenAccordions ( accordion . map ( ( _ , index ) => index ) ) ;
29
+ const validatePanel = ( item : PanelItem ) => Boolean ( item . title && item . html ) ;
32
30
33
- const openChangeHandler = ( isOpening : boolean , tittel : string , index : number ) => {
34
- handleStickyScrollOffset ( isOpening , itemRefs . current [ index ] ) ;
31
+ useShortcuts ( { shortcut : Shortcuts . SEARCH , callback : expandAll } ) ;
35
32
36
- if ( isOpening ) {
37
- setOpenAccordions ( [ ... openAccordions , index ] ) ;
38
- } else {
39
- setOpenAccordions ( openAccordions . filter ( ( i ) => i !== index ) ) ;
40
- }
33
+ const handleOpenChange = ( isOpening : boolean , tittel : string , index : number ) => {
34
+ handleStickyScrollOffset ( isOpening , itemRefs . current [ index ] . current ) ;
35
+ setOpenAccordions ( ( prev ) =>
36
+ isOpening ? [ ... prev , index ] : prev . filter ( ( i ) => i !== index )
37
+ ) ;
41
38
logAnalyticsEvent ( isOpening ? AnalyticsEvents . ACC_EXPAND : AnalyticsEvents . ACC_COLLAPSE , {
42
39
tittel,
43
40
opprinnelse : 'trekkspill' ,
@@ -47,20 +44,11 @@ export const Accordion = ({ accordion }: AccordionProps) => {
47
44
} ) ;
48
45
} ;
49
46
50
- useEffect ( ( ) => {
51
- if ( window . location . toString ( ) . includes ( 'expandall=true' ) ) {
52
- expandAll ( ) ;
53
- return ;
54
- }
55
- const anchorHash = window . location . hash || '' ;
56
- const matchingAccordion = accordion . findIndex (
57
- ( item ) => validatePanel ( item ) && item . anchorId === anchorHash . slice ( 1 )
58
- ) ;
59
- if ( matchingAccordion !== - 1 ) {
60
- setOpenAccordions ( [ matchingAccordion ] ) ;
61
- }
62
- /* eslint-disable-next-line react-hooks/exhaustive-deps */
63
- } , [ ] ) ;
47
+ if ( itemRefs . current . length !== accordion . length ) {
48
+ itemRefs . current = accordion . map ( ( ) => React . createRef < HTMLDivElement > ( ) ) ;
49
+ }
50
+
51
+ useCheckAndOpenAccordionPanel ( openAccordions , setOpenAccordions , itemRefs . current , expandAll ) ;
64
52
65
53
// Show all panels in edit mode, but only valid panels in view mode
66
54
const validAccordion = accordion . filter ( validatePanel ) ;
@@ -75,21 +63,16 @@ export const Accordion = ({ accordion }: AccordionProps) => {
75
63
key = { index }
76
64
className = { styles . item }
77
65
open = { openAccordions . includes ( index ) }
78
- onOpenChange = { ( open ) => openChangeHandler ( open , item . title , index ) }
79
- ref = { ( el ) => {
80
- itemRefs . current [ index ] = el ;
81
- } }
66
+ onOpenChange = { ( open ) => handleOpenChange ( open , item . title , index ) }
67
+ ref = { itemRefs . current [ index ] }
82
68
tabIndex = { - 1 }
83
69
>
84
70
< DSAccordion . Header className = { styles . header } id = { item . anchorId } >
85
- { ! isValid && (
86
- < EditorHelp
87
- text = {
88
- 'Panelet mangler tittel eller innhold. Klikk for å redigere'
89
- }
90
- />
71
+ { ! isValid ? (
72
+ < EditorHelp text = "Panelet mangler tittel eller innhold. Klikk for å redigere" />
73
+ ) : (
74
+ < div className = { styles . headerTitle } > { item . title } </ div >
91
75
) }
92
- { isValid && < div className = { styles . headerTitle } > { item . title } </ div > }
93
76
</ DSAccordion . Header >
94
77
< DSAccordion . Content className = { styles . content } >
95
78
< div className = { classNames ( defaultHtml . html , 'parsedHtml' ) } >
0 commit comments