-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
58 lines (50 loc) · 1.68 KB
/
index.js
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
import React, {useContext, createContext, useState} from 'react';
import {
Container,
Frame,
Inner,
Header,
Title,
Item,
Body
} from './styles/accordion';
const ToggleContext = createContext();
export default function Accordion({ children, ...restProps }) {
return (
<Container {...restProps}>
<Inner>{children}</Inner>
</Container>
);
};
Accordion.Title = function AccordionTitle({children, ...restProps}) {
return <Title {...restProps}>{children}</Title>;
};
Accordion.Frame = function AccordionFrame({children, ...restProps}) {
return <Frame {...restProps}>{children}</Frame>;
};
Accordion.Item = function AccordionItem({children, ...restProps}) {
const [toggleShow, setToggleShow] = useState(false);
return (
<ToggleContext.Provider value={{ toggleShow, setToggleShow}}>
<Item {...restProps}>{children}</Item>
</ToggleContext.Provider>
);
};
Accordion.Header = function AccordionHeader({children, ...restProps}) {
const { toggleShow, setToggleShow } = useContext(ToggleContext);
return (
<Header onClick={()=> setToggleShow((toggleShow) => !toggleShow)} {...restProps}>
{children}
{/* <pre>{JSON.stringify(toggleShow, null, 2)}</pre> */}
{toggleShow ? (
<img src="/images/icons/close-slim.png" alt="Close" />
) : (
<img src="/images/icons/add.png" alt="Open" />
)}
</Header>
);
};
Accordion.Body = function AccordionBody({children, ...restProps}) {
const {toggleShow} = useContext(ToggleContext);
return toggleShow ? <Body {...restProps}>{children}</Body> : null;
}