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

Add searchBothFieldFunction to labelField, valueField and Empty result message #308

Open
wants to merge 4 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ yarn add react-native-element-dropdown
| itemTextStyle | TextStyle | No | Styling for text item in list |
| activeColor | String | No | Background color for item selected in list container |
| search | Boolean | No | Show or hide input search |
| searchBothFields | Boolean | No | Search by valueField and labelField |
| searchQuery | (keyword: string, labelValue: string) => Boolean| No | Callback used to filter the list of items |
| inputSearchStyle | ViewStyle | No | Styling for input search |
| searchPlaceholder | String | No | The string that will be rendered before text input has been entered |
Expand Down
41 changes: 40 additions & 1 deletion src/components/Dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const DropdownComponent: <T>(
searchPlaceholderTextColor = 'gray',
placeholder = 'Select item',
search = false,
searchBothFields = false,
maxHeight = 340,
minHeight = 0,
disable = false,
Expand Down Expand Up @@ -373,14 +374,38 @@ const DropdownComponent: <T>(
return item.indexOf(key) >= 0;
};

const searchBothFieldFunction = (e: any) => {
const item = _get(e, labelField)
?.toLowerCase()
.replace(/\s/g, '')
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '');
const key = text
.toLowerCase()
.replace(/\s/g, '')
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '');

const item1 = _get(e, valueField)
?.toLowerCase()
.replace(/\s/g, '')
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '');

const isLabelMatch = item.indexOf(key) >= 0;
const isValueMatch = item1.indexOf(key) >= 0;

return isLabelMatch || isValueMatch;
};

const propSearchFunction = (e: any) => {
const labelText = _get(e, searchField || labelField);

return searchQuery?.(text, labelText);
};

const dataSearch = data.filter(
searchQuery ? propSearchFunction : defaultFilterFunction
searchQuery ? propSearchFunction : searchBothFields ? searchBothFieldFunction: defaultFilterFunction
);

if (excludeSearchItems.length > 0 || excludeItems.length > 0) {
Expand All @@ -403,6 +428,7 @@ const DropdownComponent: <T>(
[
data,
searchQuery,
searchBothFields,
excludeSearchItems,
excludeItems,
searchField,
Expand Down Expand Up @@ -606,6 +632,19 @@ const DropdownComponent: <T>(
ref={refList}
onScrollToIndexFailed={scrollIndex}
data={listData}
ListEmptyComponent={
<View style={styles.item}>
<Text
style={StyleSheet.flatten([
styles.textItem,
itemTextStyle,
font(),
])}
>
No Result Found
</Text>
</View>
}
inverted={isTopPosition ? inverted : false}
renderItem={_renderItem}
keyExtractor={(_item, index) => index.toString()}
Expand Down
1 change: 1 addition & 0 deletions src/components/Dropdown/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface DropdownProps<T> {
valueField: keyof T;
searchField?: keyof T;
search?: boolean;
searchBothFields?: boolean;
searchPlaceholder?: string;
searchPlaceholderTextColor?: string;
disable?: boolean;
Expand Down