SPB Git

Repository Public

controls.tsx 8.3 KB 215 lines tsx

1// Contrôles de filtre partagés entre la barre (menus du bureau) et la feuille2// « Filtres » (mobile et filtres avancés) : même logique, deux présentations.34import { useEffect, useRef, useState, type ReactNode } from "react";5import { Check, ChevronDown } from "lucide-react";6import { cn } from "@/lib/cn";7import { fmtInt } from "@/lib/format";8import { fmtCompact, PRICE_STEPS, TYPE_GROUPS, typeCount, type Facets } from "@/lib/api.recherche";9import { iconForType } from "@/brand/compat";10import type { Filters, Patch } from "./filters";1112// --- Menu déroulant (bureau) --------------------------------------------------------13export function Menu({14  label,15  active,16  children,17  width = "w-72",18  align = "left",19}: {20  label: ReactNode;21  active?: boolean;22  children: (close: () => void) => ReactNode;23  width?: string;24  align?: "left" | "right";25}) {26  const [open, setOpen] = useState(false);27  const wrap = useRef<HTMLDivElement>(null);28  useEffect(() => {29    if (!open) return;30    const away = (e: PointerEvent) => {31      if (!wrap.current?.contains(e.target as Node)) setOpen(false);32    };33    const key = (e: KeyboardEvent) => e.key === "Escape" && setOpen(false);34    document.addEventListener("pointerdown", away);35    document.addEventListener("keydown", key);36    return () => {37      document.removeEventListener("pointerdown", away);38      document.removeEventListener("keydown", key);39    };40  }, [open]);41  return (42    <div ref={wrap} className="relative shrink-0">43      <button44        type="button"45        aria-haspopup="dialog"46        aria-expanded={open}47        onClick={() => setOpen((o) => !o)}48        className={cn(49          "inline-flex h-10 items-center gap-1.5 whitespace-nowrap rounded-[10px] border pl-3.5 pr-2.5 text-[0.88rem] font-medium transition-colors duration-150",50          active ? "border-ink bg-ink text-bg" : open ? "border-ink-3 bg-surface text-ink" : "border-line-2 bg-surface text-ink hover:border-ink-3",51        )}52      >53        {label}54        <ChevronDown className={cn("size-3.5 opacity-70 transition-transform duration-200", open && "rotate-180")} />55      </button>56      {open && (57        <div58          role="dialog"59          className={cn("ik-rise absolute top-[calc(100%+8px)] z-40 rounded-[12px] border border-line-2 bg-surface p-4 shadow-float", width, align === "right" ? "right-0" : "left-0")}60        >61          {children(() => setOpen(false))}62        </div>63      )}64    </div>65  );66}6768// --- Type de propriété ----------------------------------------------------------------69/** Liste à cocher des familles de types, avec leur nombre réel d'inscriptions. */70export function TypeChoices({ filters, set, facets, dense }: { filters: Filters; set: (p: Patch) => void; facets: Facets | undefined; dense?: boolean }) {71  const toggle = (id: string) => {72    const next = filters.types.includes(id) ? filters.types.filter((t) => t !== id) : [...filters.types, id];73    set({ property_type: next.join(",") || null });74  };75  // un type exact venu d'un lien (« Duplex ») reste visible et décochable76  const extra = filters.types.filter((t) => !TYPE_GROUPS.some((g) => g.id === t));77  const rows = [...TYPE_GROUPS.map((g) => ({ id: g.id, label: g.plural })), ...extra.map((t) => ({ id: t, label: t }))];78  return (79    <ul className={cn("grid", dense ? "grid-cols-1" : "grid-cols-1 gap-x-6 sm:grid-cols-2")}>80      {rows.map(({ id, label }) => {81        const on = filters.types.includes(id);82        const n = typeCount(facets, id);83        const Icon = iconForType(id);84        return (85          <li key={id}>86            <button87              type="button"88              role="checkbox"89              aria-checked={on}90              onClick={() => toggle(id)}91              className={cn("group flex min-h-11 w-full items-center gap-3 rounded-[8px] px-1.5 text-left text-[0.92rem] transition-colors duration-100 hover:bg-surface-2", dense && "min-h-10")}92            >93              <span className={cn("grid size-[18px] shrink-0 place-items-center rounded-[5px] border transition-colors duration-150", on ? "border-ink bg-ink text-bg" : "border-line-2 bg-surface")}>94                {on && <Check className="size-3" strokeWidth={3} />}95              </span>96              <Icon size={19} className={cn("shrink-0", on ? "text-ink" : "text-ink-3")} />97              <span className={cn("min-w-0 flex-1 truncate", on ? "font-medium text-ink" : "text-ink")}>{label}</span>98              {n != null && <span className="num shrink-0 text-[0.8rem] text-ink-3">{fmtInt(n)}</span>}99            </button>100          </li>101        );102      })}103    </ul>104  );105}106107// --- Prix ------------------------------------------------------------------------------108const QUICK_MAX = [300_000, 500_000, 750_000, 1_000_000];109110function PriceSelect({ label, value, onChange, steps, none }: { label: string; value: number | null; onChange: (v: number | null) => void; steps: number[]; none: string }) {111  // une valeur venue d'un lien et absente des paliers reste sélectionnée112  const all = value && !steps.includes(value) ? [...steps, value].sort((a, b) => a - b) : steps;113  return (114    <label className="block min-w-0">115      <span className="mb-1.5 block text-[0.8rem] text-ink-3">{label}</span>116      <span className="relative block">117        <select118          value={value ?? ""}119          onChange={(e) => onChange(e.target.value ? Number(e.target.value) : null)}120          className={cn("field num cursor-pointer appearance-none pr-8 text-[0.92rem]", !value && "text-ink-3")}121        >122          <option value="">{none}</option>123          {all.map((p) => (124            <option key={p} value={p}>125              {fmtCompact(p)}126            </option>127          ))}128        </select>129        <ChevronDown className="pointer-events-none absolute right-2.5 top-1/2 size-4 -translate-y-1/2 text-ink-3" />130      </span>131    </label>132  );133}134135export function PriceFields({ filters, set }: { filters: Filters; set: (p: Patch) => void }) {136  const { priceMin: min, priceMax: max } = filters;137  return (138    <div>139      <div className="grid grid-cols-2 gap-3">140        <PriceSelect141          label="Minimum"142          none="Aucun"143          value={min}144          steps={PRICE_STEPS.filter((p) => !max || p < max)}145          onChange={(v) => set({ price_min: v ? String(v) : null })}146        />147        <PriceSelect148          label="Maximum"149          none="Aucun"150          value={max}151          steps={PRICE_STEPS.filter((p) => !min || p > min)}152          onChange={(v) => set({ price_max: v ? String(v) : null })}153        />154      </div>155      <div className="mt-3 flex flex-wrap gap-2">156        {QUICK_MAX.map((p) => {157          const on = max === p && !min;158          return (159            <button160              key={p}161              type="button"162              aria-pressed={on}163              onClick={() => set({ price_min: null, price_max: on ? null : String(p) })}164              className={cn(165                "num inline-flex h-9 items-center rounded-[8px] border px-2.5 text-[0.82rem] font-medium transition-colors duration-150 max-lg:h-11 max-lg:px-3",166                on ? "border-ink bg-ink text-bg" : "border-line-2 text-ink-2 hover:border-ink-3 hover:text-ink",167              )}168            >169              ≤ {fmtCompact(p)}170            </button>171          );172        })}173      </div>174    </div>175  );176}177178// --- Paliers « n et plus » (chambres, salles de bain, superficies) --------------------------179export function Steps({180  label,181  value,182  options,183  onChange,184  format = (n) => `${n}+`,185}: {186  label: string;187  value: number | null;188  options: number[];189  onChange: (v: number | null) => void;190  format?: (n: number) => string;191}) {192  return (193    <div role="radiogroup" aria-label={label} className="flex flex-wrap gap-2">194      {[null, ...options].map((o) => {195        const on = value === o;196        return (197          <button198            key={o ?? "tous"}199            type="button"200            role="radio"201            aria-checked={on}202            onClick={() => onChange(o)}203            className={cn(204              "num inline-flex h-10 min-w-11 items-center justify-center rounded-[8px] border px-3 text-[0.86rem] font-medium transition-colors duration-150 max-lg:h-11",205              on ? "border-ink bg-ink text-bg" : "border-line-2 text-ink-2 hover:border-ink-3 hover:text-ink",206            )}207          >208            {o == null ? "Tous" : format(o)}209          </button>210        );211      })}212    </div>213  );214}215
Files main
Menu

Theme