ช่องรหัสผ่าน
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"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "รหัสผ่าน" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| name | "password" | string | — |
| autoComplete | "current-password" | string | — |
| onValueChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: string) => void | Hears the password on each change. |
เข้าสู่ระบบไม่สำเร็จ
error แสดงใต้ช่องและทำให้ช่องเป็นสีผิดพลาด บอกว่าผิดอะไรและทำอะไรต่อ
อีเมลหรือรหัสผ่านไม่ถูกต้อง ลองอีกครั้ง หรือกดลืมรหัสผ่าน
<PasswordInput
label="รหัสผ่าน"
name="password"
autoComplete="current-password"
error="อีเมลหรือรหัสผ่านไม่ถูกต้อง ลองอีกครั้ง หรือกดลืมรหัสผ่าน"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "รหัสผ่าน" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| name | "password" | string | — |
| autoComplete | "current-password" | string | — |
| error | "อีเมลหรือรหัสผ่านไม่ถูกต้อง ลองอีกครั้ง หรือกดลืมรหัสผ่าน" | string | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
| onValueChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: string) => void | Hears the password on each change. |
ตั้งรหัสผ่านใหม่
เมื่อหน้าจอต้องตรวจขณะพิมพ์ ให้เก็บค่าด้วย 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}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "รหัสผ่านใหม่" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "อย่างน้อย 12 ตัวอักษร" | string | Help under the field, such as the format or a limit. |
| required | true | boolean | Marks the label with a red star, and the control as required. |
| icon | KeyRound | LucideIcon | The leading icon. |
| autoComplete | "new-password" | string | — |
| value | password | string | The password, when the screen keeps it. |
| onValueChange | setPassword | (next: string) => void | Hears the password on each change. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| label | string | — | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | string | — | Help under the field, such as the format or a limit. |
| error | string | — | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
| required | boolean | — | Marks the label with a red star, and the control as required. |
| icon | LucideIcon | Lock | The leading icon. |
| value | string | — | The password, when the screen keeps it. |
| defaultValue | string | "" | 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