Skip to content

Commit 652edfe

Browse files
committed
Replaced ScrollView with FlatList
1 parent b2a5b9d commit 652edfe

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

src/components/dropdown/index.js

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { PureComponent } from 'react';
33
import {
44
Text,
55
View,
6-
ScrollView,
6+
FlatList,
77
Animated,
88
Modal,
99
TouchableWithoutFeedback,
@@ -149,10 +149,15 @@ export default class Dropdown extends PureComponent {
149149
this.onClose = this.onClose.bind(this);
150150
this.onSelect = this.onSelect.bind(this);
151151
this.onLayout = this.onLayout.bind(this);
152+
152153
this.updateRippleRef = this.updateRef.bind(this, 'ripple');
153154
this.updateContainerRef = this.updateRef.bind(this, 'container');
154155
this.updateScrollRef = this.updateRef.bind(this, 'scroll');
156+
155157
this.renderAccessory = this.renderAccessory.bind(this);
158+
this.renderItem = this.renderItem.bind(this);
159+
160+
this.keyExtractor = this.keyExtractor.bind(this);
156161

157162
this.blur = this.onClose;
158163
this.focus = this.onPress;
@@ -306,7 +311,7 @@ export default class Dropdown extends PureComponent {
306311
setTimeout((() => {
307312
if (this.mounted) {
308313
if (this.scroll) {
309-
this.scroll.scrollTo({ x: 0, y: offset, animated: false });
314+
this.scroll.scrollToOffset({ offset, animated: false });
310315
}
311316

312317
Animated
@@ -426,6 +431,12 @@ export default class Dropdown extends PureComponent {
426431
this[name] = ref;
427432
}
428433

434+
keyExtractor(item, index) {
435+
let { valueExtractor } = this.props;
436+
437+
return `${index}-${valueExtractor(item, index)}`;
438+
}
439+
429440
renderBase(props) {
430441
let { value } = this.state;
431442
let {
@@ -512,11 +523,10 @@ export default class Dropdown extends PureComponent {
512523
);
513524
}
514525

515-
renderItems() {
526+
renderItem({ item, index }) {
516527
let { selected, leftInset, rightInset } = this.state;
517528

518529
let {
519-
data,
520530
valueExtractor,
521531
labelExtractor,
522532
textColor,
@@ -544,35 +554,32 @@ export default class Dropdown extends PureComponent {
544554
},
545555
};
546556

547-
return data
548-
.map((item, index) => {
549-
if (null == item) {
550-
return null;
551-
}
557+
if (null == item) {
558+
return null;
559+
}
552560

553-
let value = valueExtractor(item, index);
554-
let label = labelExtractor(item, index);
561+
let value = valueExtractor(item, index);
562+
let label = labelExtractor(item, index);
555563

556-
let title = null == label?
557-
value:
558-
label;
564+
let title = null == label?
565+
value:
566+
label;
559567

560-
let color = ~selected?
561-
index === selected?
562-
selectedItemColor:
563-
itemColor:
564-
selectedItemColor;
568+
let color = ~selected?
569+
index === selected?
570+
selectedItemColor:
571+
itemColor:
572+
selectedItemColor;
565573

566-
let style = { color, fontSize };
574+
let style = { color, fontSize };
567575

568-
return (
569-
<DropdownItem index={index} key={index} {...props}>
570-
<Text style={[styles.item, itemTextStyle, style]} numberOfLines={1}>
571-
{title}
572-
</Text>
573-
</DropdownItem>
574-
);
575-
});
576+
return (
577+
<DropdownItem index={index} {...props}>
578+
<Text style={[styles.item, itemTextStyle, style]} numberOfLines={1}>
579+
{title}
580+
</Text>
581+
</DropdownItem>
582+
);
576583
}
577584

578585
render() {
@@ -687,14 +694,15 @@ export default class Dropdown extends PureComponent {
687694
<Animated.View
688695
style={[styles.picker, pickerStyle, pickerStyleOverrides]}
689696
>
690-
<ScrollView
697+
<FlatList
691698
ref={this.updateScrollRef}
699+
data={data}
692700
style={styles.scroll}
701+
renderItem={this.renderItem}
702+
keyExtractor={this.keyExtractor}
693703
scrollEnabled={visibleItemCount < itemCount}
694704
contentContainerStyle={styles.scrollContainer}
695-
>
696-
{this.renderItems()}
697-
</ScrollView>
705+
/>
698706
</Animated.View>
699707
</View>
700708
</TouchableWithoutFeedback>

0 commit comments

Comments
 (0)