TextField文字欄位
文字欄位元件讓使用者能在頁面中輸入文字,通常用於表單和對話框。
使用方法
按需引入元件:
import 'mdui/components/text-field.js';
按需引入元件的 TypeScript 型別:
import type { TextField } from 'mdui/components/text-field.js';
使用範例:
<mdui-text-field label="Text Field"></mdui-text-field>輔助文字
透過 label 屬性設定文字欄位上方的標籤文字。
透過 placeholder 屬性設定無值時的佔位文字。
透過 helper 屬性設定文字欄位底部的輔助文字。你也可以使用 helper slot 來設定輔助文字。加上 helper-on-focus 屬性則僅在輸入框聚焦時顯示輔助文字。
可清空
加上 clearable 屬性後,當文字欄位有值時,會在右側加上清空按鈕。
文字靠右對齊
加上 end-aligned 屬性可以使文字靠右對齊。
前後文字及圖示
透過設定 icon 和 end-icon 屬性,即可在文字欄位的左側和右側加入 Material Icons 圖示。也可以透過 icon 和 end-icon slot 在文字欄位的左側和右側加入元素。
透過設定 prefix 和 suffix 屬性,可以在文字欄位的左側和右側加入文字。也可以透過 prefix 和 suffix slot 在文字欄位的左側和右側加入文字元素。這些文字只有在文字欄位聚焦或有值時才會顯示。
唯讀模式
加上 readonly 屬性,可以將文字欄位設定為唯讀模式。
停用狀態
加上 disabled 屬性即可停用文字欄位。
多行文字欄位
透過 rows 屬性,可以設定多行文字欄位的行數。
也可以加上 autosize 屬性,使文字欄位能根據輸入內容的長度自動調整高度。透過 min-rows 和 max-rows 屬性,可以指定自動調整高度時的最小行數和最大行數。
字數統計
當透過 maxlength 屬性設定了最大字數時,可以加上 counter 屬性在文字欄位下方顯示字數統計。
密碼欄位
當 type="password" 時,加上 toggle-password 屬性可以在文字欄位右側加入切換密碼可見性的按鈕。
API
屬性
| HTML 屬性 | JavaScript 屬性 | Reflect | 型別 | 預設值 |
|---|---|---|---|---|
variant | variant | 'filled' | 'outlined' | 'filled' | |
文字欄位的形狀。預設為
| ||||
type | type | 'text' | 'number' | 'password' | 'url' | 'email' | 'search' | 'tel' | 'hidden' | 'date' | 'datetime-local' | 'month' | 'time' | 'week' | 'text' | |
文字欄位輸入類型。預設為
| ||||
name | name | string | '' | |
文字欄位名稱,將與表單資料一起送出 | ||||
value | value | string | '' | |
文字欄位的值,將與表單資料一起送出 | ||||
defaultValue | string | '' | ||
預設值。在重設表單時,將重設為該預設值。該屬性只能透過 JavaScript 屬性設定 | ||||
label | label | string | - | |
標籤文字 | ||||
placeholder | placeholder | string | - | |
佔位符文字 | ||||
helper | helper | string | - | |
文字欄位底部的說明文字。也可以透過 | ||||
helper-on-focus | helperOnFocus | boolean | false | |
是否僅在取得焦點時,顯示底部的說明文字 | ||||
clearable | clearable | boolean | false | |
是否可清空文字欄位內容 | ||||
clear-icon | clearIcon | string | - | |
可清空文字欄位時,顯示在文字欄位右側的清空按鈕的 Material Icons 圖示名。也可以透過 | ||||
end-aligned | endAligned | boolean | false | |
是否將文字右對齊 | ||||
prefix | prefix | string | - | |
文字欄位的前綴文字。只在文字欄位聚焦或有值時顯示。也可以透過 | ||||
suffix | suffix | string | - | |
文字欄位的後綴文字。只在文字欄位聚焦或有值時顯示。也可以透過 | ||||
icon | icon | string | - | |
文字欄位的前綴圖示的 Material Icons 圖示名。也可以透過 | ||||
end-icon | endIcon | string | - | |
文字欄位的後綴圖示的 Material Icons 圖示名。也可以透過 | ||||
error-icon | errorIcon | string | - | |
表單欄位驗證失敗時,顯示在文字欄位右側的 Material Icons 圖示名。也可以透過 | ||||
form | form | string | - | |
關聯的 若未指定此屬性,則該元素必須是 | ||||
readonly | readonly | boolean | false | |
是否為唯讀模式 | ||||
disabled | disabled | boolean | false | |
是否停用輸入框 | ||||
required | required | boolean | false | |
送出表單時,是否必須填寫該欄位 | ||||
rows | rows | number | - | |
文字欄位的固定顯示行數 | ||||
autosize | autosize | boolean | false | |
是否根據輸入內容自動調整文字欄位高度 | ||||
min-rows | minRows | number | - | |
| ||||
max-rows | maxRows | number | - | |
| ||||
minlength | minlength | number | - | |
允許輸入的最小字元數 | ||||
maxlength | maxlength | number | - | |
允許輸入的最大字元數 | ||||
counter | counter | boolean | false | |
是否顯示字數統計,只在 | ||||
min | min | number | - | |
當 | ||||
max | max | number | - | |
當 | ||||
step | step | number | - | |
| ||||
pattern | pattern | string | - | |
用於表單驗證的正規表示式 | ||||
toggle-password | togglePassword | boolean | false | |
| ||||
show-password-icon | showPasswordIcon | string | - | |
密碼切換按鈕的 Material Icons 圖示,當密碼為明文時顯示。也可以透過 | ||||
hide-password-icon | hidePasswordIcon | string | - | |
密碼切換按鈕的 Material Icons 圖示,當密碼為密文時顯示。也可以透過 | ||||
autocapitalize | autocapitalize | 'none' | 'sentences' | 'words' | 'characters' | - | |
iOS 的非標準屬性,用於控制文字首字母是否自動大寫。在 iOS5 及以後的版本上有效。可選值包括:
| ||||
autocorrect | autocorrect | string | - | |
| ||||
autocomplete | autocomplete | string | - | |
| ||||
enterkeyhint | enterkeyhint | 'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send' | - | |
| ||||
spellcheck | spellcheck | boolean | false | |
是否啟用拼字檢查 | ||||
inputmode | inputmode | 'none' | 'text' | 'decimal' | 'numeric' | 'tel' | 'search' | 'email' | 'url' | - | |
| ||||
validity | ValidityState | - | ||
表單驗證狀態物件,具體參見 | ||||
validationMessage | string | - | ||
如果表單驗證未通過,此屬性將包含提示資訊。如果驗證通過,此屬性將為空字串 | ||||
valueAsNumber | number | - | ||
取得目前值,並轉換為 | ||||
autofocus | autofocus | boolean | false | |
是否在頁面載入完成後自動取得焦點 | ||||
tabindex | tabIndex | number | - | |
元素在使用 Tab 鍵切換焦點時的順序 | ||||
方法
| 名稱 | 參數 | 回傳值 |
|---|---|---|
select | void | |
選取文字欄位中的文字 | ||
setSelectionRange |
| void |
選取文字欄位中特定範圍的內容 | ||
setRangeText |
| void |
將文字欄位中特定範圍的文字替換為新的文字 | ||
checkValidity | boolean | |
檢查表單欄位是否通過驗證。如果未通過,回傳 | ||
reportValidity | boolean | |
檢查表單欄位是否通過驗證。如果未通過,回傳 如果驗證未通過,還會在元件上顯示驗證失敗的提示。 | ||
setCustomValidity |
| void |
設定自訂的錯誤提示文字。只要這個文字不為空,就表示欄位未通過驗證 | ||
click | void | |
模擬滑鼠點擊元素 | ||
focus |
| void |
將焦點設定到目前元素。 可以傳入一個物件作為引數,該物件的屬性包括:
| ||
blur | void | |
移除目前元素的焦點 | ||
Slots
| 名稱 |
|---|
icon |
左側圖示 |
end-icon |
右側圖示 |
error-icon |
驗證失敗狀態的右側圖示 |
prefix |
左側文字 |
suffix |
右側文字 |
clear-button |
清空按鈕 |
clear-icon |
清空按鈕中的圖示 |
toggle-password-button |
密碼顯示狀態切換按鈕 |
show-password-icon |
顯示密碼狀態下,密碼顯示狀態切換按鈕中的圖示 |
hide-password-icon |
隱藏密碼狀態下,密碼顯示狀態切換按鈕中的圖示 |
helper |
底部的說明文字 |
CSS Parts
| 名稱 |
|---|
container |
文字欄位容器 |
icon |
左側圖示 |
end-icon |
右側圖示 |
error-icon |
驗證失敗狀態的右側圖示 |
prefix |
左側文字 |
suffix |
右側文字 |
label |
上方的標籤文字 |
input |
內部的 |
clear-button |
清空按鈕 |
clear-icon |
清空按鈕中的圖示 |
toggle-password-button |
密碼顯示狀態切換按鈕 |
show-password-icon |
顯示密碼狀態下,密碼顯示狀態切換按鈕中的圖示 |
hide-password-icon |
隱藏密碼狀態下,密碼顯示狀態切換按鈕中的圖示 |
supporting |
底部輔助資訊容器,包括 helper、error、counter |
helper |
底部的說明文字 |
error |
底部的錯誤描述文字 |
counter |
底部右側的字數統計 |