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

ช่องป้ายกำกับ

TagInput

A text box that turns each entry into a chip. Enter adds, Backspace in an empty box removes the last one.

ใช้เมื่อผู้ใช้พิมพ์ค่าเองได้หลายค่า เช่น คำสำคัญของคำขอ แต่ละค่ากลายเป็นชิปที่ลบได้ ถ้าค่าต้องมาจากรายการที่กำหนดไว้ ใช้ Select แบบ multiple หรือ Combobox ถ้าแค่แสดงค่าที่ลบได้ โดยไม่ต้องพิมพ์ ใช้ RemovableChip

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

ตัวอย่าง

รายการคำสำคัญ

เก็บรายการไว้ใน state ของหน้าจอ onAdd ได้ข้อความที่ตัดช่องว่างแล้ว onRemove ได้ลำดับของค่าที่จะลบ

ครุภัณฑ์เร่งด่วน

กด Enter เพื่อเพิ่มแต่ละคำ

const [tags, setTags] = useState<string[]>(["ครุภัณฑ์", "เร่งด่วน"]);

<TagInput
  label="คำสำคัญ"
  hint="กด Enter เพื่อเพิ่มแต่ละคำ"
  placeholder="เพิ่มคำสำคัญ"
  tags={tags}
  onAdd={(text) => setTags([...tags, text])}
  onRemove={(index) => setTags(tags.filter((_, i) => i !== index))}
/>

ตรวจค่าก่อนเก็บ

TagInput ส่งทุกค่าที่พิมพ์มา หน้าจอเป็นผู้ตัดสินว่าจะเก็บหรือไม่ เช่น ข้ามค่าซ้ำ และส่ง error เมื่อค่าไม่ถูกต้อง ลองพิมพ์ข้อความที่ไม่มี @

staff01@example.com

พิมพ์อีเมลแล้วกด Enter

const [emails, setEmails] = useState(["staff01@example.com"]);
const [error, setError] = useState<string>();

<TagInput
  label="สำเนาถึง"
  hint="พิมพ์อีเมลแล้วกด Enter"
  tags={emails}
  error={error}
  onAdd={(text) => {
    if (!text.includes("@")) {
      setError(`${text} ไม่ใช่อีเมล ใส่ให้ครบ เช่น name@example.com`);
      return false; // The text stays in the box to fix.
    }
    setError(undefined);
    if (!emails.includes(text)) setEmails([...emails, text]);
  }}
  onRemove={(index) => setEmails(emails.filter((_, i) => i !== index))}
/>

props ทั้งหมด

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

TagInput props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
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.
idstring—The text box's id. One is made when it's left out.
tags* จำเป็นreadonly string[]—The values added so far. The screen keeps them in its state.
onAdd* จำเป็น(text: string) => boolean | void—Called with the trimmed text when the user presses Enter. The screen adds it to its list. Return false to reject it, such as an invalid email, and the text stays in the box to fix.
onRemove* จำเป็น(index: number) => void—Called with the index of the value to remove.
placeholderstring—Hint text in the empty box.
removeLabelstring—Read before each value on its remove button. Defaults to "ลบ " or "Remove ".

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

  • ส่ง label เสมอ ป้ายผูกกับช่องพิมพ์ให้เอง
  • แป้นพิมพ์: Enter เพิ่มค่า Backspace ในช่องว่างลบค่าสุดท้าย Tab ไปยังปุ่มลบของแต่ละชิป หลังกดปุ่มลบ โฟกัสกลับไปที่ช่องพิมพ์
  • ช่องพิมพ์ว่างลงหลัง Enter ทุกครั้ง ถ้าไม่เก็บค่า ให้ error บอกค่าที่พิมพ์มาด้วย ผู้ใช้จะได้รู้ว่าค่าใดผิด
  • removeLabel เปลี่ยนคำที่อ่านก่อนแต่ละค่าบนปุ่มลบ ค่าเริ่มต้นคือ ลบ หรือ Remove

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