Skip to content

Commit

Permalink
Merge pull request #65 from kesha-antonov/migrate_to_flatlist
Browse files Browse the repository at this point in the history
Migrate to flatlist
  • Loading branch information
Javier-Rotelli authored Apr 8, 2019
2 parents 4a78d5a + 75e5304 commit ee99242
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ node_modules/
npm-debug.log
coverage
.nyc_output

package-lock.json
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ render() {
data={data}
defaultValue={query}
onChangeText={text => this.setState({ query: text })}
renderItem={item => (

renderItem={({ item, i }) => (
<TouchableOpacity onPress={() => this.setState({ query: item })}>
<Text>{item}</Text>
</TouchableOpacity>
Expand Down Expand Up @@ -85,15 +86,17 @@ const styles = StyleSheet.create({
| :------------ |:---------------:| :-----|
| containerStyle | style | These styles will be applied to the container which surrounds the autocomplete component. |
| hideResults | bool | Set to `true` to hide the suggestion list.
| data | array | An array with suggestion items to be rendered in `renderItem(item)`. Any array with length > 0 will open the suggestion list and any array with length < 1 will hide the list. |
| data | array | An array with suggestion items to be rendered in `renderItem({ item, i })`. Any array with length > 0 will open the suggestion list and any array with length < 1 will hide the list. |
| inputContainerStyle | style | These styles will be applied to the container which surrounds the textInput component. |
| listContainerStyle | style | These styles will be applied to the container which surrounds the result list. |
| listStyle | style | These style will be applied to the result list. |
| onShowResult | function | `onShowResult` will be called when the autocomplete suggestions appear or disappear. |
| onStartShouldSetResponderCapture | function | `onStartShouldSetResponderCapture` will be passed to the result list view container ([onStartShouldSetResponderCapture](https://facebook.github.io/react-native/docs/gesture-responder-system.html#capture-shouldset-handlers)). |
| renderItem | function | `renderItem` will be called to render the data objects which will be displayed in the result view below the text input. |
| keyExtractor | function | `keyExtractor(item, i)` will be called to get key for each item. It's up to you which string to return as a key. |
| renderSeparator | function | `renderSeparator` will be called to render the list separators which will be displayed between the list elements in the result view below the text input. |
| renderTextInput | function | render custom TextInput. All props passed to this function. |
| flatListProps | object | custom props to FlatList[](https://facebook.github.io/react-native/docs/flatlist.html)]. |

## Known issues
* By default the autocomplete will not behave as expected inside a `<ScrollView />`. Set the scroll view's prop to fix this: `keyboardShouldPersistTaps={true}` for RN <= 0.39, or `keyboardShouldPersistTaps='always'` for RN >= 0.40. ([#5](https://github.com/mrlaessig/react-native-autocomplete-input/issues/5)).
Expand Down
6 changes: 3 additions & 3 deletions __tests__/autocomplete-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ describe('<AutocompleteInput />', () => {
/>
);

const customInput = autocomplete.find('Text')
const customInput = autocomplete.find('Text');
expect(autocomplete.find('TextInput')).to.have.length(0);
expect(customInput.children().get(0)).to.equal(text);
expect(customInput.prop('foo')).to.equal('bar');
})
});

it('should render default <TextInput /> if no custom one is supplied', () => {
const autocomplete = shallow(<Autocomplete data={[]} foo="bar" />);

const textInput = autocomplete.childAt(0).children().first();
expect(textInput.name()).to.equal('TextInput');
expect(textInput.prop('foo')).to.equal('bar');
})
});
});
60 changes: 38 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
ListView,
FlatList,
Platform,
StyleSheet,
Text,
Expand Down Expand Up @@ -65,6 +65,7 @@ class Autocomplete extends Component {
* text input.
*/
renderItem: PropTypes.func,
keyExtractor: PropTypes.func,
/**
* `renderSeparator` will be called to render the list separators
* which will be displayed between the list elements in the result view
Expand All @@ -75,34 +76,45 @@ class Autocomplete extends Component {
* renders custom TextInput. All props passed to this function.
*/
renderTextInput: PropTypes.func,
/**
* `rowHasChanged` will be used for data objects comparison for dataSource
*/
rowHasChanged: PropTypes.func
flatListProps: PropTypes.object
};

static defaultProps = {
data: [],
defaultValue: '',
keyboardShouldPersistTaps: 'always',
onStartShouldSetResponderCapture: () => false,
renderItem: rowData => <Text>{rowData}</Text>,
renderItem: ({ item }) => <Text>{item}</Text>,
renderSeparator: null,
renderTextInput: props => <TextInput {...props} />,
rowHasChanged: (r1, r2) => r1 !== r2
flatListProps: {}
};

constructor(props) {
super(props);

const ds = new ListView.DataSource({ rowHasChanged: props.rowHasChanged });
this.state = { dataSource: ds.cloneWithRows(props.data) };
this.state = { data: props.data };
this.resultList = null;
this.textInput = null;

this.onRefListView = this.onRefListView.bind(this);
this.onRefTextInput = this.onRefTextInput.bind(this);
this.onEndEditing = this.onEndEditing.bind(this);
}

componentWillReceiveProps({ data }) {
const dataSource = this.state.dataSource.cloneWithRows(data);
this.setState({ dataSource });
this.setState({ data });
}

onEndEditing(e) {
this.props.onEndEditing && this.props.onEndEditing(e);
}

onRefListView(resultList) {
this.resultList = resultList;
}

onRefTextInput(textInput) {
this.textInput = textInput;
}

/**
Expand All @@ -122,44 +134,48 @@ class Autocomplete extends Component {
}

renderResultList() {
const { dataSource } = this.state;
const { data } = this.state;
const {
listStyle,
renderItem,
keyExtractor,
renderSeparator,
keyboardShouldPersistTaps,
flatListProps,
onEndReached,
onEndReachedThreshold
} = this.props;

return (
<ListView
ref={(resultList) => { this.resultList = resultList; }}
dataSource={dataSource}
<FlatList
ref={this.onRefListView}
data={data}
keyboardShouldPersistTaps={keyboardShouldPersistTaps}
renderRow={renderItem}
renderItem={renderItem}
keyExtractor={keyExtractor}
renderSeparator={renderSeparator}
onEndReached={onEndReached}
onEndReachedThreshold={onEndReachedThreshold}
style={[styles.list, listStyle]}
{...flatListProps}
/>
);
}

renderTextInput() {
const { onEndEditing, renderTextInput, style } = this.props;
const { renderTextInput, style } = this.props;
const props = {
style: [styles.input, style],
ref: ref => (this.textInput = ref),
onEndEditing: e => onEndEditing && onEndEditing(e),
ref: this.onRefTextInput,
onEndEditing: this.onEndEditing,
...this.props
};

return renderTextInput(props);
}

render() {
const { dataSource } = this.state;
const { data } = this.state;
const {
containerStyle,
hideResults,
Expand All @@ -168,7 +184,7 @@ class Autocomplete extends Component {
onShowResults,
onStartShouldSetResponderCapture
} = this.props;
const showResults = dataSource.getRowCount() > 0;
const showResults = data.length > 0;

// Notify listener if the suggestion will be shown.
onShowResults && onShowResults(showResults);
Expand Down
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
"url": "https://github.com/l-urence/react-native-autocomplete-input/issues"
},
"homepage": "https://github.com/l-urence/react-native-autocomplete-input#readme",
"dependencies": {
"prop-types": "^15.5.10"
},
"dependencies": {},
"devDependencies": {
"babel-eslint": "^7.1.1",
"babel-preset-react-native": "^1.9.1",
Expand All @@ -52,9 +50,9 @@
"estraverse-fb": "^1.3.1",
"mocha": "^3.2.0",
"nyc": "^10.1.2",
"react": "~15.4.0-rc.4",
"react-addons-test-utils": "^15.2.1",
"react-dom": "^15.2.1",
"react": "~15.5.4",
"react-addons-test-utils": "^15.6.2",
"react-dom": "~15.5.4",
"react-native": "^0.40.0",
"react-native-mock": "^0.3.1",
"sinon": "^1.17.4"
Expand Down
84 changes: 77 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,19 @@ fbjs@^0.8.1, fbjs@^0.8.4, fbjs@^0.8.5:
setimmediate "^1.0.5"
ua-parser-js "^0.7.9"

fbjs@^0.8.9:
version "0.8.17"
resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.17.tgz#c4d598ead6949112653d6588b01a5cdcd9f90fdd"
integrity sha1-xNWY6taUkRJlPWWIsBpc3Nn5D90=
dependencies:
core-js "^1.0.0"
isomorphic-fetch "^2.1.1"
loose-envify "^1.0.0"
object-assign "^4.1.0"
promise "^7.1.1"
setimmediate "^1.0.5"
ua-parser-js "^0.7.18"

figures@^1.3.5:
version "1.7.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-1.7.0.tgz#cbe1e3affcf1cd44b80cadfed28dc793a9701d2e"
Expand Down Expand Up @@ -2423,6 +2436,11 @@ js-tokens@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.0.tgz#a2f2a969caae142fb3cd56228358c89366957bd1"

"js-tokens@^3.0.0 || ^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==

js-yaml@^3.5.1:
version "3.7.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.7.0.tgz#5c967ddd837a9bfdca5f2de84253abe8a1c03b80"
Expand Down Expand Up @@ -2736,6 +2754,13 @@ loose-envify@^1.0.0, loose-envify@^1.1.0:
dependencies:
js-tokens "^3.0.0"

loose-envify@^1.3.1, loose-envify@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
dependencies:
js-tokens "^3.0.0 || ^4.0.0"

lru-cache@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.0.2.tgz#1d17679c069cda5d040991a09dbc2c0db377e55e"
Expand Down Expand Up @@ -3016,9 +3041,10 @@ object-assign@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-3.0.0.tgz#9bedd5ca0897949bca47e7ff408062d549f587f2"

object-assign@^4.0.1, object-assign@^4.1.0:
object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=

object-is@^1.0.1:
version "1.0.1"
Expand Down Expand Up @@ -3249,6 +3275,23 @@ promise@^7.1.1:
dependencies:
asap "~2.0.3"

prop-types@^15.5.7:
version "15.7.2"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5"
integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==
dependencies:
loose-envify "^1.4.0"
object-assign "^4.1.1"
react-is "^16.8.1"

prop-types@~15.5.7:
version "15.5.10"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.5.10.tgz#2797dfc3126182e3a95e3dfbb2e893ddd7456154"
integrity sha1-J5ffwxJhguOpXj37suiT3ddFYVQ=
dependencies:
fbjs "^0.8.9"
loose-envify "^1.3.1"

prr@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a"
Expand Down Expand Up @@ -3319,13 +3362,18 @@ react-addons-pure-render-mixin@^15.4.0:
fbjs "^0.8.4"
object-assign "^4.1.0"

react-addons-test-utils@^15.2.1, react-addons-test-utils@^15.4.0:
react-addons-test-utils@^15.4.0:
version "15.4.2"
resolved "https://registry.yarnpkg.com/react-addons-test-utils/-/react-addons-test-utils-15.4.2.tgz#93bcaa718fcae7360d42e8fb1c09756cc36302a2"
dependencies:
fbjs "^0.8.4"
object-assign "^4.1.0"

react-addons-test-utils@^15.6.2:
version "15.6.2"
resolved "https://registry.yarnpkg.com/react-addons-test-utils/-/react-addons-test-utils-15.6.2.tgz#c12b6efdc2247c10da7b8770d185080a7b047156"
integrity sha1-wStu/cIkfBDae4dw0YUICnsEcVY=

react-addons-update@^15.4.0:
version "15.4.2"
resolved "https://registry.yarnpkg.com/react-addons-update/-/react-addons-update-15.4.2.tgz#43a9578e3b46bc5f7eed2ba52560dd57a8aff918"
Expand All @@ -3341,14 +3389,29 @@ react-deep-force-update@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/react-deep-force-update/-/react-deep-force-update-1.0.1.tgz#f911b5be1d2a6fe387507dd6e9a767aa2924b4c7"

react-dom@^15.2.1, react-dom@^15.4.0:
react-dom@^15.4.0:
version "15.4.2"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-15.4.2.tgz#015363f05b0a1fd52ae9efdd3a0060d90695208f"
dependencies:
fbjs "^0.8.1"
loose-envify "^1.1.0"
object-assign "^4.1.0"

react-dom@~15.5.4:
version "15.5.4"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-15.5.4.tgz#ba0c28786fd52ed7e4f2135fe0288d462aef93da"
integrity sha1-ugwoeG/VLtfk8hNf4CiNRirvk9o=
dependencies:
fbjs "^0.8.9"
loose-envify "^1.1.0"
object-assign "^4.1.0"
prop-types "~15.5.7"

react-is@^16.8.1:
version "16.8.6"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.6.tgz#5bbc1e2d29141c9fbdfed456343fe2bc430a6a16"
integrity sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==

react-native-mock@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/react-native-mock/-/react-native-mock-0.3.1.tgz#13d22433c5351a8a7fb8aedd7862b614d087851c"
Expand Down Expand Up @@ -3465,13 +3528,15 @@ react-transform-hmr@^1.0.4:
global "^4.3.0"
react-proxy "^1.1.7"

react@~15.4.0-rc.4:
version "15.4.2"
resolved "https://registry.yarnpkg.com/react/-/react-15.4.2.tgz#41f7991b26185392ba9bae96c8889e7e018397ef"
react@~15.5.4:
version "15.5.4"
resolved "https://registry.yarnpkg.com/react/-/react-15.5.4.tgz#fa83eb01506ab237cdc1c8c3b1cea8de012bf047"
integrity sha1-+oPrAVBqsjfNwcjDsc6o3gEr8Ec=
dependencies:
fbjs "^0.8.4"
fbjs "^0.8.9"
loose-envify "^1.1.0"
object-assign "^4.1.0"
prop-types "^15.5.7"

read-pkg-up@^1.0.1:
version "1.0.1"
Expand Down Expand Up @@ -4074,6 +4139,11 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

ua-parser-js@^0.7.18:
version "0.7.19"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.19.tgz#94151be4c0a7fb1d001af7022fdaca4642659e4b"
integrity sha512-T3PVJ6uz8i0HzPxOF9SWzWAlfN/DavlpQqepn22xgve/5QecC+XMCAtmUNnY7C9StehaV6exjUCI801lOI7QlQ==

ua-parser-js@^0.7.9:
version "0.7.12"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.12.tgz#04c81a99bdd5dc52263ea29d24c6bf8d4818a4bb"
Expand Down

0 comments on commit ee99242

Please sign in to comment.