feat(suggest): school autosuggest, and the rate-limit fix it needed first #127
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { SuggestList, suggestOptionId } from '@/components/SuggestList';
|
||||
|
||||
const ROWS = [
|
||||
{ urn: 1, school_name: "St Mary's Primary", local_authority: 'Camden',
|
||||
postcode: 'NW1 1AA', phase: 'Primary', school_type: 'Voluntary aided school' },
|
||||
{ urn: 2, school_name: "St Mary's Primary", local_authority: 'Barnet',
|
||||
postcode: 'EN5 2AA', phase: 'Primary', school_type: 'Community school' },
|
||||
];
|
||||
|
||||
describe('SuggestList', () => {
|
||||
it('is a listbox of options', () => {
|
||||
render(<SuggestList id="s" suggestions={ROWS} activeIndex={-1}
|
||||
onPick={() => {}} onHover={() => {}} />);
|
||||
expect(screen.getByRole('listbox')).toBeInTheDocument();
|
||||
expect(screen.getAllByRole('option')).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('shows the local authority, which is what tells two schools apart', () => {
|
||||
// Both rows are "St Mary's Primary". Without the authority the list is
|
||||
// unusable for exactly the query autosuggest exists to serve.
|
||||
render(<SuggestList id="s" suggestions={ROWS} activeIndex={-1}
|
||||
onPick={() => {}} onHover={() => {}} />);
|
||||
expect(screen.getByText('Camden')).toBeInTheDocument();
|
||||
expect(screen.getByText('Barnet')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('marks only the active option selected', () => {
|
||||
render(<SuggestList id="s" suggestions={ROWS} activeIndex={1}
|
||||
onPick={() => {}} onHover={() => {}} />);
|
||||
const options = screen.getAllByRole('option');
|
||||
expect(options[0]).toHaveAttribute('aria-selected', 'false');
|
||||
expect(options[1]).toHaveAttribute('aria-selected', 'true');
|
||||
});
|
||||
|
||||
it('gives each option the id the input will point at', () => {
|
||||
// aria-activedescendant on the input has to name a real element id, or
|
||||
// a screen reader announces nothing as the user arrows through.
|
||||
render(<SuggestList id="s" suggestions={ROWS} activeIndex={0}
|
||||
onPick={() => {}} onHover={() => {}} />);
|
||||
expect(screen.getAllByRole('option')[0]).toHaveAttribute(
|
||||
'id', suggestOptionId('s', 0));
|
||||
});
|
||||
|
||||
it('renders nothing when there is nothing to suggest', () => {
|
||||
const { container } = render(<SuggestList id="s" suggestions={[]}
|
||||
activeIndex={-1} onPick={() => {}} onHover={() => {}} />);
|
||||
expect(container).toBeEmptyDOMElement();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Anchored to .omniBoxContainer, which is position: relative for this reason.
|
||||
*
|
||||
* Every colour is a token, so the dropdown follows the theme. The dark theme
|
||||
* redefines --bg-card, --border, --text-muted and --shadow-soft, and this
|
||||
* inherits all four without a second rule.
|
||||
*/
|
||||
.list {
|
||||
position: absolute;
|
||||
top: calc(100% + 4px);
|
||||
left: 0;
|
||||
right: 0;
|
||||
/* Above the sticky filter bar (10) and the hero layers (0–2), below the
|
||||
skip-link (10000) and the modal overlay (1000). */
|
||||
z-index: 40;
|
||||
margin: 0;
|
||||
padding: 4px;
|
||||
list-style: none;
|
||||
max-height: 320px;
|
||||
overflow-y: auto;
|
||||
background: var(--bg-card);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius-md);
|
||||
box-shadow: var(--shadow-soft);
|
||||
}
|
||||
|
||||
.option {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
padding: 10px 12px;
|
||||
border-radius: var(--radius-sm);
|
||||
cursor: pointer;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
/* Hover and keyboard share one style: the active option is the active option
|
||||
however it became active. Two rules would drift. */
|
||||
.option:hover,
|
||||
.active {
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
|
||||
.name {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.meta {
|
||||
font-size: 0.85em;
|
||||
color: var(--text-muted);
|
||||
white-space: nowrap;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
'use client';
|
||||
|
||||
/**
|
||||
* The autosuggest dropdown. Presentational only — it fetches nothing and owns
|
||||
* no state, so the fetching rules and the ARIA rules can be read separately.
|
||||
*/
|
||||
|
||||
import type { Suggestion } from '@/lib/suggest';
|
||||
import styles from './SuggestList.module.css';
|
||||
|
||||
/** The id the input's aria-activedescendant points at. */
|
||||
export function suggestOptionId(id: string, index: number): string {
|
||||
return `${id}-option-${index}`;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
/** Shared with the input's aria-controls. */
|
||||
id: string;
|
||||
suggestions: Suggestion[];
|
||||
activeIndex: number;
|
||||
onPick: (s: Suggestion) => void;
|
||||
onHover: (index: number) => void;
|
||||
}
|
||||
|
||||
export function SuggestList({ id, suggestions, activeIndex, onPick, onHover }: Props) {
|
||||
if (suggestions.length === 0) return null;
|
||||
|
||||
return (
|
||||
<ul className={styles.list} id={id} role="listbox">
|
||||
{suggestions.map((s, i) => (
|
||||
<li
|
||||
key={s.urn}
|
||||
id={suggestOptionId(id, i)}
|
||||
role="option"
|
||||
aria-selected={i === activeIndex}
|
||||
className={`${styles.option} ${i === activeIndex ? styles.active : ''}`}
|
||||
/*
|
||||
* onMouseDown, not onClick: the input's blur handler closes the list,
|
||||
* and blur fires before click — so a click handler never runs. This
|
||||
* is the classic autosuggest bug where the dropdown is unclickable
|
||||
* with a mouse while working perfectly with a keyboard.
|
||||
*/
|
||||
onMouseDown={(e) => { e.preventDefault(); onPick(s); }}
|
||||
onMouseEnter={() => onHover(i)}
|
||||
>
|
||||
<span className={styles.name}>{s.school_name}</span>
|
||||
{/* Not decoration: there are many "St Mary's". */}
|
||||
<span className={styles.meta}>{s.local_authority}</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user