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

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

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. With layout cards, the options show as a grid of cards, and all four arrows move through it.

ใช้เมื่อมีตัวเลือกไม่เกินประมาณ 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}
/>

ตัวเลือกเป็นการ์ดมีรูป

layout cards แสดงตัวเลือกเป็นการ์ดเรียงเป็นตาราง มีรูปจาก image อยู่ด้านบน menuWidth lg ให้รายการกว้าง 42rem แม้ช่องจะแคบ และไม่กว้างเกินจอ ถ้าด้านขวาไม่พอ รายการจะชิดขอบขวาของช่องแทน ลูกศรทั้งสี่ทิศเลื่อนไปมาในตาราง

รูปแบบการจัดห้อง

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

<Select
  label="รูปแบบการจัดห้อง"
  placeholder="เลือกรูปแบบ"
  options={[
    {
      value: "boardroom",
      label: "ประชุมโต๊ะยาว",
      note: "14 ที่นั่ง",
      image: "/examples/rooms/boardroom.svg",
    },
    {
      value: "classroom",
      label: "ห้องเรียน",
      note: "24 ที่นั่ง",
      image: "/examples/rooms/classroom.svg",
    },
    {
      value: "theatre",
      label: "โรงละคร",
      note: "60 ที่นั่ง",
      image: "/examples/rooms/theatre.svg",
    },
    {
      value: "ushape",
      label: "ตัวยู",
      note: "15 ที่นั่ง",
      image: "/examples/rooms/ushape.svg",
    },
    {
      value: "banquet",
      label: "โต๊ะจัดเลี้ยง",
      note: "24 ที่นั่ง",
      image: "/examples/rooms/banquet.svg",
    },
    {
      value: "workshop",
      label: "กลุ่มปฏิบัติการ",
      note: "18 ที่นั่ง",
      image: "/examples/rooms/workshop.svg",
    },
  ]}
  layout="cards"
  menuWidth="lg"
  value={value}
  onChange={setValue}
/>

รายการมีรูปเล็ก

ในรายการแบบแถว image แสดงเป็นรูปสี่เหลี่ยมเล็กหน้าข้อความ แทนไอคอน menuWidth sm ให้รายการกว้าง 20rem

รูปแบบการจัดห้อง

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

<Select
  label="รูปแบบการจัดห้อง"
  placeholder="เลือกรูปแบบ"
  options={[
    {
      value: "boardroom",
      label: "ประชุมโต๊ะยาว",
      note: "14 ที่นั่ง",
      image: "/examples/rooms/boardroom.svg",
    },
    {
      value: "classroom",
      label: "ห้องเรียน",
      note: "24 ที่นั่ง",
      image: "/examples/rooms/classroom.svg",
    },
    {
      value: "theatre",
      label: "โรงละคร",
      note: "60 ที่นั่ง",
      image: "/examples/rooms/theatre.svg",
    },
    {
      value: "ushape",
      label: "ตัวยู",
      note: "15 ที่นั่ง",
      image: "/examples/rooms/ushape.svg",
    },
    {
      value: "banquet",
      label: "โต๊ะจัดเลี้ยง",
      note: "24 ที่นั่ง",
      image: "/examples/rooms/banquet.svg",
    },
    {
      value: "workshop",
      label: "กลุ่มปฏิบัติการ",
      note: "18 ที่นั่ง",
      image: "/examples/rooms/workshop.svg",
    },
  ]}
  menuWidth="sm"
  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——
layoutOptionLayout
ค่าที่ใช้ได้:"list""cards"
—How the options show: rows, or cards in a grid with each option's image on top. Cards suit a menuWidth wider than the field.
menuWidthMenuWidth
ค่าที่ใช้ได้:"trigger""sm""md""lg""xl"
—How wide the list is. trigger matches the field. The others may be wider than the field, but never wider than the screen.
hideLabelboolean—Keeps the label for screen readers only, with the placeholder naming the field on screen, such as a filter in a DataTable's filter bar. Pass the label as usual.
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——
imagestring—A picture's address, such as a product photo or a room plan. It shows in place of the icon: a small square in a list, and across the top of a card. The label names the option, so the picture needs no text of its own.

OptionLayout

How a dropdown shows its options: rows, or cards in a grid, each with its picture on top.

type OptionLayout = "list" | "cards";

MenuWidth

How wide a dropdown's list is. trigger matches the field. The others are fixed widths that may be wider than the field: sm 20rem, md 28rem, lg 42rem and xl 56rem, never wider than the screen. A list wider than the room to the right of the field lines up with the field's right edge instead.

type MenuWidth = "trigger" | "sm" | "md" | "lg" | "xl";

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

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

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