import { cn } from "@suphark/ui/lib/utils"; import type * as React from "react"; // แยก Props พื้นฐานที่ใช้ร่วมกันออกมา (Common props) interface PrimarySecondaryTextBaseProps { /** ข้อความหลัก (ชื่อ, เลขเอกสาร …) */ primary: React.ReactNode; /** ข้อความรอง (อีเมล, รหัส …) — ไม่ส่ง/null/"" = ไม่แสดงบรรทัดรอง (0 แสดง) */ secondary?: React.ReactNode; /** class ของกล่องนอกสุด */ className?: string; /** class ของข้อความหลัก (บน/ซ้าย) */ primaryClassName?: string; /** class ของข้อความรอง (ล่าง/ขวา) — ค่าเริ่มต้น "text-xs text-muted-foreground" */ secondaryClassName?: string; } // สร้าง Type ที่แตกต่างกันเพื่อใช้ทำ Discriminated Union // แบบที่ 1: แนวนอน (default) type VerticalProps = { /** * Specifies the display layout direction. * @default 'vertical' */ orientation?: "vertical"; /** * Separator is not allowed for vertical orientation. * Providing this prop when orientation is 'vertical' will cause a TypeScript error. */ separator?: never; // สำคัญ! ห้ามใช้ 'separator' เมื่อ orientation เป็น 'vertical' }; // แบบที่ 2: แนวนอน type HorizontalProps = { orientation: "horizontal"; /** * Specifies the separator style *only* when orientation is 'horizontal'. * - 'pipe': Displays | * - 'dash': Displays - * - 'parentheses': Wraps the secondary text in ( ). * If omitted, primary and secondary are separated by a space. */ separator?: "pipe" | "parentheses" | "dash"; // [EDIT] กลับเป็น string union + เพิ่ม 'dash' }; /** * รวม Props ทั้งหมดเข้าด้วยกัน * Combines base props with the orientation-specific props. * This creates a discriminated union based on the 'orientation' prop. */ export type PrimarySecondaryTextProps = PrimarySecondaryTextBaseProps & (VerticalProps | HorizontalProps) & { /** * Array of items to display. If provided, primary/secondary props are ignored (or used as fallback if logic dictates, but typically mutually exclusive in usage). */ items?: { primary: React.ReactNode; secondary?: React.ReactNode; }[]; /** * Style of the list when items are provided. * @default 'none' */ listStyle?: "none" | "bullet"; /** * Gap between items in the list (tailwind class). * @default 'gap-1' for vertical, 'gap-2' for horizontal (if supported later) or standard vertical stack gap. */ gap?: string; }; /** * ข้อความสองระดับ: หลัก + รอง ซ้อนบน–ล่าง (ค่าเริ่มต้น) หรือเรียงซ้าย–ขวา (`orientation="horizontal"` + `separator`) * เช่น ชื่อ + อีเมล, เลขเอกสาร + ชื่อโครงการ · `items` = แสดงหลายคู่เป็นรายการ */ export function PrimarySecondaryText({ primary, secondary, className, primaryClassName, secondaryClassName, orientation = "vertical", separator, items, listStyle = "none", gap, }: PrimarySecondaryTextProps) { const isHorizontal = orientation === "horizontal"; const defaultSecondaryStyles = "text-xs text-muted-foreground"; // Helper to render a single item const renderItem = (p: React.ReactNode, s: React.ReactNode, idx?: number) => { // 0 เป็นค่าที่แสดงได้ (`{0 && …}` เดิม render "0" ดิบ ๆ นอกบรรทัดรอง) — ไม่แสดงเฉพาะ null/undefined/false/"" const hasSecondary = s != null && s !== false && s !== ""; let separatorElement: React.ReactNode = null; if (isHorizontal && hasSecondary) { if (separator === "pipe") { separatorElement = ( ); } else if (separator === "dash") { separatorElement = ( ); } else if (!separator && separator !== "parentheses") { separatorElement = ; } } const content = (
{p} {hasSecondary && ( <> {separatorElement} {isHorizontal && separator === "parentheses" ? <>({s}) : s} )}
); if (listStyle === "bullet") { return (
• {content}
); } return
{content}
; }; // Render List if (items && items.length > 0) { return (
{items.map((item, idx) => renderItem(item.primary, item.secondary, idx))}
); } // Render Single (Legacy/Direct mode) return
{renderItem(primary, secondary)}
; }