- 7 pruebas del componente: sección única, sin subcategoría, buscador por provincia, chips con conteo/aria-pressed, orden segmentado (distancia solo con ubicación), Limpiar global y favoritos. - Foco visible en la barra vía :focus-visible (inputStyle anula el outline por defecto en inputs/selects). - Verificado que los marcadores del mapa usan localesFiltrados. 146/146 tests y build en verde.
142 lines
6.7 KiB
React
142 lines
6.7 KiB
React
import { CATEGORIAS, NOMBRES_CATEGORIAS } from "../constants/categorias.js";
|
|
import { PROVINCIAS } from "../constants/provincias.js";
|
|
import { inputStyle } from "../styles/shared.js";
|
|
|
|
// UI de filtrado del directorio. El estado vive en App.jsx (la derivación de
|
|
// localesFiltrados la comparten lista y mapa); este componente solo pinta y
|
|
// dispara los setters recibidos por props.
|
|
//
|
|
// Diseño (openspec unificar-filtros-iu): una única sección con tres filas —
|
|
// búsqueda + provincia compacta, chips de categorías y acciones rápidas con
|
|
// el orden segmentado y el «✕ Limpiar» global. Sin control de subcategoría
|
|
// (los subtipos se replantean en la issue #22) y sin desplegable de orden.
|
|
// Visible en las vistas de lista y mapa (mismos criterios en ambas).
|
|
const OPCIONES_ORDEN = [
|
|
{ valor: "fecha", etiqueta: "🕒 Reciente" },
|
|
{ valor: "puntuacion", etiqueta: "★ Mejor" },
|
|
{ valor: "nombre", etiqueta: "A-Z" },
|
|
];
|
|
const OPCION_ORDEN_DISTANCIA = { valor: "distancia", etiqueta: "📍 Más cerca" };
|
|
|
|
const estiloChip = {
|
|
borderRadius: 20,
|
|
padding: "5px 12px",
|
|
fontSize: 12,
|
|
cursor: "pointer",
|
|
display: "flex",
|
|
alignItems: "center",
|
|
gap: 5,
|
|
transition: "all 0.15s",
|
|
};
|
|
|
|
export default function BarraFiltros({
|
|
locales,
|
|
favoritos,
|
|
busqueda, setBusqueda,
|
|
filtroCategoria, setFiltroCategoria,
|
|
filtroProvincia, setFiltroProvincia,
|
|
orden, setOrden,
|
|
soloFavoritos, setSoloFavoritos,
|
|
ubicacion, buscandoUbicacion, errorUbicacion, pedirUbicacion, limpiarUbicacion,
|
|
}) {
|
|
const conteosCat = NOMBRES_CATEGORIAS.reduce((acc, c) => { acc[c] = locales.filter((l) => l.categoria === c).length; return acc; }, {});
|
|
const opcionesOrden = ubicacion ? [...OPCIONES_ORDEN, OPCION_ORDEN_DISTANCIA] : OPCIONES_ORDEN;
|
|
const hayFiltros = busqueda || filtroCategoria || filtroProvincia || soloFavoritos || orden !== "fecha" || ubicacion;
|
|
|
|
const limpiarTodo = () => {
|
|
setBusqueda("");
|
|
setFiltroCategoria("");
|
|
setFiltroProvincia("");
|
|
setSoloFavoritos(false);
|
|
setOrden("fecha");
|
|
if (ubicacion) limpiarUbicacion();
|
|
};
|
|
|
|
return (
|
|
<section
|
|
aria-label="Filtros de locales"
|
|
className="barra-filtros"
|
|
style={{
|
|
background: "var(--color-background-primary)",
|
|
border: "0.5px solid var(--color-border-tertiary)",
|
|
borderRadius: "var(--border-radius-lg)",
|
|
padding: "0.75rem",
|
|
marginBottom: "1.25rem",
|
|
display: "grid",
|
|
gap: "0.6rem",
|
|
}}
|
|
>
|
|
{/* Búsqueda + provincia compacta */}
|
|
<div style={{ display: "flex", gap: 8, flexWrap: "wrap" }}>
|
|
<input
|
|
style={{ ...inputStyle, flex: 1, minWidth: 140 }}
|
|
placeholder="🔍 Buscar por nombre, dirección o provincia..."
|
|
value={busqueda}
|
|
onChange={(e) => setBusqueda(e.target.value)}
|
|
aria-label="Buscar por nombre, dirección o provincia"
|
|
/>
|
|
<select style={{ ...inputStyle, width: "auto", flexShrink: 0 }} value={filtroProvincia} onChange={(e) => setFiltroProvincia(e.target.value)} aria-label="Filtrar por provincia">
|
|
<option value="">📍 Todas las provincias</option>
|
|
{PROVINCIAS.map((p) => <option key={p} value={p}>{p}</option>)}
|
|
</select>
|
|
</div>
|
|
|
|
{/* Chips categorías */}
|
|
<div style={{ display: "flex", flexWrap: "wrap", gap: 6 }}>
|
|
{NOMBRES_CATEGORIAS.map((cat) => {
|
|
const c = CATEGORIAS[cat];
|
|
const activo = filtroCategoria === cat;
|
|
return (
|
|
<button key={cat} onClick={() => setFiltroCategoria(activo ? "" : cat)}
|
|
style={{ ...estiloChip, background: activo ? c.bgLight : "var(--color-background-secondary)", color: activo ? c.textColor : "var(--color-text-secondary)", border: `0.5px solid ${activo ? c.textColor : "var(--color-border-tertiary)"}`, fontWeight: activo ? 500 : 400 }}
|
|
aria-pressed={activo}>
|
|
{c.emoji} {cat}
|
|
{conteosCat[cat] > 0 && <span style={{ background: activo ? c.textColor : "var(--color-border-secondary)", color: activo ? "white" : "var(--color-text-secondary)", borderRadius: 10, padding: "0 6px", fontSize: 11 }}>{conteosCat[cat]}</span>}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
|
|
{/* Acciones rápidas + orden + limpiar */}
|
|
<div style={{ display: "flex", alignItems: "center", gap: 10, flexWrap: "wrap" }}>
|
|
{!ubicacion ? (
|
|
<button onClick={pedirUbicacion} disabled={buscandoUbicacion}
|
|
style={{ ...estiloChip, background: "none", border: "0.5px solid var(--color-border-secondary)", color: "var(--color-text-secondary)" }}>
|
|
<i className="ti ti-current-location" aria-hidden="true"></i> {buscandoUbicacion ? "Localizando..." : "Cerca de mí"}
|
|
</button>
|
|
) : (
|
|
<button onClick={() => { limpiarUbicacion(); if (orden === "distancia") setOrden("fecha"); }}
|
|
style={{ ...estiloChip, background: "#EAFAF1", border: "0.5px solid #A9DFBF", color: "#1E8449" }}>
|
|
<i className="ti ti-map-pin-check" aria-hidden="true"></i> Ubicación activada ✕
|
|
</button>
|
|
)}
|
|
{errorUbicacion && <span style={{ fontSize: 12, color: "#922B21" }}>{errorUbicacion}</span>}
|
|
<button onClick={() => setSoloFavoritos((v) => !v)} aria-pressed={soloFavoritos}
|
|
style={{ ...estiloChip, background: soloFavoritos ? "#FDEDEC" : "none", border: `0.5px solid ${soloFavoritos ? "#8B0000" : "var(--color-border-secondary)"}`, color: soloFavoritos ? "#8B0000" : "var(--color-text-secondary)" }}>
|
|
<i className={soloFavoritos ? "ti ti-heart-filled" : "ti ti-heart"} aria-hidden="true"></i> Favoritos {favoritos.length > 0 && `(${favoritos.length})`}
|
|
</button>
|
|
|
|
{/* Orden segmentado (sin desplegable) */}
|
|
<div role="group" aria-label="Ordenar locales" style={{ display: "flex", overflow: "hidden", borderRadius: "var(--border-radius-md)", border: "0.5px solid var(--color-border-tertiary)", marginLeft: "auto" }}>
|
|
{opcionesOrden.map((o) => {
|
|
const activo = orden === o.valor;
|
|
return (
|
|
<button key={o.valor} onClick={() => setOrden(o.valor)} aria-pressed={activo}
|
|
style={{ border: "none", padding: "5px 10px", fontSize: 12, cursor: "pointer", background: activo ? "#8B0000" : "var(--color-background-secondary)", color: activo ? "white" : "var(--color-text-secondary)", fontWeight: activo ? 500 : 400, display: "flex", alignItems: "center" }}>
|
|
{o.etiqueta}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
|
|
{hayFiltros && (
|
|
<button onClick={limpiarTodo}
|
|
style={{ ...estiloChip, background: "none", border: "0.5px solid var(--color-border-secondary)", color: "var(--color-text-tertiary)" }}>
|
|
✕ Limpiar
|
|
</button>
|
|
)}
|
|
</div>
|
|
</section>
|
|
);
|
|
}
|