Skip to content

Commit bc09462

Browse files
authored
Fix/expandable section animation (#1798)
* screen fixes * animate on any expanded change * use useDidUpdate instaed of useEffect
1 parent 361407f commit bc09462

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

demo/src/screens/componentScreens/ExpandableSectionScreen.tsx

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,45 @@ const cardImage = require('../../assets/images/card-example.jpg');
88
const chevronDown = require('../../assets/icons/chevronDown.png');
99
const chevronUp = require('../../assets/icons/chevronUp.png');
1010

11-
const elements = [
12-
<Card style={{marginBottom: 10}} onPress={() => {}}>
13-
<Card.Section
14-
content={[
15-
{text: 'Card #1', text70: true, grey10: true},
16-
{text: 'card description', text90: true, grey50: true}
17-
]}
18-
style={{padding: 20}}
19-
/>
20-
<Card.Section source={cardImage2} imageStyle={{height: 120}} />
21-
</Card>,
22-
<Card style={{marginBottom: 10}} onPress={() => {}}>
23-
<Card.Section
24-
content={[
25-
{text: 'Card #2', text70: true, grey10: true},
26-
{text: 'card description', text90: true, grey50: true}
27-
]}
28-
style={{padding: 20}}
29-
/>
30-
<Card.Section source={cardImage} imageStyle={{height: 120}} />
31-
</Card>,
32-
<Card style={{marginBottom: 10}} onPress={() => {}}>
33-
<Card.Section
34-
content={[
35-
{text: 'Card #3', text70: true, grey10: true},
36-
{text: 'card description', text90: true, grey50: true}
37-
]}
38-
style={{padding: 20}}
39-
/>
40-
<Card.Section source={cardImage2} imageStyle={{height: 120}} />
41-
</Card>
42-
];
43-
4411
class ExpandableSectionScreen extends PureComponent {
4512
state = {
4613
expanded: false,
4714
top: false
4815
};
4916

17+
elements = [
18+
<Card key={0} style={{marginBottom: 10}} onPress={() => this.onExpand()}>
19+
<Card.Section
20+
content={[
21+
{text: 'Card #1', text70: true, grey10: true},
22+
{text: 'card description', text90: true, grey50: true}
23+
]}
24+
style={{padding: 20}}
25+
/>
26+
<Card.Section imageSource={cardImage2} imageStyle={{height: 120}}/>
27+
</Card>,
28+
<Card key={1} style={{marginBottom: 10}} onPress={() => this.onExpand()}>
29+
<Card.Section
30+
content={[
31+
{text: 'Card #2', text70: true, grey10: true},
32+
{text: 'card description', text90: true, grey50: true}
33+
]}
34+
style={{padding: 20}}
35+
/>
36+
<Card.Section imageSource={cardImage} imageStyle={{height: 120}}/>
37+
</Card>,
38+
<Card key={2} style={{marginBottom: 10}} onPress={() => this.onExpand()}>
39+
<Card.Section
40+
content={[
41+
{text: 'Card #3', text70: true, grey10: true},
42+
{text: 'card description', text90: true, grey50: true}
43+
]}
44+
style={{padding: 20}}
45+
/>
46+
<Card.Section imageSource={cardImage2} imageStyle={{height: 120}}/>
47+
</Card>
48+
];
49+
5050
onExpand() {
5151
this.setState({
5252
expanded: !this.state.expanded
@@ -74,7 +74,7 @@ class ExpandableSectionScreen extends PureComponent {
7474
getBodyElement() {
7575
return (
7676
<Carousel pageWidth={350} itemSpacings={Spacings.s2}>
77-
{_.map(elements, (element, key) => {
77+
{_.map(this.elements, (element, key) => {
7878
return (
7979
<View key={key} margin-12>
8080
{element}
@@ -99,7 +99,7 @@ class ExpandableSectionScreen extends PureComponent {
9999
onValueChange={() => {
100100
this.setState({top: !this.state.top});
101101
}}
102-
></Switch>
102+
/>
103103
</View>
104104
<ExpandableSection
105105
top={top}

src/components/expandableSection/index.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import {LayoutAnimation, StyleSheet} from 'react-native';
33
import View from '../view';
44
import TouchableOpacity from '../touchableOpacity';
5+
import {useDidUpdate} from 'hooks';
56

67
export type ExpandableSectionProps = {
78
/**
@@ -35,11 +36,23 @@ export type ExpandableSectionProps = {
3536
function ExpandableSection(props: ExpandableSectionProps) {
3637
const {expanded, sectionHeader, children, top} = props;
3738

39+
/**
40+
* TODO: move to reanimated LayoutAnimation after updating to version 2.3.0
41+
* after migration, trigger the animation only in useDidUpdate.
42+
*/
43+
const animate = () => {
44+
LayoutAnimation.configureNext({...LayoutAnimation.Presets.easeInEaseOut, duration: 300});
45+
};
46+
3847
const onPress = () => {
3948
props.onPress?.();
40-
LayoutAnimation.configureNext({...LayoutAnimation.Presets.easeInEaseOut, duration: 300});
49+
animate();
4150
};
4251

52+
useDidUpdate(() => {
53+
animate();
54+
}, [expanded]);
55+
4356
return (
4457
<View style={styles.container}>
4558
{top && expanded && children}

0 commit comments

Comments
 (0)