Ir al contenido

SortMenu

Cómo se ordena una lista o un conjunto de tarjetas: por qué campo y en qué sentido. Un botón dice el orden en uso y abre un menú de campos y sentidos.

import { SortMenu, SortMenuSkeleton } from '@sypnia/ui/react'

En esta página
  1. Tarjetas en orden
  2. Skeleton
  3. Cuándo usarlo
  4. Para todos
  5. Props

Tarjetas en orden

  • Shop
    Tables
    14
    Last change
    2026-11-16
  • Customers
    Tables
    22
    Last change
    2026-11-12
  • Blog
    Tables
    3
    Last change
    2026-11-02
  • Operations
    Tables
    8
    Last change
    2026-10-28
Código
import { useMemo, useState } from 'react';
import { DataCards, SortMenu, type DataSort } from '@sypnia/ui/react';

// Cards have no headers to press: the SortMenu says the order in use and changes it.
const projects = [
  { id: 'shop', name: 'Shop', tables: 14, updated: '2026-11-16' },
  { id: 'blog', name: 'Blog', tables: 3, updated: '2026-11-02' },
  { id: 'crm', name: 'Customers', tables: 22, updated: '2026-11-12' },
  { id: 'ops', name: 'Operations', tables: 8, updated: '2026-10-28' },
];
type P = (typeof projects)[number];
const by: Record<string, (p: P) => string | number> = { name: (p) => p.name, tables: (p) => p.tables, updated: (p) => p.updated };
export default function Projects() {
  const [sort, setSort] = useState<DataSort | null>({ id: 'updated', dir: 'descending' });
  const rows = useMemo(() => {
    if (!sort) return projects;
    const key = by[sort.id];
    return [...projects].sort((a, b) => (key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0) * (sort.dir === 'ascending' ? 1 : -1));
  }, [sort]);
  return (
    <div className="sy-stack">
      <div className="sy-row"><SortMenu options={[{ id: 'name', label: 'Name' }, { id: 'tables', label: 'Tables' }, { id: 'updated', label: 'Last change' }]} value={sort} onChange={setSort} /></div>
      <DataCards label="Projects" rows={rows} rowKey={(p) => p.id} title={(p) => p.name} min="12rem"
        fields={[{ id: 'tables', label: 'Tables', numeric: true, value: (p) => p.tables }, { id: 'updated', label: 'Last change', value: (p) => p.updated }]} />
    </div>
  );
}

Skeleton

Código
import { SortMenu, SortMenuSkeleton } from '@sypnia/ui/react';

// The button keeps its place while the fields are not known.
export default function Skeleton() {
  return (
    <div className="sy-row">
      <SortMenu options={[{ id: 'name', label: 'Name' }]} value={{ id: 'name', dir: 'ascending' }} onChange={() => {}} />
      <SortMenuSkeleton />
    </div>
  );
}

Cuándo usarlo

Úsalo cuando

  • Listas y tarjetas, que no tienen encabezados para pulsar.
  • Pasa none={false} cuando los datos no tienen un orden propio al cual volver.

No lo uses cuando

  • Tablas: sus encabezados ordenan.
  • Filtros: eso es FilterBar.

Para todos

  • El nombre del botón dice el orden completo: «Ordenar por Tamaño, ascendente».
  • El menú tiene dos grupos de opciones leídas como opciones de radio; las flechas mueven, Enter elige.

Props

SortMenuTipoPor defecto
onChangeobligatorio (sort: DataSort | null) => void
optionsobligatorio SortOption[]
valueobligatorio DataSort | null
none

Offers «No order», which goes back to the order the data came in.

boolean true
size "sm" | "md" sm
SortMenuSkeletonTipoPor defecto
size "sm" | "md" sm
  1. Tarjetas en orden
  2. Skeleton
  3. Cuándo usarlo
  4. Para todos
  5. Props

    Busca páginas, secciones y términos