This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "chore: Convert Components A-D to TS (WIP) (#138)"
This reverts commit 62d4420.
- Loading branch information
1 parent
62d4420
commit 5d0b4cc
Showing
39 changed files
with
383 additions
and
486 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { css } from 'styled-components'; | ||
import Collapse from '../Collapse'; | ||
import { createComponent } from '../utils'; | ||
import Box from '../Box'; | ||
import Icon from '../Icon'; | ||
import Flex from '../Flex'; | ||
|
||
const AccordionItemProps = { | ||
title: PropTypes.oneOfType([PropTypes.element, PropTypes.string]), | ||
content: PropTypes.oneOfType([PropTypes.element, PropTypes.string]), | ||
renderHeader: PropTypes.func, | ||
renderContent: PropTypes.func, | ||
}; | ||
|
||
const AccordionContainer = createComponent({ | ||
name: 'Accordion', | ||
}); | ||
|
||
const AccordionItemContainer = createComponent({ | ||
name: 'AccordionItem', | ||
}); | ||
|
||
const AccordionHeader = createComponent({ | ||
name: 'AccordionItemHeader', | ||
tag: 'header', | ||
style: css` | ||
padding: 0.75rem 1rem; | ||
cursor: pointer; | ||
`, | ||
}); | ||
|
||
const AccordionTitle = createComponent({ | ||
name: 'AccordionItemTitle', | ||
tag: 'span', | ||
style: css``, | ||
}); | ||
|
||
const AccordionIcon = createComponent({ | ||
name: 'AccordionItemIcon', | ||
as: Icon, | ||
style: ({ isOpen }) => css` | ||
transition: 175ms transform; | ||
transform: rotate(${isOpen ? 90 : 0}deg); | ||
`, | ||
}); | ||
|
||
const AccordionContent = createComponent({ | ||
name: 'AccordionItemContent', | ||
}); | ||
|
||
const AccordionItem = ({ isOpen, title, content, contentContainerStyle, renderHeader, renderContent, onToggle }) => ( | ||
<AccordionItemContainer> | ||
<Collapse | ||
isOpen={isOpen} | ||
renderTrigger={() => | ||
renderHeader ? ( | ||
renderHeader({ isOpen, title, onToggle }) | ||
) : ( | ||
<AccordionHeader onClick={onToggle}> | ||
<Flex> | ||
<Box flex={1}> | ||
<AccordionTitle>{title}</AccordionTitle> | ||
</Box> | ||
<AccordionIcon name="chevron-right" isOpen={isOpen} /> | ||
</Flex> | ||
</AccordionHeader> | ||
) | ||
}> | ||
{renderContent ? ( | ||
renderContent({ isOpen, content }) | ||
) : ( | ||
<AccordionContent style={contentContainerStyle}>{content}</AccordionContent> | ||
)} | ||
</Collapse> | ||
</AccordionItemContainer> | ||
); | ||
|
||
AccordionItem.propTypes = AccordionItemProps; | ||
|
||
export default class Accordion extends Component { | ||
static propTypes = { | ||
/** | ||
* An array of AccordionItems | ||
*/ | ||
items: PropTypes.arrayOf(PropTypes.shape(AccordionItemProps)).isRequired, | ||
|
||
/** | ||
* Only one accordion cell open at a time | ||
*/ | ||
solo: PropTypes.bool, | ||
|
||
/** | ||
* Style passed to content container box | ||
*/ | ||
contentContainerStyle: PropTypes.shape(), | ||
}; | ||
|
||
static Item = AccordionItem; | ||
|
||
state = { | ||
openList: [], | ||
}; | ||
|
||
handleItemToggle = idx => { | ||
const { solo } = this.props; | ||
this.setState(({ openList }) => { | ||
if (openList.indexOf(idx) >= 0) { | ||
return { openList: openList.filter(i => i !== idx) }; | ||
} | ||
|
||
return { | ||
openList: solo ? [idx] : [...openList, idx], | ||
}; | ||
}); | ||
}; | ||
|
||
render() { | ||
const { items, children, contentContainerStyle } = this.props; | ||
const { openList } = this.state; | ||
|
||
return ( | ||
<AccordionContainer> | ||
{children || | ||
items.map((item, i) => ( | ||
<AccordionItem | ||
key={item.title} | ||
contentContainerStyle={contentContainerStyle} | ||
isOpen={openList.indexOf(i) >= 0} | ||
{...item} | ||
onToggle={() => this.handleItemToggle(i)} | ||
/> | ||
))} | ||
</AccordionContainer> | ||
); | ||
} | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.