"use client"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput, } from "@suphark/ui/components/ui/input-group"; import { cn } from "@suphark/ui/lib/utils"; import { type ComponentProps, useRef, useState } from "react"; import { ActionClose, ActionSearch } from "../semantic-icons"; export interface SearchInputProps extends Omit, "size" | "value" | "onChange" | "type"> { /** ไม่ส่ง = uncontrolled (คุมค่าในตัวเอง จาก `defaultValue`) — ใช้กับฟอร์มที่ submit เองได้ */ value?: string; /** รับค่าใหม่ตรง ๆ (ไม่ใช่ event) — ปุ่มล้างก็เรียกด้วย `""`; uncontrolled ก็ยังเรียกให้ */ onValueChange?: (value: string) => void; /** เรียกเพิ่มหลังกดปุ่มล้าง/Escape (เช่นรีเซ็ตหน้า pagination) */ onClear?: () => void; /** xs = h-7, sm = h-8 (ค่าเริ่มต้น), default = h-9 */ size?: "xs" | "sm" | "default"; /** aria-label ของช่อง (ค่าเริ่มต้น `UiText.search.label`) — ส่ง `aria-label` ตรง ๆ ก็ได้ ชนะ `label` */ label?: string; /** aria-label ของปุ่มล้าง (ค่าเริ่มต้น `UiText.search.clear`) */ clearLabel?: string; /** Escape ในช่องล้างค่า (ค่าเริ่มต้น true) */ clearOnEscape?: boolean; /** class ของกรอบ (InputGroup) — `className` ไปที่ `` */ groupClassName?: string; } /** * ช่องค้นหามาตรฐาน: ไอคอนแว่นขยายซ้าย + ปุ่มล้าง (X) ขวาเมื่อมีข้อความ + Escape ล้างค่า * ใช้ได้ทั้ง controlled (`value` + `onValueChange` — แอปคุม state/debounce เอง) และ uncontrolled * (`defaultValue` + `name` ในฟอร์มที่ submit ไป server action/URL param) — ซ้ำอยู่ใน Pi Auth/Pi PDF/DueBell * `onValueChange` ยิงทุกตัวอักษร (ไม่มี debounce ในตัว — ให้แอป debounce เองถ้าต้องยิง query) * * const [q, setQ] = useState(""); * *
*/ export function SearchInput({ value: valueProp, onValueChange, defaultValue, onClear, size = "sm", label, clearLabel, clearOnEscape = true, placeholder, className, groupClassName, disabled, onKeyDown, ref, "aria-label": ariaLabelProp, ...props }: SearchInputProps) { const text = useUiText(); const inputRef = useRef(null); // uncontrolled: เก็บค่าเองเพื่อให้ปุ่มล้าง/Escape ทำงาน และรู้ว่าจะโชว์ปุ่มล้างไหม const [uncontrolled, setUncontrolled] = useState(String(defaultValue ?? "")); const value = valueProp ?? uncontrolled; function change(next: string) { if (valueProp === undefined) setUncontrolled(next); onValueChange?.(next); } function clear() { change(""); if (valueProp === undefined && inputRef.current) inputRef.current.value = ""; onClear?.(); inputRef.current?.focus(); } return ( { inputRef.current = node; if (typeof ref === "function") ref(node); else if (ref) ref.current = node; }} type="search" value={value} disabled={disabled} placeholder={placeholder ?? text.search?.placeholder ?? "ค้นหา…"} aria-label={ariaLabelProp ?? label ?? text.search?.label ?? "ค้นหา"} autoComplete="off" spellCheck={false} onChange={(e) => change(e.target.value)} onKeyDown={(e) => { onKeyDown?.(e); if (clearOnEscape && e.key === "Escape" && value && !e.defaultPrevented) { // ล้างคำค้นอย่างเดียว — ไม่ปิด dialog/sheet ที่ครอบอยู่ (กด Esc อีกครั้งเมื่อช่องว่างจึงปิด) e.preventDefault(); e.stopPropagation(); clear(); } }} className={cn( // ซ่อนปุ่ม X ของ type=search ใน WebKit — เรามีปุ่มล้างของเราเอง "[&::-webkit-search-cancel-button]:appearance-none", size === "xs" && "text-xs", className, )} /> {value && !disabled && ( )} ); }