Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function SearchFiltersBarNarrow({queryJSON}: SearchFiltersBarNarrowProps) {
ref={scrollRef}
showsHorizontalScrollIndicator={false}
data={data}
keyExtractor={(item) => item.key}
keyExtractor={(item) => item.id}
renderItem={renderFilterItem}
onEndReached={adjustScroll}
onEndReachedThreshold={0.75}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function SearchFiltersBarWideContent({hasErrors, shouldShowFiltersBarLoading, fi

return filters.map((item) => (
<SearchFilterBar
key={item.key}
key={item.id}
item={item}
/>
));
Expand Down
14 changes: 11 additions & 3 deletions src/libs/SearchUIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6256,6 +6256,8 @@ function getReportFieldTextKey(key: ReportFieldKey): ReportFieldTextKey {

type SearchFilter = {
key: keyof typeof FILTER_VIEW_MAP;
/** Unique per filter, unlike `key` which is shared by a filter and its negation (e.g. `status` and `statusNot`) */
id: MappedFilterKey;
label: string;
value: string | string[];
};
Expand Down Expand Up @@ -6328,7 +6330,7 @@ function mapFiltersFormToLabelValueList(
if (displayValue && label) {
addedGroups.add(syntax);
const isDefault = defaultSearchQueryFilterKeys.has(syntax);
(isDefault ? defaultFilters : nonDefaultFilters).push({key: syntax, label: translate(label), value: displayValue, ...mapper?.(syntax, isDefault)});
(isDefault ? defaultFilters : nonDefaultFilters).push({key: syntax, id: syntax, label: translate(label), value: displayValue, ...mapper?.(syntax, isDefault)});
}
continue;
}
Expand All @@ -6344,7 +6346,13 @@ function mapFiltersFormToLabelValueList(
addedGroups.add(CONST.SEARCH.REPORT_FIELD.GLOBAL_PREFIX);
const isDefault = defaultSearchQueryFilterKeys.has(getReportFieldTextKey(key));
const extra = mapper?.(CONST.SEARCH.SYNTAX_FILTER_KEYS.REPORT_FIELD, isDefault);
(isDefault ? defaultFilters : nonDefaultFilters).push({key: CONST.SEARCH.SYNTAX_FILTER_KEYS.REPORT_FIELD, label: translate('workspace.common.reportField'), value, ...extra});
(isDefault ? defaultFilters : nonDefaultFilters).push({
key: CONST.SEARCH.SYNTAX_FILTER_KEYS.REPORT_FIELD,
id: CONST.SEARCH.SYNTAX_FILTER_KEYS.REPORT_FIELD,
label: translate('workspace.common.reportField'),
value,
...extra,
});
}
continue;
}
Expand All @@ -6361,7 +6369,7 @@ function mapFiltersFormToLabelValueList(

if (label && value && !(Array.isArray(value) && value.length === 0)) {
const isDefault = defaultSearchQueryFilterKeys.has(baseKey);
(isDefault ? defaultFilters : nonDefaultFilters).push({key: baseKey, label, value, ...mapper?.(key, isDefault)});
(isDefault ? defaultFilters : nonDefaultFilters).push({key: baseKey, id: key, label, value, ...mapper?.(key, isDefault)});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/Search/SearchSavePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function SearchSavePage() {
appliedFilters.map((filter) => (
<View
style={[styles.flexRow]}
key={filter.key}
key={filter.id}
>
<Text style={[styles.label, styles.ph2]}>{CONST.DOT_SEPARATOR}</Text>
<Text style={[styles.label]}>
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/Search/SearchUIUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15364,6 +15364,22 @@ describe('SearchUIUtils', () => {
expect(result.at(0)?.isDefault).toBe(true);
expect(result.at(1)?.isDefault).toBe(false);
});

it('gives a filter and its negation the same key but different ids', () => {
// Given a query that uses the status filter both normally and negated, e.g. `status:approved -status:drafts,paid`
const form = {
[FILTER_KEYS.TYPE]: CONST.SEARCH.DATA_TYPES.EXPENSE,
[FILTER_KEYS.STATUS]: [CONST.SEARCH.STATUS.EXPENSE.APPROVED],
[FILTER_KEYS.STATUS_NOT]: [CONST.SEARCH.STATUS.EXPENSE.DRAFTS, CONST.SEARCH.STATUS.EXPENSE.PAID],
};

// When the form is mapped to filter chips
const result = SearchUIUtils.mapFiltersFormToLabelValueList(form, new Set(), new Set(), translateLocal, undefined, localeCompare, convertToDisplayStringWithoutCurrency);
const statusFilters = result.filter((filter) => filter.key === CONST.SEARCH.SYNTAX_FILTER_KEYS.STATUS);

// Then both chips share the base key (used to pick the chip's popover), while their ids stay unique so they can be used as React keys
expect(statusFilters.map((filter) => filter.id)).toEqual([FILTER_KEYS.STATUS, FILTER_KEYS.STATUS_NOT]);
});
});
});

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/pages/Search/SearchSavePageTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ it.each([[['12']], [['12', '23']], [['123']]])('renders canonical card selection
});
it('renders scalar feed and tax values without calling array display hooks', () => {
jest.spyOn(SearchUIUtils, 'mapFiltersFormToLabelValueList').mockReturnValueOnce([
{key: CONST.SEARCH.SYNTAX_FILTER_KEYS.FEED, label: 'Feed', value: 'feed-scalar'},
{key: CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE, label: 'Tax', value: 'tax-scalar'},
{key: CONST.SEARCH.SYNTAX_FILTER_KEYS.FEED, id: CONST.SEARCH.SYNTAX_FILTER_KEYS.FEED, label: 'Feed', value: 'feed-scalar'},
{key: CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE, id: CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE, label: 'Tax', value: 'tax-scalar'},
]);
const output = JSON.stringify(render(<SearchSavePage />).toJSON());
expect(['feed-scalar', 'tax-scalar'].every((text) => output.includes(text))).toBe(true);
Expand Down
Loading