-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
Accordion.js
160 lines (143 loc) · 4.06 KB
/
Accordion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React, { Component } from 'react';
import { View, TouchableHighlight, FlatList } from 'react-native';
import Collapsible from './Collapsible';
const COLLAPSIBLE_PROPS = [
'align',
'collapsed',
'collapsedHeight',
'renderChildrenCollapsed',
'enablePointerEvents',
'duration',
'easing',
'style',
'onAnimationEnd',
];
export default class Accordion extends Component {
static defaultProps = {
underlayColor: 'black',
disabled: false,
expandFromBottom: false,
expandMultiple: false,
touchableComponent: TouchableHighlight,
keyExtractor: (item, index) => index,
renderSectionTitle: () => null,
onAnimationEnd: () => null,
sectionContainerStyle: {},
renderAsFlatList: false,
};
_toggleSection(section) {
if (!this.props.disabled) {
const { activeSections, expandMultiple, onChange } = this.props;
let updatedSections = [];
if (activeSections.includes(section)) {
updatedSections = activeSections.filter((a) => a !== section);
} else if (expandMultiple) {
updatedSections = [...activeSections, section];
} else {
updatedSections = [section];
}
if (onChange) {
onChange(updatedSections);
}
}
}
_renderContainer = (section, key, renderCollapsible) => {
const {
activeSections,
sectionContainerStyle,
expandFromBottom,
sections,
underlayColor,
touchableProps,
touchableComponent: Touchable,
renderHeader,
renderFooter,
renderSectionTitle,
} = this.props;
return (
<View key={key} style={sectionContainerStyle}>
{renderSectionTitle(section, key, activeSections.includes(key))}
{expandFromBottom && renderCollapsible(section, key)}
<Touchable
onPress={() => this._toggleSection(key)}
underlayColor={underlayColor}
{...touchableProps}
accessibilityState={{
expanded: activeSections.includes(key),
}}
>
{renderHeader(section, key, activeSections.includes(key), sections)}
</Touchable>
{!expandFromBottom && renderCollapsible(section, key)}
{renderFooter &&
renderFooter(section, key, activeSections.includes(key), sections)}
</View>
);
};
render() {
const {
activeSections,
expandMultiple,
onChange,
containerStyle,
sectionContainerStyle,
expandFromBottom,
sections,
underlayColor,
touchableProps,
touchableComponent: Touchable,
onAnimationEnd,
renderContent,
renderHeader,
renderFooter,
renderSectionTitle,
disabled,
renderAsFlatList,
keyExtractor,
...restProps
} = this.props;
const viewProps = {};
const collapsibleProps = {};
Object.keys(restProps).forEach((key) => {
if (COLLAPSIBLE_PROPS.includes(key)) {
collapsibleProps[key] = restProps[key];
} else {
viewProps[key] = restProps[key];
}
});
const renderCollapsible = (section, key) => (
<Collapsible
collapsed={!activeSections.includes(key)}
{...collapsibleProps}
onAnimationEnd={() => onAnimationEnd(section, key)}
>
{renderContent(section, key, activeSections.includes(key), sections)}
</Collapsible>
);
if (renderAsFlatList) {
return (
<FlatList
style={containerStyle}
data={sections}
extraData={activeSections}
nestedScrollEnabled={true}
keyExtractor={keyExtractor}
renderItem={({ item, index }) => {
const section = item;
const key = keyExtractor(item, index);
return this._renderContainer(section, key, renderCollapsible);
}}
{...viewProps}
/>
);
}
return (
<View style={containerStyle} {...viewProps}>
{sections.map((section, index) => {
const key = keyExtractor(section, index);
return this._renderContainer(section, key, renderCollapsible);
})}
</View>
);
}
}