Skip to content

Commit 4345ab6

Browse files
committed
[Refactor] Renamed suggestion text property and moved the logic to get suggestion text to own function
1 parent 0327018 commit 4345ab6

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

AutoComplete/AutoComplete.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class AutoComplete extends Component {
1616
)
1717
}
1818

19+
getSuggestionText = (suggestion) => {
20+
if (this.props.suggestionObjectTextProperty) {
21+
return suggestion[this.props.suggestionObjectTextProperty]
22+
}
23+
24+
return suggestion
25+
}
1926

2027
isSimilar = (value, suggestionText) => {
2128
const minimumScore = this.props.minimumSimilarityScore
@@ -35,12 +42,7 @@ class AutoComplete extends Component {
3542
}
3643

3744
return newSuggestions.reduce((suggestions, suggestion) => {
38-
let suggestionText
39-
if (this.props.textProperty) {
40-
suggestionText = suggestion[this.props.textProperty]
41-
} else {
42-
suggestionText = suggestion
43-
}
45+
const suggestionText = this.getSuggestionText(suggestion)
4446

4547
if (!suggestionText || !this.isSimilar(value, suggestionText)) {
4648
return suggestions
@@ -118,9 +120,9 @@ AutoComplete.propTypes = {
118120
value: React.PropTypes.string,
119121
minimumSimilarityScore: React.PropTypes.number,
120122
comparationFuzziness: React.PropTypes.number,
121-
textProperty: React.PropTypes.string,
123+
suggestionObjectTextProperty: React.PropTypes.string,
122124
onChangeText: React.PropTypes.func,
123-
onSelect: React.PropTypes.func,
125+
onSelect: React.PropTypes.func.isRequired,
124126
suggestionsWrapperStyle: React.PropTypes.any,
125127
suggestionStyle: React.PropTypes.any,
126128
suggestionTextStyle: React.PropTypes.any,

AutoComplete/AutoComplete.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('AutoComplete', () => {
2727
const params = {
2828
value: '0.5',
2929
suggestions: fakeSuggestions,
30-
textProperty: 'text'
30+
suggestionObjectTextProperty: 'text'
3131
}
3232

3333
const wrapper = shallow(
@@ -47,7 +47,7 @@ describe('AutoComplete', () => {
4747
let params = {
4848
value: '0.5',
4949
suggestions: fakeSuggestions,
50-
textProperty: 'text'
50+
suggestionObjectTextProperty: 'text'
5151
}
5252

5353
const wrapper = shallow(
@@ -70,7 +70,7 @@ describe('AutoComplete', () => {
7070
const params = {
7171
value: '0.5',
7272
suggestions: fakeSuggestions,
73-
textProperty: 'text',
73+
suggestionObjectTextProperty: 'text',
7474
onSelect: jest.fn()
7575
}
7676

AutoComplete/__snapshots__/AutoComplete.spec.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ ShallowWrapper {
5555
"_reactInternalInstance": [Circular],
5656
"context": Object {},
5757
"filterSugestions": [Function],
58+
"getSuggestionText": [Function],
5859
"isSimilar": [Function],
5960
"onChangeText": [Function],
6061
"props": Object {

0 commit comments

Comments
 (0)