-
Notifications
You must be signed in to change notification settings - Fork 31
/
Accordion.tsx
62 lines (55 loc) · 1.98 KB
/
Accordion.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React from 'react';
import { Accordion as DesignSystemAccordion } from '@digdir/designsystemet-react';
import { Grid } from '@material-ui/core';
import { useLanguage } from 'src/features/language/useLanguage';
import classes from 'src/layout/Accordion/Accordion.module.css';
import { AccordionItem as AltinnAcordionItem } from 'src/layout/Accordion/AccordionItem';
import { useIsInAccordionGroup } from 'src/layout/AccordionGroup/AccordionGroupContext';
import { ComponentStructureWrapper } from 'src/layout/ComponentStructureWrapper';
import { GenericComponent } from 'src/layout/GenericComponent';
import { useNodeItem } from 'src/utils/layout/useNodeItem';
import type { PropsFromGenericComponent } from 'src/layout';
type IAccordionProps = PropsFromGenericComponent<'Accordion'>;
export const Accordion = ({ node }: IAccordionProps) => {
const { textResourceBindings, headingLevel, childComponents, openByDefault } = useNodeItem(node);
const { langAsString } = useLanguage();
const renderAsAccordionItem = useIsInAccordionGroup();
const title = langAsString(textResourceBindings?.title ?? '');
const AccordionItem = ({ className }: { className?: string }) => (
<AltinnAcordionItem
title={title}
className={className}
headingLevel={headingLevel}
open={openByDefault}
>
<Grid
item={true}
container={true}
spacing={6}
alignItems='flex-start'
>
{childComponents.map((node) => (
<GenericComponent
key={node.id}
node={node}
/>
))}
</Grid>
</AltinnAcordionItem>
);
return (
<ComponentStructureWrapper node={node}>
{renderAsAccordionItem ? (
<AccordionItem className={classes.container} />
) : (
<DesignSystemAccordion
color='subtle'
border
className={classes.container}
>
<AccordionItem />
</DesignSystemAccordion>
)}
</ComponentStructureWrapper>
);
};