Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed special character bug and implemented new search method #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -4,6 +4,7 @@ import * as React from "react";
import { ViewStyle, TextStyle, TextInputProps, StyleProp, FlatListProps } from 'react-native';

export interface MultiSelectProps {
loading?: boolean;
single?: boolean;
selectedItems?: any[];
items: any[];
Expand Down
47 changes: 32 additions & 15 deletions lib/react-native-multi-select.js
Expand Up @@ -7,7 +7,9 @@ import {
TouchableOpacity,
FlatList,
UIManager,
ViewPropTypes
ViewPropTypes,
ActivityIndicator,
ScrollView
} from 'react-native';
import PropTypes from 'prop-types';
import reject from 'lodash/reject';
Expand All @@ -34,6 +36,7 @@ const defaultSearchIcon = (

export default class MultiSelect extends Component {
static propTypes = {
loading: PropTypes.bool,
single: PropTypes.bool,
selectedItems: PropTypes.array,
items: PropTypes.array.isRequired,
Expand Down Expand Up @@ -85,6 +88,7 @@ export default class MultiSelect extends Component {
};

static defaultProps = {
loading: false,
single: false,
selectedItems: [],
uniqueKey: '_id',
Expand Down Expand Up @@ -434,20 +438,20 @@ export default class MultiSelect extends Component {
switch (this.props.filterMethod) {
case 'full':
return this._filterItemsFull(searchTerm);
case 'no_filter':
return this.props.items;
default:
return this._filterItemsPartial(searchTerm);
}
};

_filterItemsPartial = searchTerm => {
const { items, displayKey } = this.props;
const filteredItems = [];
items.forEach(item => {
const parts = searchTerm.trim().split(/[ \-:]+/);
const regex = new RegExp(`(${parts.join('|')})`, 'ig');
if (regex.test(get(item, displayKey))) {
filteredItems.push(item);
}
let filteredItems = [];
filteredItems = items.filter(item => {
let q = searchTerm.toLowerCase();
let payload = get(item, displayKey);
return payload.toLowerCase().indexOf(q) > -1;
});
return filteredItems;
};
Expand Down Expand Up @@ -476,7 +480,8 @@ export default class MultiSelect extends Component {
selectedItems,
flatListProps,
styleListContainer,
removeSelected
removeSelected,
loading
} = this.props;
const { searchTerm } = this.state;
let component = null;
Expand Down Expand Up @@ -529,12 +534,24 @@ export default class MultiSelect extends Component {
if (canAddItems && !searchTermMatch && searchTerm.length) {
addItemRow = this._getRowNew({ name: searchTerm });
}
component = (
<View style={styleListContainer && styleListContainer}>
{itemList}
{addItemRow}
</View>
);

if (loading) {
component = (
<View style={styleListContainer && styleListContainer}>
<View style={{ alignItems: 'center', width: '100%', height: 50 }}>
<ActivityIndicator color={'#000'} size={'small'} />
</View>
</View>
);
} else {
component = (
<View style={styleListContainer && styleListContainer}>
{itemList}
{addItemRow}
</View>
);
}

return component;
};

Expand Down