|
| 1 | +import React, { useState, PropsWithChildren } from 'react'; |
| 2 | +import styled from 'styled-components'; |
| 3 | + |
| 4 | +import { Colors } from '../../../essentials'; |
| 5 | +import { Text } from '../../Text/Text'; |
| 6 | +import { Box } from '../../Box/Box'; |
| 7 | +import { Headline } from '../../Headline/Headline'; |
| 8 | +import { Header } from './Header'; |
| 9 | +import { ChevronUp } from './ChevronUp'; |
| 10 | +import { ChevronDown } from './ChevronDown'; |
| 11 | +import { Description } from './Description'; |
| 12 | +import { AccordionProps } from '../types'; |
| 13 | + |
| 14 | +const ButtonLabel = styled(Text).attrs({ as: 'p' })` |
| 15 | + color: ${Colors.ACTION_BLUE_900}; |
| 16 | +`; |
| 17 | + |
| 18 | +const PanelHeader = styled(Header)` |
| 19 | + &:hover { |
| 20 | + background-color: ${Colors.ACTION_BLUE_50}; |
| 21 | + } |
| 22 | +
|
| 23 | + &:hover ${ButtonLabel} { |
| 24 | + color: ${Colors.ACTION_BLUE_1000}; |
| 25 | + } |
| 26 | +
|
| 27 | + &:hover ${ChevronDown} { |
| 28 | + color: ${Colors.ACTION_BLUE_1000}; |
| 29 | + } |
| 30 | +`; |
| 31 | + |
| 32 | +const CardHeader = styled(Header).attrs({ p: '3' })` |
| 33 | + background-color: ${Colors.AUTHENTIC_BLUE_50}; |
| 34 | + border-radius: 0.3125rem 0.3125rem 0 0; |
| 35 | +
|
| 36 | + &:hover { |
| 37 | + background-color: ${Colors.ACTION_BLUE_50}; |
| 38 | + } |
| 39 | +
|
| 40 | + &:hover ${ButtonLabel} { |
| 41 | + color: ${Colors.ACTION_BLUE_1000}; |
| 42 | + } |
| 43 | +
|
| 44 | + &:hover ${ChevronUp} { |
| 45 | + color: ${Colors.ACTION_BLUE_1000}; |
| 46 | + } |
| 47 | +`; |
| 48 | + |
| 49 | +const PanelBody = styled(Box).attrs({ my: '3' })` |
| 50 | + border: solid 0.0625rem ${Colors.AUTHENTIC_BLUE_200}; |
| 51 | + border-radius: 0.3125rem; |
| 52 | +`; |
| 53 | + |
| 54 | +const PanelIcon = ({ isOpen }: { isOpen: boolean }) => |
| 55 | + isOpen ? <ChevronUp color={Colors.ACTION_BLUE_900} /> : <ChevronDown color={Colors.ACTION_BLUE_900} />; |
| 56 | + |
| 57 | +export const DefaultPanel = ({ |
| 58 | + heading, |
| 59 | + description, |
| 60 | + info, |
| 61 | + buttonLabel, |
| 62 | + defaultExpanded = false, |
| 63 | + children, |
| 64 | + onExpand, |
| 65 | + onCollapse |
| 66 | +}: PropsWithChildren<AccordionProps>) => { |
| 67 | + const [isOpen, setIsOpen] = useState<boolean>(defaultExpanded); |
| 68 | + |
| 69 | + return ( |
| 70 | + <> |
| 71 | + {isOpen ? ( |
| 72 | + <PanelBody> |
| 73 | + <CardHeader |
| 74 | + onClick={() => { |
| 75 | + setIsOpen(!isOpen); |
| 76 | + onCollapse(); |
| 77 | + }} |
| 78 | + > |
| 79 | + <Box display="flex" flexDirection="column" maxWidth="33%"> |
| 80 | + <Headline as="h4" mr="3"> |
| 81 | + {heading} |
| 82 | + </Headline> |
| 83 | + <Description mt="1" description={description} /> |
| 84 | + </Box> |
| 85 | + <Box ml="3" display="flex" flexDirection="row"> |
| 86 | + <ButtonLabel>{buttonLabel}</ButtonLabel> |
| 87 | + <PanelIcon isOpen={isOpen} /> |
| 88 | + </Box> |
| 89 | + </CardHeader> |
| 90 | + <Box m="3">{children}</Box> |
| 91 | + </PanelBody> |
| 92 | + ) : ( |
| 93 | + <PanelHeader |
| 94 | + onClick={() => { |
| 95 | + setIsOpen(!isOpen); |
| 96 | + onExpand(); |
| 97 | + }} |
| 98 | + > |
| 99 | + <Headline as="h4" mr="3"> |
| 100 | + {heading} |
| 101 | + </Headline> |
| 102 | + <Box> |
| 103 | + <Description description={description} /> |
| 104 | + <Text as="p" style={{ textOverflow: 'ellipsis', overflow: 'hidden', whiteSpace: 'nowrap' }}> |
| 105 | + {info} |
| 106 | + </Text> |
| 107 | + </Box> |
| 108 | + <Box ml="3" display="flex" flexDirection="row"> |
| 109 | + <ButtonLabel>{buttonLabel}</ButtonLabel> |
| 110 | + <PanelIcon isOpen={isOpen} /> |
| 111 | + </Box> |
| 112 | + </PanelHeader> |
| 113 | + )} |
| 114 | + </> |
| 115 | + ); |
| 116 | +}; |
0 commit comments