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

ช่องรหัสผ่าน

PasswordInput

A password field with a leading icon and a button that shows or hides what was typed.

ใช้กับรหัสผ่านทุกช่อง ช่องมีไอคอนนำหน้าและปุ่มแสดงหรือซ่อนสิ่งที่พิมพ์ ส่ง label hint และ error เป็นข้อความ สำหรับรหัสยืนยันครั้งเดียวที่ส่งทาง SMS หรืออีเมล ใช้ OtpInput

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

ตัวอย่าง

เข้าสู่ระบบ

label แสดงป้ายและผูกกับช่องให้ ส่ง autoComplete เพื่อให้ตัวจัดการรหัสผ่านเติมให้ได้ ช่องเก็บค่าเอง อ่านจากฟอร์มตอนส่งด้วย name

<PasswordInput
  label="รหัสผ่าน"
  name="password"
  autoComplete="current-password"
/>

เข้าสู่ระบบไม่สำเร็จ

error แสดงใต้ช่องและทำให้ช่องเป็นสีผิดพลาด บอกว่าผิดอะไรและทำอะไรต่อ

อีเมลหรือรหัสผ่านไม่ถูกต้อง ลองอีกครั้ง หรือกดลืมรหัสผ่าน

<PasswordInput
  label="รหัสผ่าน"
  name="password"
  autoComplete="current-password"
  error="อีเมลหรือรหัสผ่านไม่ถูกต้อง ลองอีกครั้ง หรือกดลืมรหัสผ่าน"
/>

ตั้งรหัสผ่านใหม่

เมื่อหน้าจอต้องตรวจขณะพิมพ์ ให้เก็บค่าด้วย value และ onValueChange icon เปลี่ยนไอคอนนำหน้า บอกเงื่อนไขของรหัสผ่านใน hint ก่อนผู้ใช้พิมพ์

อย่างน้อย 12 ตัวอักษร

const [password, setPassword] = useState("");

<PasswordInput
  label="รหัสผ่านใหม่"
  hint="อย่างน้อย 12 ตัวอักษร"
  required
  icon={KeyRound}
  autoComplete="new-password"
  value={password}
  onValueChange={setPassword}
  error={password.length > 0 && password.length < 12 ? "ยังสั้นเกินไป ใช้อย่างน้อย 12 ตัวอักษร" : undefined}
/>

props ทั้งหมด

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

PasswordInput 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.
iconLucideIconLockThe leading icon.
valuestring—The password, when the screen keeps it.
defaultValuestring""The starting value. Leave it empty on real screens.
onValueChange(next: string) => void—Hears the password on each change.

รับ attribute ของ element HTML ต้นแบบได้ทั้งหมดด้วย เช่น id, name, required และ disabled

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

  • อย่าส่ง defaultValue บนหน้าจอจริง และห้ามใส่รหัสผ่านจริงในตัวอย่าง
  • required ใส่ดาวสีแดงที่ป้าย และทำให้ช่องเป็นช่องบังคับ
  • ปุ่มแสดงรหัสผ่านใช้ aria-pressed และชื่อปุ่มเปลี่ยนตามภาษาของหน้าจอ

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