Skip to content

Commit

Permalink
fix: Fixes collection prefs filtering when column labels are not stri…
Browse files Browse the repository at this point in the history
…ngs and filtering is disabled (#2887)
  • Loading branch information
pan-kot authored Oct 16, 2024
1 parent 5496d3e commit 51d35e1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
import { fireEvent } from '@testing-library/react';

import { CollectionPreferencesProps } from '../../../../lib/components';
Expand Down Expand Up @@ -255,6 +256,8 @@ describe('Content Display preference', () => {
contentDisplayPreference: {
...contentDisplayPreference,
enableColumnFiltering: false,
// Adding an option with a non-string label to ensure the filter does not break rendering
options: [...contentDisplayPreference.options, { id: 'id-extra', label: (<span>Extra</span>) as any }],
},
});
const filterInput = wrapper.findTextFilter();
Expand Down
7 changes: 2 additions & 5 deletions src/collection-preferences/content-display/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ContentDisplayOption from './content-display-option';
import DraggableOption from './draggable-option';
import useDragAndDropReorder from './use-drag-and-drop-reorder';
import useLiveAnnouncements from './use-live-announcements';
import { getSortedOptions, OptionWithVisibility } from './utils';
import { getFilteredOptions, getSortedOptions, OptionWithVisibility } from './utils';

import styles from '../styles.css.js';

Expand Down Expand Up @@ -60,10 +60,7 @@ export default function ContentDisplayPreference({
const descriptionId = `${idPrefix}-description`;

const sortedAndFilteredOptions = useMemo(
() =>
getSortedOptions({ options, contentDisplay: value }).filter(option =>
option.label.toLowerCase().trim().includes(columnFilteringText.toLowerCase().trim())
),
() => getFilteredOptions(getSortedOptions({ options, contentDisplay: value }), columnFilteringText),
[columnFilteringText, options, value]
);

Expand Down
13 changes: 13 additions & 0 deletions src/collection-preferences/content-display/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ export function getSortedOptions({
}))
.filter(Boolean);
}

export function getFilteredOptions(
options: ReadonlyArray<CollectionPreferencesProps.ContentDisplayOption>,
filterText: string
) {
filterText = filterText.trim().toLowerCase();

if (!filterText) {
return options;
}

return options.filter(option => option.label.toLowerCase().trim().includes(filterText));
}

0 comments on commit 51d35e1

Please sign in to comment.