Skip to content

Commit

Permalink
Merge pull request #858 from equinor/fix/custom-id-filter
Browse files Browse the repository at this point in the history
🩹 Add ID prop to Filter component
  • Loading branch information
mariush2 authored Nov 6, 2024
2 parents 52bce34 + 6bee1d5 commit c01bdb3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/organisms/Filter/Filter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ test('Filter opens/closes as expected when clicking search field', async () => {
expect(screen.getByText(childText)).toBeInTheDocument();
});

test('ID works as expected', async () => {
const props = fakeProps();
const childText = faker.lorem.sentence();
const id = faker.lorem.word();
render(
<Filter {...props} id={id}>
<p>{childText}</p>
</Filter>
);
const search = screen.getByRole('searchbox');

expect(search).toHaveAttribute('id', id);
});

test('Filter opens/closes as expected when clicking search field with multiple children', async () => {
const props = fakeProps();
const childText = faker.lorem.sentence().split(' ');
Expand Down
5 changes: 4 additions & 1 deletion src/organisms/Filter/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface FilterProps<T> {
children: ReactNode | ReactNode[];
initialOpen?: boolean;
placeholder?: string;
id?: string;
showClearFiltersButton?: boolean;
}

Expand All @@ -43,6 +44,7 @@ export interface FilterProps<T> {
* @param children - ReactNode or ReactNode[] to display below when the filter is open
* @param initialOpen - Whether the filter should be open by default, defaults to false
* @param placeholder - Placeholder text for the search input, defaults to 'Search...'
* @param id - ID for the search field
* @param showClearFiltersButton - Whether to show the clear filters button, defaults to true
*/
export function Filter<T>({
Expand All @@ -55,6 +57,7 @@ export function Filter<T>({
children,
initialOpen = false,
placeholder = 'Search...',
id = 'filter-search',
showClearFiltersButton = true,
}: FilterProps<T>) {
const [open, setOpen] = useState(initialOpen);
Expand Down Expand Up @@ -111,7 +114,7 @@ export function Filter<T>({
</StyledChip>
))}
<SearchField
id="filter-search"
id={id}
type="search"
value={search}
placeholder={placeholder}
Expand Down

0 comments on commit c01bdb3

Please sign in to comment.