"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { Badge } from "@suphark/ui/components/ui/badge"; import { Popover, PopoverContent, PopoverTrigger } from "@suphark/ui/components/ui/popover"; import { Separator } from "@suphark/ui/components/ui/separator"; import { cn } from "@suphark/ui/lib/utils"; import type { Column } from "@tanstack/react-table"; import { Loader2 } from "lucide-react"; import * as React from "react"; import { ActionAdd } from "../shared/semantic-icons"; export interface FacetedFilterOption { label: string; value: string | number | boolean; icon?: React.ComponentType<{ className?: string }>; } // รายการตัวเลือก (cmdk ~17 kB gz) โหลดเมื่อจะเปิดตัวกรอง — เริ่มตั้งแต่ชี้/โฟกัสปุ่ม (0.97.0, P9) // cache ระดับโมดูล (ไม่ใช้ React.lazy): โหลดแล้ว = render ทันทีตั้งแต่ครั้งแรก ไม่ suspend type ListModule = typeof import("./data-table-faceted-filter-list"); let listModule: ListModule | null = null; let listLoading: Promise | null = null; /** โหลดรายการตัวเลือกของตัวกรองล่วงหน้า — เทสต์ที่เปิดตัวกรองแล้วตรวจรายการทันทีใช้ `beforeAll(() => preloadDataTableFacetedFilter())` */ export function preloadDataTableFacetedFilter(): Promise { listLoading ??= import("./data-table-faceted-filter-list").then( (mod) => { listModule = mod; return mod; }, (error: unknown) => { // โหลด chunk ไม่ได้ — ให้ลองใหม่ตอนชี้/เปิดครั้งหน้า listLoading = null; throw error; }, ); return listLoading; } function FacetedFilterLoading({ compact }: { compact: boolean }) { const text = useUiText(); return (
); } interface DataTableFacetedFilterProps { column?: Column; title?: string; options: FacetedFilterOption[]; compact?: boolean; fullWidth?: boolean; } export function DataTableFacetedFilter({ column, title, options, compact = false, fullWidth = false, }: DataTableFacetedFilterProps) { const text = useUiText(); const [listState, setList] = React.useState(listModule); // ตัวกรองอื่นโหลดไปแล้วหลังตัวนี้ mount = ใช้ได้เลย (ไม่โชว์วงหมุน/ไม่เข้าทางย้ายโฟกัส) const list = listState ?? listModule; const [openedWhileLoading, setOpenedWhileLoading] = React.useState(false); // เปิดด้วยการแตะ — Base UI ตั้งใจโฟกัสกรอบ popup (ไม่ให้แป้นพิมพ์มือถือเด้งทับรายการ) ห้ามย้ายไปช่องค้นหา const touchOpen = React.useRef(false); const loadList = () => { if (list) return; preloadDataTableFacetedFilter().then(setList, () => {}); }; const filterValue = column?.getFilterValue(); // Set + ลำดับ memo จากค่า filter จริง (เดิมสร้าง Set ใหม่ทุก render ทำให้ memo ไม่เคย hit) const { selectedValues, sortedOptions } = React.useMemo(() => { const selected = new Set(Array.isArray(filterValue) ? filterValue.map(String) : []); const sorted = [...options].sort((a, b) => { const aSelected = selected.has(String(a.value)); const bSelected = selected.has(String(b.value)); if (aSelected && !bSelected) return -1; if (!aSelected && bSelected) return 1; return 0; }); return { selectedValues: selected, sortedOptions: sorted }; }, [options, filterValue]); return ( { // เปิดระหว่างยังโหลด = Base UI เลือกโฟกัสตัว popup ไว้แล้ว — ให้รายการย้ายไปช่องค้นหาเองตอนมาถึง setOpenedWhileLoading(open && !list && !touchOpen.current); if (open) loadList(); }} > { touchOpen.current = event.pointerType === "touch"; }} onKeyDown={() => { touchOpen.current = false; }} render={