Skip to content

Commit 4c83788

Browse files
authored
Fix/layout animation in expandable section (#977)
* add onPress prop to expandableSection * typing
1 parent ad27cd5 commit 4c83788

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

demo/src/screens/componentScreens/ExpandableSectionScreen.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ class ExpandableSectionScreen extends PureComponent {
5454

5555
getHeaderElement() {
5656
return (
57-
<TouchableOpacity onPress={() => this.onExpand()}>
57+
<View>
5858
<Text margin-10 dark10 text60>
5959
ExpandableSection's sectionHeader
6060
</Text>
6161
<View style={styles.header}>
6262
<Image style={styles.icon} source={!this.state.expanded ? chevronUp : chevronDown} />
6363
</View>
64-
</TouchableOpacity>
64+
</View>
6565
);
6666
}
6767

@@ -84,7 +84,7 @@ class ExpandableSectionScreen extends PureComponent {
8484

8585
return (
8686
<ScrollView>
87-
<ExpandableSection expanded={expanded} sectionHeader={this.getHeaderElement()}>
87+
<ExpandableSection expanded={expanded} sectionHeader={this.getHeaderElement()} onPress={() => this.onExpand()}>
8888
{this.getBodyElement()}
8989
</ExpandableSection>
9090
<ListItem>

generatedTypes/components/expandableSection/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export declare type ExpandableSectionProps = {
1212
* should the expandableSection be expanded
1313
*/
1414
expanded?: boolean;
15+
/**
16+
* action for when pressing the header of the expandableSection
17+
*/
18+
onPress?: () => void;
1519
};
1620
declare function ExpandableSection(props: ExpandableSectionProps): JSX.Element;
1721
export default ExpandableSection;

src/components/expandableSection/index.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import _ from 'lodash';
2-
import React, {useEffect} from 'react';
2+
import React from 'react';
33
import {LayoutAnimation, StyleSheet} from 'react-native';
44
import View from '../view';
5+
import TouchableOpacity from '../touchableOpacity'
56

67
export type ExpandableSectionProps = {
78
/**
@@ -16,19 +17,23 @@ export type ExpandableSectionProps = {
1617
* should the expandableSection be expanded
1718
*/
1819
expanded?: boolean;
20+
/**
21+
* action for when pressing the header of the expandableSection
22+
*/
23+
onPress?: () => void;
1924
};
2025

2126
function ExpandableSection(props: ExpandableSectionProps) {
2227
const {expanded, sectionHeader, children} = props;
2328

24-
useEffect(() => {
25-
LayoutAnimation.configureNext({...LayoutAnimation.Presets.easeInEaseOut, duration: 300});
26-
}, [expanded]);
27-
29+
const onPress = () => {
30+
props.onPress?.();
31+
LayoutAnimation.configureNext({...LayoutAnimation.Presets.easeInEaseOut, duration: 300});
32+
}
2833

2934
return (
3035
<View style={styles.container}>
31-
{sectionHeader}
36+
<TouchableOpacity onPress={onPress}>{sectionHeader}</TouchableOpacity>
3237
{expanded && children}
3338
</View>
3439
);

0 commit comments

Comments
 (0)