feat(app): collapsible sheet filters on phone, one empty announcement

This commit is contained in:
bchanot committed 2026-11-16 20:30:27 +01:00
1 parent 5014a45053
commit 9efc7b7647
4 files changed
+220 -5

No files matched your search

@@ -99,7 +99,24 @@ function useListView() {
const [filters, setFilters] = useState<SheetFilters>(NO_FILTER)
const [sort, setSort] = useState<SheetSort>('modified')
const search = useSearch()
return { filters, setFilters, sort, setSort, ...search }
const [open, setOpen] = useState(false)
const toggleRef = useRef<HTMLButtonElement>(null)
/** The reset button disappears with the last filter: the focus moves to the toggle. */
const resetFilters = () => {
setFilters(NO_FILTER)
toggleRef.current?.focus()
}
return {
...search,
filters,
setFilters,
sort,
setSort,
open,
setOpen,
toggleRef,
resetFilters,
}
}
type ListView = ReturnType<typeof useListView>
@@ -255,7 +272,7 @@ const STATUS_FILTERS = [
] as const
/** The four filters of §11.1, unchanged; species offered are those of the loaded list. */
function Filters({ sheets, species, view }: ResultsProps) {
function FilterGrid({ sheets, species, view }: ResultsProps) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const speciesOptions = [...new Set(sheets.map((s) => s.species_id))].map(
@@ -285,6 +302,84 @@ function Filters({ sheets, species, view }: ResultsProps) {
)
}
const FILTERS_PANEL_ID = 'admin-sheets-filters'
/** Phone only: « Filtres » or « Filtres (n) », n the filters that are not empty. */
function FiltersToggle({ view }: { view: ListView }) {
const t = useT()
const active = Object.values(view.filters).filter(Boolean).length
return (
<Button
ref={view.toggleRef}
type="button"
variant="secondary"
aria-expanded={view.open}
aria-controls={FILTERS_PANEL_ID}
onClick={() => view.setOpen((o) => !o)}
className="min-h-11 w-full justify-between sm:hidden"
>
{active === 0
? t('admin.sheets.filters')
: t('admin.sheets.filters_active').replace('{count}', String(active))}
<Icon
name="chevron-down"
size={16}
className={
'transition-transform motion-reduce:transition-none ' +
(view.open ? 'rotate-180' : '')
}
/>
</Button>
)
}
/** Phone only, inside the panel, while at least one filter is not empty. */
function ResetFilters({ view }: { view: ListView }) {
const t = useT()
if (!Object.values(view.filters).some(Boolean)) return null
return (
<Button
type="button"
variant="secondary"
onClick={view.resetFilters}
className="mt-3 min-h-11 w-full sm:hidden"
>
<Icon name="close" size={14} className="mr-1" />
{t('admin.sheets.filters_reset')}
</Button>
)
}
// A closed panel is collapsed (grid row 0fr) and invisible, so nothing in it takes
// focus or a tap; visibility changes with the transition. `sm:` keeps the grid as
// it was on a desktop: always shown, no button.
const PANEL =
'grid transition-[grid-template-rows,visibility] duration-200 ' +
'motion-reduce:transition-none sm:block'
/** The filters: a collapsible panel on a phone (closed by default), the grid from `sm`. */
function Filters(props: ResultsProps) {
const { view } = props
const state = view.open
? 'max-sm:mt-3 max-sm:grid-rows-[1fr]'
: 'max-sm:grid-rows-[0fr] max-sm:invisible'
return (
<div>
<FiltersToggle view={view} />
<div
id={FILTERS_PANEL_ID}
data-open={view.open}
className={`${PANEL} ${state}`}
>
<div className="-m-1 min-h-0 overflow-hidden p-1 sm:overflow-visible">
<FilterGrid {...props} />
<ResetFilters view={view} />
</div>
</div>
</div>
)
}
/** « {n} fiches » when everything is shown, « {n} fiches sur {total} » otherwise. */
function CountLine({ shown, total }: { shown: number; total: number }) {
const plural = usePlural()
@@ -298,9 +393,16 @@ function CountLine({ shown, total }: { shown: number; total: number }) {
)
}
function EmptyLine({ text }: { text: string }) {
/** `announce` false when the count line already speaks for the empty result. */
function EmptyLine({
text,
announce = true,
}: {
text: string
announce?: boolean
}) {
return (
<p role="status" className={EMPTY_LINE}>
<p role={announce ? 'status' : undefined} className={EMPTY_LINE}>
{text}
</p>
)
@@ -359,7 +461,7 @@ function Results({ sheets, species, view }: ResultsProps) {
<CountLine shown={shown.length} total={sheets.length} />
{shown.length === 0 &&
(filtering ? (
<EmptyLine text={t('admin.sheets.no_match')} />
<EmptyLine text={t('admin.sheets.no_match')} announce={false} />
) : (
<SearchNoResult view={view} />
))}
+3
View File
@@ -1038,6 +1038,9 @@ export const en: Dict = {
'admin.sheets.count_of_one': '{count} sheet of {total}',
'admin.sheets.count_of_other': '{count} sheets of {total}',
'admin.sheets.modified': 'Modified on {date}',
'admin.sheets.filters': 'Filters',
'admin.sheets.filters_active': 'Filters ({count})',
'admin.sheets.filters_reset': 'Reset filters',
'admin.sheets.no_result': 'No sheet name contains “{q}”.',
'admin.sheet_form.lede': 'The sheet starts empty: views, annotations and findings are entered later in the editor.',
'admin.sheet_form.species': 'Species',
+3
View File
@@ -1069,6 +1069,9 @@ export const fr: Dict = {
'admin.sheets.count_of_one': '{count} fiche sur {total}',
'admin.sheets.count_of_other': '{count} fiches sur {total}',
'admin.sheets.modified': 'Modifiée le {date}',
'admin.sheets.filters': 'Filtres',
'admin.sheets.filters_active': 'Filtres ({count})',
'admin.sheets.filters_reset': 'Réinitialiser les filtres',
'admin.sheets.no_result': 'Aucune fiche ne contient « {q} » dans son nom.',
'admin.sheet_form.lede': "La fiche démarre vide : vues, annotations et constats se saisissent ensuite dans l'éditeur.",
'admin.sheet_form.species': 'Espèce',
+107
View File
@@ -920,6 +920,113 @@ describe('Official sheets list (H1)', () => {
)
expect(screen.getByText('1 sheet of 3')).toBeInTheDocument()
})
it('announces an empty filtered result once: the count line, not a status', async () => {
const user = userEvent.setup()
await mountThree()
await user.selectOptions(
screen.getByLabelText('Statut de version'),
'published',
)
const note = screen.getByText('Aucune fiche ne correspond à ces filtres.')
expect(note.closest('[role="status"]')).toBeNull()
expect(screen.getByText('0 fiche sur 3')).toHaveAttribute(
'aria-live',
'polite',
)
})
describe('collapsible filters on a phone', () => {
const toggle = (name: RegExp | string = /^Filtres/) =>
screen.getByRole('button', { name })
const panel = () => document.getElementById('admin-sheets-filters')!
const reset = () =>
screen.queryByRole('button', { name: 'Réinitialiser les filtres' })
it('is closed by default: aria-expanded false, controls the panel, hidden on a phone only', async () => {
await mountThree()
expect(toggle()).toHaveTextContent('Filtres')
expect(toggle()).toHaveAttribute('aria-expanded', 'false')
expect(toggle()).toHaveAttribute('aria-controls', 'admin-sheets-filters')
expect(toggle()).toHaveClass('sm:hidden')
expect(panel()).toHaveAttribute('data-open', 'false')
expect(panel().className).toContain('max-sm:invisible')
expect(panel().className).toContain('motion-reduce:transition-none')
expect(panel().className).not.toMatch(/(^|\s)invisible/)
})
it('opens and closes on the button, with the four filters inside', async () => {
const user = userEvent.setup()
await mountThree()
await user.click(toggle())
expect(toggle()).toHaveAttribute('aria-expanded', 'true')
expect(panel()).toHaveAttribute('data-open', 'true')
expect(panel().className).not.toContain('max-sm:invisible')
for (const label of [
'Espèce',
'État de la fiche',
'Statut de version',
'État de la dernière revue',
])
expect(within(panel()).getByLabelText(label)).toBeInTheDocument()
await user.click(toggle())
expect(toggle()).toHaveAttribute('aria-expanded', 'false')
expect(panel().className).toContain('max-sm:invisible')
})
it('counts the active filters on the button; closing keeps them', async () => {
const user = userEvent.setup()
const list = await mountThree()
await user.click(toggle())
await user.selectOptions(screen.getByLabelText('Espèce'), 'equine')
await user.selectOptions(
screen.getByLabelText('État de la fiche'),
'active',
)
expect(toggle()).toHaveTextContent('Filtres (2)')
await user.click(toggle(/^Filtres \(2\)/))
expect(toggle()).toHaveTextContent('Filtres (2)')
await user.click(toggle())
expect(screen.getByLabelText('Espèce')).toHaveValue('equine')
expect(screen.getByLabelText('État de la fiche')).toHaveValue('active')
expect(list.getAllByRole('link').length).toBeGreaterThan(0)
})
it('resets every filter, keeps the search, and hands the focus to the button', async () => {
const user = userEvent.setup()
await mountThree()
expect(reset()).toBeNull()
await user.click(toggle())
await user.type(searchField(), 'chev')
await user.selectOptions(
screen.getByLabelText('Statut de version'),
'published',
)
expect(toggle()).toHaveTextContent('Filtres (1)')
await user.click(reset()!)
expect(screen.getByLabelText('Statut de version')).toHaveValue('')
expect(toggle()).toHaveTextContent(/^Filtres$/)
expect(searchField()).toHaveValue('chev')
expect(reset()).toBeNull()
expect(toggle()).toHaveFocus()
})
it('reads in English', async () => {
useLocaleStore.setState({ locale: 'en' })
const user = userEvent.setup()
fake.rpcResults.list_official_sheets_admin = { data: threeSheets() }
mount('/app/admin/sheets', [grant('sheet_editor')])
await screen.findByRole('list', { name: 'Official sheet list' })
await user.click(screen.getByRole('button', { name: 'Filters' }))
await user.selectOptions(screen.getByLabelText('Version status'), 'draft')
expect(
screen.getByRole('button', { name: 'Filters (1)' }),
).toBeInTheDocument()
expect(
screen.getByRole('button', { name: 'Reset filters' }),
).toBeInTheDocument()
})
})
})
describe('Sheet creation (A1)', () => {