Skip to content

Commit de62d20

Browse files
author
Amirul Abu
committed
Testing pull request sohobloo#219
1 parent 4d7bd5e commit de62d20

File tree

1 file changed

+120
-125
lines changed

1 file changed

+120
-125
lines changed

components/ModalDropdown.js

+120-125
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import {
1313
Dimensions,
1414
View,
1515
Text,
16+
ListView,
17+
FlatList,
18+
SectionList,
1619
TouchableWithoutFeedback,
1720
TouchableNativeFeedback,
1821
TouchableOpacity,
@@ -21,8 +24,6 @@ import {
2124
ActivityIndicator,
2225
} from 'react-native';
2326

24-
import { ListView } from 'deprecated-react-native-listview';
25-
2627
import PropTypes from 'prop-types';
2728

2829
const TOUCHABLE_ELEMENTS = [
@@ -89,9 +90,9 @@ export default class ModalDropdown extends Component {
8990
};
9091
}
9192

92-
componentWillReceiveProps(nextProps) {
93+
componentDidUpate(prevProps, prevState){
9394
let {buttonText, selectedIndex} = this.state;
94-
const {defaultIndex, defaultValue, options} = nextProps;
95+
const {defaultIndex, defaultValue, options} = this.props;
9596
buttonText = this._nextValue == null ? buttonText : this._nextValue;
9697
selectedIndex = this._nextIndex == null ? selectedIndex : this._nextIndex;
9798
if (selectedIndex < 0) {
@@ -111,6 +112,7 @@ export default class ModalDropdown extends Component {
111112
}
112113

113114
render() {
115+
114116
return (
115117
<View {...this.props}>
116118
{this._renderButton()}
@@ -119,6 +121,116 @@ export default class ModalDropdown extends Component {
119121
);
120122
}
121123

124+
_renderModal = () => {
125+
const {animated, accessible, dropdownStyle} = this.props;
126+
const {showDropdown, loading} = this.state;
127+
if (showDropdown && this._buttonFrame) {
128+
const frameStyle = this._calcPosition();
129+
const animationType = animated ? 'fade' : 'none';
130+
return (
131+
<Modal animationType={animationType}
132+
visible={true}
133+
transparent={true}
134+
onRequestClose={this._onRequestClose}
135+
supportedOrientations={['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']}
136+
>
137+
<TouchableWithoutFeedback accessible={accessible}
138+
disabled={!showDropdown}
139+
onPress={this._onModalPress}
140+
>
141+
<View style={styles.modal}>
142+
<View style={[styles.dropdown, dropdownStyle, frameStyle]}>
143+
{loading ? this._renderLoading() : this._renderDropdown()}
144+
</View>
145+
</View>
146+
</TouchableWithoutFeedback>
147+
</Modal>
148+
);
149+
}
150+
}
151+
152+
_renderDropdown = () => {
153+
const {scrollEnabled, renderSeparator, showsVerticalScrollIndicator, keyboardShouldPersistTaps} = this.props;
154+
return (
155+
<FlatList
156+
scrollEnabled={scrollEnabled}
157+
style={styles.list}
158+
keyExtractor={(_, index) => index.toString()}
159+
renderItem = {({item, index}) => this._renderRow(item, index)}
160+
data={this.props.options}
161+
renderSeparator={renderSeparator || this._renderSeparator}
162+
automaticallyAdjustContentInsets={false}
163+
showsVerticalScrollIndicator={showsVerticalScrollIndicator}
164+
/>
165+
);
166+
}
167+
168+
_renderRow = (item, index) => {
169+
const rowData = item
170+
const rowID = index
171+
const {renderRow, dropdownTextStyle, dropdownTextHighlightStyle, accessible} = this.props;
172+
const {selectedIndex} = this.state;
173+
const key = `row_${rowID}`;
174+
const highlighted = rowID == selectedIndex;
175+
const row = !renderRow ?
176+
(<Text style={[
177+
styles.rowText,
178+
dropdownTextStyle,
179+
]}
180+
>
181+
{rowData}
182+
</Text>) :
183+
renderRow(rowData, rowID, highlighted);
184+
const preservedProps = {
185+
key,
186+
accessible,
187+
onPress: () => this._onRowPress(rowData, rowID),
188+
};
189+
if (TOUCHABLE_ELEMENTS.find(name => name == row.type.displayName)) {
190+
const props = {...row.props};
191+
props.key = preservedProps.key;
192+
props.onPress = preservedProps.onPress;
193+
const {children} = row.props;
194+
switch (row.type.displayName) {
195+
case 'TouchableHighlight': {
196+
return (
197+
<TouchableHighlight {...props}>
198+
{children}
199+
</TouchableHighlight>
200+
);
201+
}
202+
case 'TouchableOpacity': {
203+
return (
204+
<TouchableOpacity {...props}>
205+
{children}
206+
</TouchableOpacity>
207+
);
208+
}
209+
case 'TouchableWithoutFeedback': {
210+
return (
211+
<TouchableWithoutFeedback {...props}>
212+
{children}
213+
</TouchableWithoutFeedback>
214+
);
215+
}
216+
case 'TouchableNativeFeedback': {
217+
return (
218+
<TouchableNativeFeedback {...props}>
219+
{children}
220+
</TouchableNativeFeedback>
221+
);
222+
}
223+
default:
224+
break;
225+
}
226+
}
227+
return (
228+
<TouchableHighlight {...preservedProps}>
229+
{row}
230+
</TouchableHighlight>
231+
);
232+
};
233+
122234
_updatePosition(callback) {
123235
if (this._button && this._button.measure) {
124236
this._button.measure((fx, fy, width, height, px, py) => {
@@ -197,34 +309,6 @@ export default class ModalDropdown extends Component {
197309
}
198310
};
199311

200-
_renderModal() {
201-
const {animated, accessible, dropdownStyle} = this.props;
202-
const {showDropdown, loading} = this.state;
203-
if (showDropdown && this._buttonFrame) {
204-
const frameStyle = this._calcPosition();
205-
const animationType = animated ? 'fade' : 'none';
206-
return (
207-
<Modal animationType={animationType}
208-
visible={true}
209-
transparent={true}
210-
onRequestClose={this._onRequestClose}
211-
supportedOrientations={['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']}
212-
>
213-
<TouchableWithoutFeedback accessible={accessible}
214-
disabled={!showDropdown}
215-
onPress={this._onModalPress}
216-
>
217-
<View style={styles.modal}>
218-
<View style={[styles.dropdown, dropdownStyle, frameStyle]}>
219-
{loading ? this._renderLoading() : this._renderDropdown()}
220-
</View>
221-
</View>
222-
</TouchableWithoutFeedback>
223-
</Modal>
224-
);
225-
}
226-
}
227-
228312
_calcPosition() {
229313
const {dropdownStyle, style, adjustFrame} = this.props;
230314

@@ -281,99 +365,10 @@ export default class ModalDropdown extends Component {
281365
);
282366
}
283367

284-
_renderDropdown() {
285-
const {scrollEnabled, renderSeparator, showsVerticalScrollIndicator, keyboardShouldPersistTaps} = this.props;
286-
return (
287-
<ListView scrollEnabled={scrollEnabled}
288-
style={styles.list}
289-
dataSource={this._dataSource}
290-
renderRow={this._renderRow}
291-
renderSeparator={renderSeparator || this._renderSeparator}
292-
automaticallyAdjustContentInsets={false}
293-
showsVerticalScrollIndicator={showsVerticalScrollIndicator}
294-
keyboardShouldPersistTaps={keyboardShouldPersistTaps}
295-
/>
296-
);
297-
}
298-
299-
get _dataSource() {
300-
const {options} = this.props;
301-
const ds = new ListView.DataSource({
302-
rowHasChanged: (r1, r2) => r1 !== r2
303-
});
304-
return ds.cloneWithRows(options);
305-
}
306-
307-
_renderRow = (rowData, sectionID, rowID, highlightRow) => {
308-
const {renderRow, dropdownTextStyle, dropdownTextHighlightStyle, accessible} = this.props;
309-
const {selectedIndex} = this.state;
310-
const key = `row_${rowID}`;
311-
const highlighted = rowID == selectedIndex;
312-
const row = !renderRow ?
313-
(<Text style={[
314-
styles.rowText,
315-
dropdownTextStyle,
316-
highlighted && styles.highlightedRowText,
317-
highlighted && dropdownTextHighlightStyle
318-
]}
319-
>
320-
{rowData}
321-
</Text>) :
322-
renderRow(rowData, rowID, highlighted);
323-
const preservedProps = {
324-
key,
325-
accessible,
326-
onPress: () => this._onRowPress(rowData, sectionID, rowID, highlightRow),
327-
};
328-
if (TOUCHABLE_ELEMENTS.find(name => name == row.type.displayName)) {
329-
const props = {...row.props};
330-
props.key = preservedProps.key;
331-
props.onPress = preservedProps.onPress;
332-
const {children} = row.props;
333-
switch (row.type.displayName) {
334-
case 'TouchableHighlight': {
335-
return (
336-
<TouchableHighlight {...props}>
337-
{children}
338-
</TouchableHighlight>
339-
);
340-
}
341-
case 'TouchableOpacity': {
342-
return (
343-
<TouchableOpacity {...props}>
344-
{children}
345-
</TouchableOpacity>
346-
);
347-
}
348-
case 'TouchableWithoutFeedback': {
349-
return (
350-
<TouchableWithoutFeedback {...props}>
351-
{children}
352-
</TouchableWithoutFeedback>
353-
);
354-
}
355-
case 'TouchableNativeFeedback': {
356-
return (
357-
<TouchableNativeFeedback {...props}>
358-
{children}
359-
</TouchableNativeFeedback>
360-
);
361-
}
362-
default:
363-
break;
364-
}
365-
}
366-
return (
367-
<TouchableHighlight {...preservedProps}>
368-
{row}
369-
</TouchableHighlight>
370-
);
371-
};
372-
373-
_onRowPress(rowData, sectionID, rowID, highlightRow) {
368+
_onRowPress(rowData, rowID) {
374369
const {onSelect, renderButtonText, onDropdownWillHide} = this.props;
375370
if (!onSelect || onSelect(rowID, rowData) !== false) {
376-
highlightRow(sectionID, rowID);
371+
// highlightRow();
377372
const value = renderButtonText && renderButtonText(rowData) || rowData.toString();
378373
this._nextValue = value;
379374
this._nextIndex = rowID;
@@ -389,7 +384,7 @@ export default class ModalDropdown extends Component {
389384
}
390385
}
391386

392-
_renderSeparator = (sectionID, rowID, adjacentRowHighlighted) => {
387+
_renderSeparator = (rowID, adjacentRowHighlighted) => {
393388
const key = `spr_${rowID}`;
394389
return (
395390
<View style={styles.separator}
@@ -439,4 +434,4 @@ const styles = StyleSheet.create({
439434
height: StyleSheet.hairlineWidth,
440435
backgroundColor: 'lightgray'
441436
}
442-
});
437+
});

0 commit comments

Comments
 (0)