-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add search filters for platform and gen version #8526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+263
−7
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { useState, useEffect, useCallback } from 'react'; | ||
| import { createPortal } from 'react-dom'; | ||
| import { PLATFORMS, PLATFORM_DISPLAY_NAMES } from '@/data/platforms'; | ||
| import type { Platform } from '@/data/platforms'; | ||
|
|
||
| export type GenFilter = 'gen1' | 'gen2' | 'both'; | ||
| export type PlatformFilter = Platform | 'all'; | ||
|
|
||
| interface SearchFiltersProps { | ||
| platformFilter: PlatformFilter; | ||
| genFilter: GenFilter; | ||
| onPlatformChange: (platform: PlatformFilter) => void; | ||
| onGenChange: (gen: GenFilter) => void; | ||
| } | ||
|
|
||
| const GEN_DISPLAY: Record<GenFilter, string> = { | ||
| gen2: 'Gen 2', | ||
| gen1: 'Gen 1', | ||
| both: 'Both' | ||
| }; | ||
|
|
||
| export function SearchFilters({ | ||
| platformFilter, | ||
| genFilter, | ||
| onPlatformChange, | ||
| onGenChange | ||
| }: SearchFiltersProps) { | ||
| const [portalContainer, setPortalContainer] = useState<HTMLElement | null>(null); | ||
|
|
||
| const cleanupContainer = useCallback((container: HTMLElement | null) => { | ||
| container?.remove(); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| const observer = new MutationObserver(() => { | ||
| const modal = document.querySelector('.DocSearch-Modal'); | ||
| const searchBar = modal?.querySelector('.DocSearch-SearchBar'); | ||
|
|
||
| if (searchBar && !modal?.querySelector('.search-filters-container')) { | ||
| const container = document.createElement('div'); | ||
| container.className = 'search-filters-container'; | ||
| searchBar.insertAdjacentElement('afterend', container); | ||
| setPortalContainer(container); | ||
| } else if (!modal) { | ||
| setPortalContainer((prev) => { | ||
| cleanupContainer(prev); | ||
| return null; | ||
| }); | ||
| } | ||
| }); | ||
| observer.observe(document.body, { childList: true, subtree: false }); | ||
| return () => { | ||
| observer.disconnect(); | ||
| setPortalContainer((prev) => { | ||
| cleanupContainer(prev); | ||
| return null; | ||
| }); | ||
| }; | ||
| }, [cleanupContainer]); | ||
|
|
||
| if (!portalContainer) return null; | ||
|
|
||
| return createPortal( | ||
| <div className="search-filters" role="toolbar" aria-label="Search filters"> | ||
| <div className="search-filters__group"> | ||
| <span className="search-filters__label">Platform:</span> | ||
| <select | ||
| className="search-filters__select" | ||
| value={platformFilter} | ||
| onChange={(e) => onPlatformChange(e.target.value as PlatformFilter)} | ||
| aria-label="Platform filter" | ||
| > | ||
| <option value="all">All</option> | ||
| {PLATFORMS.map((p) => ( | ||
| <option key={p} value={p}> | ||
| {PLATFORM_DISPLAY_NAMES[p]} | ||
| </option> | ||
| ))} | ||
| </select> | ||
| </div> | ||
| <div className="search-filters__separator" /> | ||
| <div className="search-filters__group"> | ||
| <span className="search-filters__label">Version:</span> | ||
| <select | ||
| className="search-filters__select" | ||
| value={genFilter} | ||
| onChange={(e) => onGenChange(e.target.value as GenFilter)} | ||
| aria-label="Generation filter" | ||
| > | ||
| <option value="gen2">{GEN_DISPLAY.gen2}</option> | ||
| <option value="gen1">{GEN_DISPLAY.gen1}</option> | ||
| <option value="both">{GEN_DISPLAY.both}</option> | ||
| </select> | ||
| </div> | ||
| </div>, | ||
| portalContainer | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import * as React from 'react'; | ||
| import { render, waitFor, act, fireEvent } from '@testing-library/react'; | ||
| import { SearchFilters } from '../SearchFilters'; | ||
| import type { GenFilter, PlatformFilter } from '../SearchFilters'; | ||
|
|
||
| describe('SearchFilters', () => { | ||
| let mockModal: HTMLElement; | ||
| let onPlatformChange: jest.Mock; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should test these interactions as well, |
||
| let onGenChange: jest.Mock; | ||
|
|
||
| beforeEach(() => { | ||
| onPlatformChange = jest.fn(); | ||
| onGenChange = jest.fn(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (mockModal) mockModal.remove(); | ||
| }); | ||
|
|
||
| function renderFilters( | ||
| platformFilter: PlatformFilter = 'react', | ||
| genFilter: GenFilter = 'gen2' | ||
| ) { | ||
| const result = render( | ||
| <SearchFilters | ||
| platformFilter={platformFilter} | ||
| genFilter={genFilter} | ||
| onPlatformChange={onPlatformChange} | ||
| onGenChange={onGenChange} | ||
| /> | ||
| ); | ||
| // Simulate DocSearch modal DOM structure | ||
| mockModal = document.createElement('div'); | ||
| mockModal.className = 'DocSearch-Modal'; | ||
| const searchBar = document.createElement('div'); | ||
| searchBar.className = 'DocSearch-SearchBar'; | ||
| const form = document.createElement('form'); | ||
| form.className = 'DocSearch-Form'; | ||
| searchBar.appendChild(form); | ||
| mockModal.appendChild(searchBar); | ||
| document.body.appendChild(mockModal); | ||
| return result; | ||
| } | ||
|
|
||
| async function waitForFilters() { | ||
| await waitFor(() => { | ||
| expect(mockModal.querySelector('.search-filters')).toBeInTheDocument(); | ||
| }); | ||
| } | ||
|
|
||
| it('should render both select dropdowns inside the modal', async () => { | ||
| renderFilters(); | ||
| await waitForFilters(); | ||
| const selects = mockModal.querySelectorAll('.search-filters__select'); | ||
| expect(selects.length).toBe(2); | ||
| }); | ||
|
|
||
| it('should include "All" option in platform select', async () => { | ||
| renderFilters(); | ||
| await waitForFilters(); | ||
| const platformSelect = mockModal.querySelectorAll('.search-filters__select')[0] as HTMLSelectElement; | ||
| const options = Array.from(platformSelect.options).map((o) => o.value); | ||
| expect(options).toContain('all'); | ||
| expect(options).toContain('react'); | ||
| }); | ||
|
|
||
| it('should include Gen options in version select', async () => { | ||
| renderFilters(); | ||
| await waitForFilters(); | ||
| const genSelect = mockModal.querySelectorAll('.search-filters__select')[1] as HTMLSelectElement; | ||
| const options = Array.from(genSelect.options).map((o) => o.value); | ||
| expect(options).toEqual(['gen2', 'gen1', 'both']); | ||
| }); | ||
|
|
||
| it('should reflect current platform value', async () => { | ||
| renderFilters('swift', 'gen2'); | ||
| await waitForFilters(); | ||
| const platformSelect = mockModal.querySelectorAll('.search-filters__select')[0] as HTMLSelectElement; | ||
| expect(platformSelect.value).toBe('swift'); | ||
| }); | ||
|
|
||
| it('should call onPlatformChange when platform is changed', async () => { | ||
| renderFilters('react', 'gen2'); | ||
| await waitForFilters(); | ||
| const platformSelect = mockModal.querySelectorAll('.search-filters__select')[0] as HTMLSelectElement; | ||
| await act(async () => { | ||
| fireEvent.change(platformSelect, { target: { value: 'angular' } }); | ||
| }); | ||
| expect(onPlatformChange).toHaveBeenCalledWith('angular'); | ||
| }); | ||
|
|
||
| it('should call onGenChange when version is changed', async () => { | ||
| renderFilters('react', 'gen2'); | ||
| await waitForFilters(); | ||
| const genSelect = mockModal.querySelectorAll('.search-filters__select')[1] as HTMLSelectElement; | ||
| await act(async () => { | ||
| fireEvent.change(genSelect, { target: { value: 'gen1' } }); | ||
| }); | ||
| expect(onGenChange).toHaveBeenCalledWith('gen1'); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { SearchFilters } from './SearchFilters'; | ||
| export type { GenFilter, PlatformFilter } from './SearchFilters'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why using 'all' by default?
could we not utilize
currentPlatformand fallback to all?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We intentionally default to "All" so search covers the full docs by default — users often search for concepts that span platforms (e.g. "auth", "data modeling"). The platform sidebar already filters navigation to the current platform, so having search default to "All" gives a broader discovery experience. If the user wants platform-specific results, they can narrow via the dropdown.