Skip to content

Commit

Permalink
fix: remove redundant functions
Browse files Browse the repository at this point in the history
  • Loading branch information
azeezat committed Sep 7, 2023
1 parent 2b01adb commit 9ac7d59
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
27 changes: 12 additions & 15 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import type {
TSectionList,
TSectionListItem,
} from './types/index.types';
import {
extractPropertyFromArray,
getMaxLengthOfSectionListProperty,
} from './utils';
import { extractPropertyFromArray } from './utils';

export const DropdownSelect: React.FC<DropdownProps> = ({
placeholder,
Expand Down Expand Up @@ -104,6 +101,11 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
newOptions,
'data'
).flat();

/**
*`modifiedOptions` should only be used for computations newOptions remains the default array.
* At this point modifiedOptions now has the same structure for both `FlatList` and `SectionList`
*/
const modifiedOptions = isSectionList ? modifiedSectionData : newOptions;

const optLabel = optionLabel || DEFAULT_OPTION_LABEL;
Expand Down Expand Up @@ -147,7 +149,11 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
const selectedValues = [];

// don't select disabled items
const filteredOptions = removeDisabledItems(optionsCopy);
const filteredOptions = removeDisabledItems(
isSectionList
? extractPropertyFromArray(optionsCopy, 'data').flat()
: optionsCopy
);

if (!prevVal) {
for (let i = 0; i < filteredOptions.length; i++) {
Expand Down Expand Up @@ -287,15 +293,6 @@ export const DropdownSelect: React.FC<DropdownProps> = ({

let primary = primaryColor || colors.gray;

const sectionListMaxLength = getMaxLengthOfSectionListProperty(
newOptions as TSectionList,
'data'
);

const listIsEmpty = isSectionList
? sectionListMaxLength > 1
: newOptions.length > 1;

/*===========================================
* setIndexOfSelectedItem - For ScrollToIndex
*==========================================*/
Expand Down Expand Up @@ -365,7 +362,7 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
/>
)}
{listHeaderComponent}
{isMultiple && listIsEmpty && (
{isMultiple && modifiedOptions.length > 1 && (
<View style={styles.optionsContainerStyle}>
<TouchableOpacity onPress={() => {}}>
<CheckBox
Expand Down
18 changes: 1 addition & 17 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import { TSectionList } from 'src/types/index.types';

/**
* Extract property from array
*/

export const extractPropertyFromArray = (arr: any, property: string) => {
let extractedValue = arr.map((item: any) => item[property]);

return extractedValue;
};

export const getMaxLengthOfSectionListProperty = (
sectionList: TSectionList,
property: 'title' | 'data'
) => {
let maxLength = 0;

sectionList.forEach((obj) => {
if (obj[property]?.length > maxLength) {
maxLength = obj.data.length;
}
});

return maxLength;
};

0 comments on commit 9ac7d59

Please sign in to comment.