Skip to content

Commit 9d53ae1

Browse files
authored
support render top section at expandableSection (#983)
* support render top section at expandableSection * add top open example in expandableSectionScreen
1 parent 2738154 commit 9d53ae1

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

demo/src/screens/componentScreens/ExpandableSectionScreen.tsx

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from 'lodash';
22
import React, {PureComponent} from 'react';
3-
import {ScrollView, StyleSheet, TouchableOpacity} from 'react-native';
4-
import {Card, Text, Image, ListItem, Carousel, Spacings, View, ExpandableSection} from 'react-native-ui-lib';
3+
import {ScrollView, StyleSheet} from 'react-native';
4+
import {Card, Text, Image, ListItem, Carousel, Spacings, View, ExpandableSection, Switch} from 'react-native-ui-lib';
55

66
const cardImage2 = require('../../assets/images/empty-state.jpg');
77
const cardImage = require('../../assets/images/card-example.jpg');
@@ -43,7 +43,8 @@ const elements = [
4343

4444
class ExpandableSectionScreen extends PureComponent {
4545
state = {
46-
expanded: false
46+
expanded: false,
47+
top: false
4748
};
4849

4950
onExpand() {
@@ -52,14 +53,21 @@ class ExpandableSectionScreen extends PureComponent {
5253
});
5354
}
5455

56+
getChevron() {
57+
if (this.state.expanded) {
58+
return this.state.top ? chevronDown : chevronUp;
59+
}
60+
return this.state.top ? chevronUp : chevronDown;
61+
}
62+
5563
getHeaderElement() {
5664
return (
5765
<View>
5866
<Text margin-10 dark10 text60>
5967
ExpandableSection's sectionHeader
6068
</Text>
6169
<View style={styles.header}>
62-
<Image style={styles.icon} source={!this.state.expanded ? chevronUp : chevronDown} />
70+
<Image style={styles.icon} source={this.getChevron()} />
6371
</View>
6472
</View>
6573
);
@@ -80,11 +88,27 @@ class ExpandableSectionScreen extends PureComponent {
8088
}
8189

8290
render() {
83-
const {expanded} = this.state;
91+
const {expanded, top} = this.state;
8492

8593
return (
8694
<ScrollView>
87-
<ExpandableSection expanded={expanded} sectionHeader={this.getHeaderElement()} onPress={() => this.onExpand()}>
95+
<View row center margin-20>
96+
<Text dark10 text70 marginR-10>
97+
Open section on top
98+
</Text>
99+
<Switch
100+
value={this.state.top}
101+
onValueChange={() => {
102+
this.setState({top: !this.state.top});
103+
}}
104+
></Switch>
105+
</View>
106+
<ExpandableSection
107+
top={top}
108+
expanded={expanded}
109+
sectionHeader={this.getHeaderElement()}
110+
onPress={() => this.onExpand()}
111+
>
88112
{this.getBodyElement()}
89113
</ExpandableSection>
90114
<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+
* should the expandableSection open above the sectionHeader
17+
*/
18+
top?: boolean;
1519
/**
1620
* action for when pressing the header of the expandableSection
1721
*/

src/components/expandableSection/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,29 @@ export type ExpandableSectionProps = {
1717
* should the expandableSection be expanded
1818
*/
1919
expanded?: boolean;
20+
/**
21+
* should the expandableSection open above the sectionHeader
22+
*/
23+
top?: boolean;
2024
/**
2125
* action for when pressing the header of the expandableSection
2226
*/
2327
onPress?: () => void;
2428
};
2529

2630
function ExpandableSection(props: ExpandableSectionProps) {
27-
const {expanded, sectionHeader, children} = props;
31+
const {expanded, sectionHeader, children, top} = props;
2832

2933
const onPress = () => {
3034
props.onPress?.();
3135
LayoutAnimation.configureNext({...LayoutAnimation.Presets.easeInEaseOut, duration: 300});
3236
}
3337

3438
return (
35-
<View style={styles.container}>
39+
<View style={styles.container}>
40+
{top && expanded && children}
3641
<TouchableOpacity onPress={onPress}>{sectionHeader}</TouchableOpacity>
37-
{expanded && children}
42+
{!top && expanded && children}
3843
</View>
3944
);
4045
}

0 commit comments

Comments
 (0)