Skip to content

Commit

Permalink
type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemorawski committed Apr 16, 2024
1 parent 91415e8 commit ef3ecdc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function YearPickerModal({isVisible, years, currentYear = new Date().getFullYear
const yearsList = searchText === '' ? years : years.filter((year) => year.text?.includes(searchText));
return {
headerMessage: !yearsList.length ? translate('common.noResultsFound') : '',
sections: [{data: yearsList.sort((a, b) => b.value - a.value)}],
sections: [{data: yearsList.sort((a, b) => b.value - a.value), indexOffset: 0}],
};
}, [years, searchText, translate]);

Expand Down
2 changes: 1 addition & 1 deletion src/components/OptionsList/BaseOptionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function BaseOptionsList(
option={item}
showTitleTooltip={showTitleTooltip}
hoverStyle={optionHoveredStyle}
optionIsFocused={!disableFocusOptions && !isItemDisabled && focusedIndex === index + section.indexOffset}
optionIsFocused={!disableFocusOptions && !isItemDisabled && focusedIndex === index + (section.indexOffset ?? 0)}
onSelectRow={onSelectRow}
isSelected={isSelected}
showSelectedState={canSelectMultipleOptions}
Expand Down
2 changes: 1 addition & 1 deletion src/components/OptionsList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Section = {

type SectionWithIndexOffset = Section & {
/** The initial index of this section given the total number of options in each section's data array */
indexOffset: number;
indexOffset?: number;
};

type OptionsListProps = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/SelectionList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ type Section<TItem extends ListItem> = {

type SectionWithIndexOffset<TItem extends ListItem> = Section<TItem> & {
/** The initial index of this section given the total number of options in each section's data array */
indexOffset: number;
indexOffset?: number;
};

type BaseSelectionListProps<TItem extends ListItem> = Partial<ChildrenProps> & {
Expand Down
27 changes: 20 additions & 7 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ type TaxSection = {

type CategoryTreeSection = CategorySectionBase & {
data: OptionTree[];
indexOffset?: number;
};

type Category = {
Expand Down Expand Up @@ -1023,11 +1024,13 @@ function getCategoryListSections(
const numberOfEnabledCategories = enabledCategories.length;

if (numberOfEnabledCategories === 0 && selectedOptions.length > 0) {
const data = getCategoryOptionTree(selectedOptions, true);
categorySections.push({
// "Selected" section
title: '',
shouldShow: false,
data: getCategoryOptionTree(selectedOptions, true),
data,
indexOffset: data.length,
});

return categorySections;
Expand All @@ -1046,34 +1049,40 @@ function getCategoryListSections(
});
});

const data = getCategoryOptionTree(searchCategories, true);
categorySections.push({
// "Search" section
title: '',
shouldShow: true,
data: getCategoryOptionTree(searchCategories, true),
data,
indexOffset: data.length,
});

return categorySections;
}

if (selectedOptions.length > 0) {
const data = getCategoryOptionTree(selectedOptions, true);
categorySections.push({
// "Selected" section
title: '',
shouldShow: false,
data: getCategoryOptionTree(selectedOptions, true),
data,
indexOffset: data.length,
});
}

const selectedOptionNames = selectedOptions.map((selectedOption) => selectedOption.name);
const filteredCategories = enabledCategories.filter((category) => !selectedOptionNames.includes(category.name));

if (numberOfEnabledCategories < CONST.CATEGORY_LIST_THRESHOLD) {
const data = getCategoryOptionTree(filteredCategories, false, selectedOptionNames);
categorySections.push({
// "All" section when items amount less than the threshold
title: '',
shouldShow: false,
data: getCategoryOptionTree(filteredCategories, false, selectedOptionNames),
data,
indexOffset: data.length,
});

return categorySections;
Expand All @@ -1089,19 +1098,23 @@ function getCategoryListSections(
if (filteredRecentlyUsedCategories.length > 0) {
const cutRecentlyUsedCategories = filteredRecentlyUsedCategories.slice(0, maxRecentReportsToShow);

const data = getCategoryOptionTree(cutRecentlyUsedCategories, true);
categorySections.push({
// "Recent" section
title: Localize.translateLocal('common.recent'),
shouldShow: true,
data: getCategoryOptionTree(cutRecentlyUsedCategories, true),
data,
indexOffset: data.length,
});
}

const data = getCategoryOptionTree(filteredCategories, false, selectedOptionNames);
categorySections.push({
// "All" section when items amount more than the threshold
title: Localize.translateLocal('common.all'),
shouldShow: true,
data: getCategoryOptionTree(filteredCategories, false, selectedOptionNames),
data,
indexOffset: data.length,
});

return categorySections;
Expand Down Expand Up @@ -2356,4 +2369,4 @@ export {
getFirstKeyForList,
};

export type {MemberForList, CategorySection, CategoryTreeSection, Options, OptionList, SearchOption, PayeePersonalDetails, Category, TaxRatesOption, Option};
export type {MemberForList, CategorySection, CategoryTreeSection, Options, OptionList, SearchOption, PayeePersonalDetails, Category, TaxRatesOption, Option, OptionTree};

0 comments on commit ef3ecdc

Please sign in to comment.