ข้ามไปยังเนื้อหา

รายการตัวเลือก

Select

A dropdown list on the ARIA listbox pattern. Arrows move, Enter or Space chooses, Esc closes, Home and End jump, and typing jumps to the first option that starts with what was typed. With `multiple`, choices show as chips and the list stays open while you pick. The list opens upward when there is no room below.

ใช้เมื่อมีตัวเลือกไม่เกินประมาณ 15 รายการ ถ้ามากกว่านั้นหรือต้องค้นหา ใช้ Combobox ส่งตัวเลือกเป็นอาร์เรย์ ส่ง label hint และ error เป็นข้อความ และเก็บค่าที่เลือกไว้ใน state ของหน้าจอ

import { Select } from "@bsru/ui";

ตัวอย่าง

เลือกรายการเดียว แบ่งกลุ่ม

ตัวเลือกที่มี group เดียวกันติดกันจะอยู่ใต้หัวข้อเดียว note แสดงเป็นบรรทัดที่สอง value เป็น null เมื่อยังไม่เลือก

รหัสงบประมาณ

แสดงงบคงเหลือใต้แต่ละรหัส

const [value, setValue] = useState<string | null>(null);

<Select
  label="รหัสงบประมาณ"
  hint="แสดงงบคงเหลือใต้แต่ละรหัส"
  placeholder="เลือกรหัสงบประมาณ"
  options={[
    {
      value: "a",
      label: "BG-2570-ก-001",
      note: "เงินรายได้ คงเหลือ 350,000.00",
      group: "หน่วยงาน ก",
    },
    {
      value: "b",
      label: "BG-2570-ก-002",
      note: "เงินงบประมาณแผ่นดิน คงเหลือ 1,200,000.00",
      group: "หน่วยงาน ก",
    },
    {
      value: "c",
      label: "BG-2570-ข-001",
      note: "เงินรายได้ คงเหลือ 480,000.00",
      group: "หน่วยงาน ข",
    },
    {
      value: "d",
      label: "BG-2570-ข-002",
      note: "เงินกองทุน คงเหลือ 95,000.00",
      group: "หน่วยงาน ข",
    },
  ]}
  value={value}
  onChange={setValue}
/>

เลือกได้หลายรายการ

ส่ง multiple แล้ว value เป็นอาร์เรย์ รายการที่เลือกแสดงเป็นชิป

สถานะ

const [value, setValue] = useState<string[]>(["pending"]);

<Select
  multiple
  label="สถานะ"
  placeholder="ทุกสถานะ"
  options={[
    { value: "draft", label: "ร่าง" },
    { value: "pending", label: "รออนุมัติ" },
    { value: "approved", label: "อนุมัติแล้ว" },
    { value: "rejected", label: "ไม่อนุมัติ" },
  ]}
  value={value}
  onChange={setValue}
/>

ตัวเลือกมีไอคอน

ช่องทางแจ้งเตือน

const [value, setValue] = useState<string | null>("mail");

<Select
  label="ช่องทางแจ้งเตือน"
  placeholder="เลือกช่องทาง"
  options={[
    { value: "mail", label: "อีเมล", icon: Mail },
    { value: "sms", label: "ข้อความ SMS", icon: Smartphone },
    { value: "push", label: "แจ้งเตือนในแอป", icon: Bell },
  ]}
  value={value}
  onChange={setValue}
/>

ช่องบังคับที่ยังไม่ได้เลือก

required ใส่ดาวสีแดงที่ป้าย error แสดงหลังกดบันทึกโดยยังไม่ได้เลือก ตัวอย่างนี้ล้างข้อความเมื่อเลือกแล้ว

รหัสงบประมาณ

เลือกรหัสงบประมาณ

const [value, setValue] = useState<string | null>(null);

<Select
  label="รหัสงบประมาณ"
  required
  placeholder="เลือกรหัสงบประมาณ"
  options={[
    {
      value: "a",
      label: "BG-2570-ก-001",
      note: "เงินรายได้ คงเหลือ 350,000.00",
      group: "หน่วยงาน ก",
    },
    {
      value: "b",
      label: "BG-2570-ก-002",
      note: "เงินงบประมาณแผ่นดิน คงเหลือ 1,200,000.00",
      group: "หน่วยงาน ก",
    },
    {
      value: "c",
      label: "BG-2570-ข-001",
      note: "เงินรายได้ คงเหลือ 480,000.00",
      group: "หน่วยงาน ข",
    },
    {
      value: "d",
      label: "BG-2570-ข-002",
      note: "เงินกองทุน คงเหลือ 95,000.00",
      group: "หน่วยงาน ข",
    },
  ]}
  value={value}
  onChange={setValue}
  error={value === null ? "เลือกรหัสงบประมาณ" : undefined}
/>

props ทั้งหมด

ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้

Select props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
options* จำเป็นreadonly SelectOption<V>[]——
placeholder* จำเป็นstring—Shown when nothing is chosen.
labelledBystring—The id of a visible label the screen wrote. Use label instead, unless the label is elsewhere.
classNamestring——
labelstring—The visible label, already in the screen language. Every field needs one, unless the screen labels it another way.
hintstring—Help under the field, such as the format or a limit.
errorstring—What's wrong and how to fix it, shown under the field. It marks the field invalid.
requiredboolean—Marks the label with a red star, and the control as required.

เมื่อ multiple: false

Select props with multiple: false
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
multiplefalse——
value* จำเป็นV | null——
onChange* จำเป็น(next: V) => void——

เมื่อ multiple: true

Select props with multiple: true
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
multiple* จำเป็นtrue——
value* จำเป็นreadonly V[]——
onChange* จำเป็น(next: V[]) => void——

ชนิดข้อมูล

SelectOption

SelectOption fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
value* จำเป็นV——
label* จำเป็นstring—The text shown, already in the screen language. Typing jumps to an option by the start of this text.
notestring—A second line, such as the balance left on a budget code.
groupstring—Options with the same group in a row sit under one heading.
iconLucideIcon——

ข้อควรปฏิบัติ

  • ส่ง label เสมอ ป้ายเป็นชื่อของปุ่มเลือกให้เอง ใช้ labelledBy เฉพาะเมื่อป้ายอยู่ที่อื่นบนหน้าจอ
  • ส่ง label ของตัวเลือกในภาษาของหน้าจออยู่แล้ว
  • แป้นพิมพ์: ลูกศรเลื่อน Enter หรือ Space เลือก Esc ปิด Home และ End ไปต้นและท้าย พิมพ์เพื่อกระโดดไปยังตัวเลือก

ซอร์สโค้ด: ui/src/select.tsx