forked from react-native-elements/react-native-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added New Sub Component: ListItem Accordion (react-native-elements#2953)
* Added Accordion * Improved * Added Docs * Added Prop * Fixed * Fixed
- Loading branch information
1 parent
d76f602
commit 19566ea
Showing
6 changed files
with
233 additions
and
0 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
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,103 @@ | ||
import React from 'react'; | ||
import { Animated } from 'react-native'; | ||
import { ListItem, ListItemProps } from './ListItem'; | ||
import { withTheme } from '../config'; | ||
import { Icon, IconNode, IconProps } from '../icons/Icon'; | ||
import { RneFunctionComponent } from '../helpers'; | ||
|
||
export type ListItemAccordionProps = ListItemProps & { | ||
isExpanded?: boolean; | ||
icon?: IconNode; | ||
expandIcon?: IconNode; | ||
content?: React.ReactNode; | ||
noAnimation?: boolean; | ||
noRotation?: boolean; | ||
noIcon?: boolean; | ||
animationDuration?: number; | ||
}; | ||
|
||
const Accordion: RneFunctionComponent<ListItemAccordionProps> = ({ | ||
children, | ||
isExpanded, | ||
icon, | ||
expandIcon, | ||
content, | ||
noAnimation, | ||
noRotation, | ||
noIcon, | ||
animationDuration = 350, | ||
...props | ||
}) => { | ||
const { current: animation } = React.useRef(new Animated.Value(0)); | ||
|
||
const startAnimation = React.useCallback(() => { | ||
Animated.timing(animation, { | ||
toValue: Number(isExpanded), | ||
useNativeDriver: false, | ||
duration: animationDuration, | ||
}).start(); | ||
}, [isExpanded, animation, animationDuration]); | ||
|
||
React.useEffect(() => { | ||
if (noAnimation) { | ||
return; | ||
} | ||
startAnimation(); | ||
}, [isExpanded, startAnimation, noAnimation]); | ||
|
||
const rotate = noRotation | ||
? '0deg' | ||
: animation.interpolate({ | ||
inputRange: [0, 1], | ||
outputRange: ['0deg', '-180deg'], | ||
}); | ||
|
||
return ( | ||
<> | ||
<ListItem {...props}> | ||
{React.isValidElement(content) ? content : <ListItem.Content />} | ||
{!noIcon && ( | ||
<Animated.View | ||
style={{ | ||
transform: [ | ||
{ | ||
rotate, | ||
}, | ||
], | ||
}} | ||
> | ||
{icon ? ( | ||
<Icon | ||
{...((expandIcon | ||
? isExpanded | ||
? expandIcon | ||
: icon | ||
: icon) as IconProps)} | ||
/> | ||
) : ( | ||
<Icon name={'chevron-down'} type="material-community" /> | ||
)} | ||
</Animated.View> | ||
)} | ||
</ListItem> | ||
<Animated.View | ||
style={[ | ||
{ | ||
maxHeight: animation.interpolate({ | ||
inputRange: [0, 1], | ||
outputRange: ['0%', '100%'], | ||
}), | ||
opacity: animation, | ||
}, | ||
noAnimation && { | ||
maxHeight: isExpanded ? '100%' : '0%', | ||
}, | ||
]} | ||
> | ||
{children} | ||
</Animated.View> | ||
</> | ||
); | ||
}; | ||
|
||
export default withTheme(Accordion, 'ListItem.Accordion'); |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.