Skip to content

Commit 9e9982b

Browse files
committed
feat: renderButtonText prop.
1 parent 608c6e0 commit 9e9982b

File tree

7 files changed

+76
-70
lines changed

7 files changed

+76
-70
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
.idea
77

88
npm-debug.log
9+
example/package-lock.json

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ You can find them in the example.
1919

2020
## Update History
2121

22-
### v0.5.0
23-
- React 16 compatibility.
24-
- Fix [#84](https://github.com/sohobloo/react-native-modal-dropdown/issues/84)
22+
### v0.6.0
23+
- Introduce `renderButtonText` property to extract button text from object array data.
2524

2625
[Full update history list](https://github.com/sohobloo/react-native-modal-dropdown/wiki/Update-History)
2726

@@ -73,6 +72,7 @@ Prop | Type | Optional | Default | Description
7372
`adjustFrame` | func | Yes | | This is a callback after the frame of the dropdown have been calculated and before showing. You will receive a style object as argument with some of the props like `width` `height` `top` `left` and `right`. Change them to appropriate values that accord with your requirement and **make the new style as the return value of this function**.
7473
`renderRow` | func | Yes | | Customize render option rows. **Will render a default row if `null`/`undefined`.**
7574
`renderSeparator` | func | Yes | | Customize render dropdown list separators. **Will render a default thin gray line if `null`/`undefined`.**
75+
`renderButtonText` | func | Yes | | Use this to extract and return text from option object. This text will show on button after option selected. **Invalid in wrapper mode.**
7676
`onDropdownWillShow`| func | Yes | | Trigger when dropdown will show by touching the button. **Return `false` can cancel the event.**
7777
`onDropdownWillHide`| func | Yes | | Trigger when dropdown will hide by touching the button. **Return `false` can cancel the event.**
7878
`onSelect` | func | Yes | | Trigger when option row touched with selected `index` and `value`. **Return `false` can cancel the event.**

components/ModalDropdown.js

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default class ModalDropdown extends Component {
4848
adjustFrame: PropTypes.func,
4949
renderRow: PropTypes.func,
5050
renderSeparator: PropTypes.func,
51+
renderButtonText: PropTypes.func,
5152

5253
onDropdownWillShow: PropTypes.func,
5354
onDropdownWillHide: PropTypes.func,
@@ -84,8 +85,8 @@ export default class ModalDropdown extends Component {
8485
}
8586

8687
componentWillReceiveProps(nextProps) {
87-
var buttonText = this._nextValue == null ? this.state.buttonText : this._nextValue.toString();
88-
var selectedIndex = this._nextIndex == null ? this.state.selectedIndex : this._nextIndex;
88+
let buttonText = this._nextValue == null ? this.state.buttonText : this._nextValue.toString();
89+
let selectedIndex = this._nextIndex == null ? this.state.selectedIndex : this._nextIndex;
8990
if (selectedIndex < 0) {
9091
selectedIndex = nextProps.defaultIndex;
9192
if (selectedIndex < 0) {
@@ -136,7 +137,7 @@ export default class ModalDropdown extends Component {
136137
}
137138

138139
select(idx) {
139-
var value = this.props.defaultValue;
140+
let value = this.props.defaultValue;
140141
if (idx == null || this.props.options == null || idx >= this.props.options.length) {
141142
idx = this.props.defaultIndex;
142143
}
@@ -184,8 +185,8 @@ export default class ModalDropdown extends Component {
184185

185186
_renderModal() {
186187
if (this.state.showDropdown && this._buttonFrame) {
187-
let frameStyle = this._calcPosition();
188-
let animationType = this.props.animated ? 'fade' : 'none';
188+
const frameStyle = this._calcPosition();
189+
const animationType = this.props.animated ? 'fade' : 'none';
189190
return (
190191
<Modal animationType={animationType}
191192
visible={true}
@@ -207,27 +208,27 @@ export default class ModalDropdown extends Component {
207208
}
208209

209210
_calcPosition() {
210-
let dimensions = Dimensions.get('window');
211-
let windowWidth = dimensions.width;
212-
let windowHeight = dimensions.height;
211+
const dimensions = Dimensions.get('window');
212+
const windowWidth = dimensions.width;
213+
const windowHeight = dimensions.height;
213214

214-
let dropdownHeight = (this.props.dropdownStyle && StyleSheet.flatten(this.props.dropdownStyle).height) ||
215+
const dropdownHeight = (this.props.dropdownStyle && StyleSheet.flatten(this.props.dropdownStyle).height) ||
215216
StyleSheet.flatten(styles.dropdown).height;
216217

217-
let bottomSpace = windowHeight - this._buttonFrame.y - this._buttonFrame.h;
218-
let rightSpace = windowWidth - this._buttonFrame.x;
219-
let showInBottom = bottomSpace >= dropdownHeight || bottomSpace >= this._buttonFrame.y;
220-
let showInLeft = rightSpace >= this._buttonFrame.x;
218+
const bottomSpace = windowHeight - this._buttonFrame.y - this._buttonFrame.h;
219+
const rightSpace = windowWidth - this._buttonFrame.x;
220+
const showInBottom = bottomSpace >= dropdownHeight || bottomSpace >= this._buttonFrame.y;
221+
const showInLeft = rightSpace >= this._buttonFrame.x;
221222

222-
var style = {
223+
let style = {
223224
height: dropdownHeight,
224225
top: showInBottom ? this._buttonFrame.y + this._buttonFrame.h : Math.max(0, this._buttonFrame.y - dropdownHeight),
225226
};
226227

227228
if (showInLeft) {
228229
style.left = this._buttonFrame.x;
229230
} else {
230-
let dropdownWidth = (this.props.dropdownStyle && StyleSheet.flatten(this.props.dropdownStyle).width) ||
231+
const dropdownWidth = (this.props.dropdownStyle && StyleSheet.flatten(this.props.dropdownStyle).width) ||
231232
(this.props.style && StyleSheet.flatten(this.props.style).width) || -1;
232233
if (dropdownWidth !== -1) {
233234
style.width = dropdownWidth;
@@ -277,16 +278,16 @@ export default class ModalDropdown extends Component {
277278
}
278279

279280
get _dataSource() {
280-
let ds = new ListView.DataSource({
281+
const ds = new ListView.DataSource({
281282
rowHasChanged: (r1, r2) => r1 !== r2
282283
});
283284
return ds.cloneWithRows(this.props.options);
284285
}
285286

286287
_renderRow(rowData, sectionID, rowID, highlightRow) {
287-
let key = `row_${rowID}`;
288-
let highlighted = rowID == this.state.selectedIndex;
289-
let row = !this.props.renderRow ?
288+
const key = `row_${rowID}`;
289+
const highlighted = rowID == this.state.selectedIndex;
290+
const row = !this.props.renderRow ?
290291
(<Text style={[
291292
styles.rowText,
292293
this.props.dropdownTextStyle,
@@ -297,13 +298,13 @@ export default class ModalDropdown extends Component {
297298
{rowData}
298299
</Text>) :
299300
this.props.renderRow(rowData, rowID, highlighted);
300-
let preservedProps = {
301+
const preservedProps = {
301302
key: key,
302303
accessible: this.props.accessible,
303304
onPress: () => this._onRowPress(rowData, sectionID, rowID, highlightRow),
304305
};
305306
if (TOUCHABLE_ELEMENTS.find(name => name == row.type.displayName)) {
306-
var props = {...row.props};
307+
const props = {...row.props};
307308
props.key = preservedProps.key;
308309
props.onPress = preservedProps.onPress;
309310
switch (row.type.displayName) {
@@ -315,7 +316,6 @@ export default class ModalDropdown extends Component {
315316
</TouchableHighlight>
316317
);
317318
}
318-
break;
319319
case 'TouchableOpacity':
320320
{
321321
return (
@@ -324,7 +324,6 @@ export default class ModalDropdown extends Component {
324324
</TouchableOpacity>
325325
);
326326
}
327-
break;
328327
case 'TouchableWithoutFeedback':
329328
{
330329
return (
@@ -333,7 +332,6 @@ export default class ModalDropdown extends Component {
333332
</TouchableWithoutFeedback>
334333
);
335334
}
336-
break;
337335
case 'TouchableNativeFeedback':
338336
{
339337
return (
@@ -342,7 +340,6 @@ export default class ModalDropdown extends Component {
342340
</TouchableNativeFeedback>
343341
);
344342
}
345-
break;
346343
default:
347344
break;
348345
}
@@ -355,26 +352,25 @@ export default class ModalDropdown extends Component {
355352
}
356353

357354
_onRowPress(rowData, sectionID, rowID, highlightRow) {
358-
if (!this.props.onSelect ||
359-
this.props.onSelect(rowID, rowData) !== false) {
355+
const { onSelect, renderButtonText, onDropdownWillHide } = this.props;
356+
if (!onSelect || onSelect(rowID, rowData) !== false) {
360357
highlightRow(sectionID, rowID);
361358
this._nextValue = rowData;
362359
this._nextIndex = rowID;
363360
this.setState({
364-
buttonText: rowData.toString(),
361+
buttonText: renderButtonText && renderButtonText(rowData) || rowData.toString(),
365362
selectedIndex: rowID
366363
});
367364
}
368-
if (!this.props.onDropdownWillHide ||
369-
this.props.onDropdownWillHide() !== false) {
365+
if (!onDropdownWillHide || onDropdownWillHide() !== false) {
370366
this.setState({
371367
showDropdown: false
372368
});
373369
}
374370
}
375371

376372
_renderSeparator(sectionID, rowID, adjacentRowHighlighted) {
377-
let key = `spr_${rowID}`;
373+
const key = `spr_${rowID}`;
378374
return (<View style={styles.separator}
379375
key={key}
380376
/>);

0 commit comments

Comments
 (0)