Skip to content

Commit

Permalink
fix: fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hoaphantn7604 committed Jun 21, 2024
1 parent 7874a95 commit 022a2da
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 76 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ yarn add react-native-element-dropdown
| itemTextStyle | TextStyle | No | Styling for text item in list |
| selectedStyle | ViewStyle | No | Styling for selected view |
| selectedTextStyle | TextStyle | No | Styling for selected text |
| selectedTextProps | TextProps | No | Text Props for selected text. Ex: numberOfLines={1} |
| renderSelectedItem | (item: object, unSelect?: () => void) => JSX.Element | No | Takes an item from data and renders it into the list selected |
| alwaysRenderSelectedItem | Boolean | No | Always show the list of selected items |
| visibleSelectedItem | Boolean | No | Option to hide selected item list, Ẽx: visibleSelectedItem={false} |
Expand Down
2 changes: 1 addition & 1 deletion example/src/dropdown/DropdownLazyLoad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const styles = StyleSheet.create({
backgroundColor: '#DDDDDD',
margin: 16,
paddingHorizontal: 16,
paddingVertical: 8,
paddingVertical: 16,
borderRadius: 8,
},
icon: {
Expand Down
92 changes: 56 additions & 36 deletions src/components/Dropdown/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/* eslint-disable @typescript-eslint/no-shadow */
import _ from 'lodash';
import _assign from 'lodash/assign';
import _differenceWith from 'lodash/differenceWith';
import _findIndex from 'lodash/findIndex';
import _get from 'lodash/get';
import _isEqual from 'lodash/isEqual';

import React, {
JSXElementConstructor,
ReactElement,
Expand Down Expand Up @@ -129,10 +134,10 @@ const DropdownComponent: <T>(
const excludeData = useCallback(
(data: any[]) => {
if (excludeItems.length > 0) {
const getData = _.differenceWith(
const getData = _differenceWith(
data,
excludeItems,
(obj1, obj2) => _.get(obj1, valueField) === _.get(obj2, valueField)
(obj1, obj2) => _get(obj1, valueField) === _get(obj2, valueField)
);
return getData || [];
} else {
Expand Down Expand Up @@ -247,10 +252,10 @@ const DropdownComponent: <T>(

const getValue = useCallback(() => {
const defaultValue =
typeof value === 'object' ? _.get(value, valueField) : value;
typeof value === 'object' ? _get(value, valueField) : value;

const getItem = data.filter((e) =>
_.isEqual(defaultValue, _.get(e, valueField))
_isEqual(defaultValue, _get(e, valueField))
);

if (getItem.length > 0) {
Expand All @@ -265,24 +270,28 @@ const DropdownComponent: <T>(
}, [value, data, getValue]);

const scrollIndex = useCallback(() => {
if (autoScroll && data.length > 0 && listData.length === data.length) {
if (autoScroll && data?.length > 0 && listData?.length === data?.length) {
setTimeout(() => {
if (refList && refList?.current) {
const defaultValue =
typeof value === 'object' ? _.get(value, valueField) : value;
typeof value === 'object' ? _get(value, valueField) : value;

const index = _.findIndex(listData, (e: any) =>
_.isEqual(defaultValue, _.get(e, valueField))
const index = _findIndex(listData, (e) =>
_isEqual(defaultValue, _get(e, valueField))
);
if (
listData.length > 0 &&
listData?.length > 0 &&
index > -1 &&
index <= listData.length - 1
index <= listData?.length - 1
) {
refList?.current?.scrollToIndex({
index: index,
animated: false,
});
try {
refList.current.scrollToIndex({
index: index,
animated: false,
});
} catch (error) {
console.warn(`scrollToIndex error: ${error}`);
}
}
}
}, 200);
Expand All @@ -291,16 +300,26 @@ const DropdownComponent: <T>(

const showOrClose = useCallback(() => {
if (!disable) {
if (keyboardHeight > 0 && visible) {
const visibleStatus = !visible;

if (keyboardHeight > 0 && !visibleStatus) {
return Keyboard.dismiss();
}

if (!visibleStatus) {
if (onChangeText) {
onChangeText('');
}
setSearchText('');
onSearch('');
}

_measure();
setVisible(!visible);
setVisible(visibleStatus);
const filterData = excludeData(data);
setListData(filterData);

if (!visible) {
if (visibleStatus) {
if (onFocus) {
onFocus();
}
Expand All @@ -309,6 +328,7 @@ const DropdownComponent: <T>(
onBlur();
}
}

if (searchText.length > 0) {
onSearch(searchText);
}
Expand All @@ -331,7 +351,7 @@ const DropdownComponent: <T>(
(text: string) => {
if (text.length > 0) {
const defaultFilterFunction = (e: any) => {
const item = _.get(e, searchField || labelField)
const item = _get(e, searchField || labelField)
?.toLowerCase()
.replace(/\s/g, '')
.normalize('NFD')
Expand All @@ -346,7 +366,7 @@ const DropdownComponent: <T>(
};

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

return searchQuery?.(text, labelText);
};
Expand All @@ -356,11 +376,10 @@ const DropdownComponent: <T>(
);

if (excludeSearchItems.length > 0 || excludeItems.length > 0) {
const excludeSearchData = _.differenceWith(
const excludeSearchData = _differenceWith(
dataSearch,
excludeSearchItems,
(obj1, obj2) =>
_.get(obj1, valueField) === _.get(obj2, valueField)
(obj1, obj2) => _get(obj1, valueField) === _get(obj2, valueField)
);

const filterData = excludeData(excludeSearchData);
Expand Down Expand Up @@ -391,14 +410,15 @@ const DropdownComponent: <T>(
return onConfirmSelectItem(item);
}

if (onChangeText) {
setSearchText('');
onChangeText('');
}
onSearch('');
setCurrentValue(item);
onChange(item);

if (closeModalWhenSelectedItem) {
if (onChangeText) {
onChangeText('');
}
setSearchText('');
onSearch('');
eventClose();
}
},
Expand All @@ -414,7 +434,7 @@ const DropdownComponent: <T>(
);

const _renderDropdown = () => {
const isSelected = currentValue && _.get(currentValue, valueField);
const isSelected = currentValue && _get(currentValue, valueField);
return (
<TouchableWithoutFeedback
testID={testID}
Expand All @@ -433,7 +453,7 @@ const DropdownComponent: <T>(
{...selectedTextProps}
>
{isSelected !== null
? _.get(currentValue, labelField)
? _get(currentValue, labelField)
: placeholder}
</Text>
{renderRightIcon ? (
Expand All @@ -455,15 +475,15 @@ const DropdownComponent: <T>(

const _renderItem = useCallback(
({ item, index }: { item: any; index: number }) => {
const isSelected = currentValue && _.get(currentValue, valueField);
const selected = _.isEqual(_.get(item, valueField), isSelected);
_.assign(item, { _index: index });
const isSelected = currentValue && _get(currentValue, valueField);
const selected = _isEqual(_get(item, valueField), isSelected);
_assign(item, { _index: index });
return (
<TouchableHighlight
key={index.toString()}
testID={_.get(item, itemTestIDField || labelField)}
testID={_get(item, itemTestIDField || labelField)}
accessible={!!accessibilityLabel}
accessibilityLabel={_.get(
accessibilityLabel={_get(
item,
itemAccessibilityLabelField || labelField
)}
Expand All @@ -489,7 +509,7 @@ const DropdownComponent: <T>(
font(),
])}
>
{_.get(item, labelField)}
{_get(item, labelField)}
</Text>
</View>
)}
Expand Down
1 change: 0 additions & 1 deletion src/components/Dropdown/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const styles = StyleSheet.create({
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
justifyContent: 'space-between',
alignItems: 'center',
height: 35,
},
title: {
marginVertical: 5,
Expand Down
Loading

0 comments on commit 022a2da

Please sign in to comment.