Skip to content

Commit 4c52c4b

Browse files
committed
AgendaList performent
1 parent f53d6eb commit 4c52c4b

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

example/src/screens/expandableCalendar.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ function getFutureDates(days) {
2828
}
2929

3030
function getPastDate(days) {
31-
return new Date(Date.now() - (864e5 * days)).toISOString().split('T')[0]; // 864e5 == 86400000 == 24*60*60*1000
31+
return new Date(Date.now() - (864e5 * days)).toISOString().split('T')[0];
3232
}
3333

34-
const items = [
34+
const ITEMS = [
3535
{title: dates[0], data: [{hour: '12am', duration: '1h', title: 'Ashtanga Yoga'}]},
3636
{title: dates[1], data: [{hour: '4pm', duration: '1h', title: 'Pilates ABC'}, {hour: '5pm', duration: '1h', title: 'Vinyasa Yoga'}]},
3737
{title: dates[2], data: [{hour: '1pm', duration: '1h', title: 'Ashtanga Yoga'}, {hour: '2pm', duration: '1h', title: 'Deep Streches'}, {hour: '3pm', duration: '1h', title: 'Private Yoga'}]},
@@ -46,11 +46,19 @@ const items = [
4646
];
4747

4848
export default class ExpandableCalendarScreen extends Component {
49-
49+
5050
onDateChanged = (/**date, updateSource*/) => {
5151
// console.warn('ExpandableCalendarScreen onDateChanged: ', date, updateSource);
5252
// fetch and set data for date + week ahead
5353
}
54+
55+
buttonPressed() {
56+
Alert.alert('show more');
57+
}
58+
59+
itemPressed(id) {
60+
Alert.alert(id);
61+
}
5462

5563
renderEmptyItem() {
5664
return (
@@ -64,36 +72,27 @@ export default class ExpandableCalendarScreen extends Component {
6472
if (_.isEmpty(item)) {
6573
return this.renderEmptyItem();
6674
}
67-
68-
const id = item.title;
69-
const props = {
70-
hour: item.hour,
71-
duration: item.duration,
72-
title: item.title,
73-
button: {label: 'info', onPress: () => Alert.alert('show more')},
74-
onPress: () => Alert.alert(id)
75-
};
76-
75+
7776
return (
7877
<TouchableOpacity
79-
onPress={props.onPress}
78+
onPress={() => this.itemPressed(item.title)}
8079
style={styles.item}
8180
>
8281
<View>
83-
<Text style={styles.itemHourText}>{props.hour}</Text>
84-
<Text style={styles.itemDurationText}>{props.duration}</Text>
82+
<Text style={styles.itemHourText}>{item.hour}</Text>
83+
<Text style={styles.itemDurationText}>{item.duration}</Text>
8584
</View>
86-
<Text style={styles.itemTitleText}>{props.title}</Text>
85+
<Text style={styles.itemTitleText}>{item.title}</Text>
8786
<View style={styles.itemButtonContainer}>
88-
<Button title={props.button.label} onPress={props.button.onPress}/>
87+
<Button title={'Info'} onPress={this.buttonPressed}/>
8988
</View>
9089
</TouchableOpacity>
9190
);
9291
}
9392

9493
getMarkedDates = () => {
9594
const marked = {};
96-
items.forEach(item => {
95+
ITEMS.forEach(item => {
9796
// only mark dates with data
9897
if (item.data && item.data.length > 0 && !_.isEmpty(item.data[0])) {
9998
marked[item.title] = {marked: true};
@@ -146,10 +145,8 @@ export default class ExpandableCalendarScreen extends Component {
146145
}
147146

148147
render() {
149-
const style = {paddingLeft: 20, paddingRight: 20};
150-
151148
return (
152-
<CalendarProvider date={items[0].title} onDateChanged={this.onDateChanged} theme={{todayButtonTextColor: '#0059ff'}} showTodayButton>
149+
<CalendarProvider date={ITEMS[0].title} onDateChanged={this.onDateChanged} theme={{todayButtonTextColor: '#0059ff'}} showTodayButton>
153150
<ExpandableCalendar
154151
// horizontal={false}
155152
// hideArrows
@@ -158,24 +155,33 @@ export default class ExpandableCalendarScreen extends Component {
158155
// initialPosition={'open'} // ExpandableCalendar.positions.OPEN - can't find static positions
159156
firstDay={1}
160157
markedDates={this.getMarkedDates()} // {'2019-06-01': {marked: true}, '2019-06-02': {marked: true}, '2019-06-03': {marked: true}};
161-
calendarStyle={style}
158+
calendarStyle={styles.calendar}
162159
theme={this.getTheme()}
163160
leftArrowImageSource={require('../img/previous.png')}
164161
rightArrowImageSource={require('../img/next.png')}
165-
headerStyle={style}
162+
headerStyle={styles.calendar}
166163
/>
167164
<AgendaList
168-
data={items}
169-
sections={items}
165+
data={ITEMS}
166+
sections={ITEMS}
167+
extraData={this.state}
170168
renderItem={this.renderItem}
171-
// sectionStyle={{backgroundColor: '#f0f4f7', color: '#79838a'}}
169+
// sectionStyle={styles.section}
172170
/>
173171
</CalendarProvider>
174172
);
175173
}
176174
}
177175

178176
const styles = StyleSheet.create({
177+
calendar: {
178+
paddingLeft: 20,
179+
paddingRight: 20
180+
},
181+
section: {
182+
backgroundColor: '#f0f4f7',
183+
color: '#79838a'
184+
},
179185
item: {
180186
padding: 20,
181187
backgroundColor: 'white',

src/expandableCalendar/agendaList.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class AgendaList extends Component {
3131
this._topSection = _.get(props, 'sections[0].title');
3232
this.didScroll = false;
3333
this.sectionScroll = false;
34+
35+
this.viewabilityConfig = {
36+
itemVisiblePercentThreshold: 20 // 50 means if 50% of the item is visible
37+
};
38+
this.list = React.createRef();
3439
}
3540

3641
getSectionIndex(date) {
@@ -57,11 +62,11 @@ class AgendaList extends Component {
5762
}
5863

5964
scrollToSection(sectionIndex) {
60-
if (this.list && sectionIndex !== undefined) {
65+
if (this.list.current && sectionIndex !== undefined) {
6166
this.sectionScroll = true; // to avoid setDate() in onViewableItemsChanged
6267
this._topSection = this.props.sections[sectionIndex].title;
6368

64-
this.list.scrollToLocation({
69+
this.list.current.scrollToLocation({
6570
animated: true,
6671
sectionIndex: sectionIndex,
6772
itemIndex: 0,
@@ -118,18 +123,18 @@ class AgendaList extends Component {
118123
);
119124
}
120125

126+
keyExtractor = (item, index) => String(index);
127+
121128
render() {
122129
return (
123130
<SectionList
124131
{...this.props}
125-
ref={r => this.list = r}
126-
keyExtractor={(item, index) => String(index)}
132+
ref={this.list}
133+
keyExtractor={this.keyExtractor}
127134
showsVerticalScrollIndicator={false}
128135
stickySectionHeadersEnabled
129136
onViewableItemsChanged={this.onViewableItemsChanged}
130-
viewabilityConfig={{
131-
itemVisiblePercentThreshold: 20 // 50 means if 50% of the item is visible
132-
}}
137+
viewabilityConfig={this.viewabilityConfig}
133138
renderSectionHeader={this.renderSectionHeader}
134139
onScroll={this.onScroll}
135140
onMomentumScrollBegin={this.onMomentumScrollBegin}

0 commit comments

Comments
 (0)