TextFieldAPI
React TextField 元件的 API 參考文件。瞭解此匯出模組的 props、CSS 和其他 API。
示範
匯入
import TextField from '@mui/material/TextField';
// or
import { TextField } from '@mui/material';
閱讀關於最小化套件大小的指南,以瞭解差異。
TextField
是最常見情況 (80%) 的便利包裝函式。它不可能滿足所有人的需求,否則 API 將會失控增長。
進階設定
重要的是要瞭解文字欄位是以下元件之上的簡單抽象概念
如果您希望變更套用至 input
元素的 props,您可以按照以下方式操作
const inputProps = {
step: 300,
};
return <TextField id="time" type="time" inputProps={inputProps} />;
對於進階情況,請點擊上方「編輯此頁面」按鈕查看 TextField 的原始碼。請考慮以下任一方式
- 使用大寫 props 將值直接傳遞至元件
- 直接使用示範中顯示的底層元件
FormControl 元件的 Props 也可使用。
名稱 | 類型 | 預設 | 描述 |
---|---|---|---|
autoComplete | string | - | 此 prop 可協助使用者更快填寫表單,尤其是在行動裝置上。名稱可能會造成混淆,因為它更像是自動填寫。您可以依照規格瞭解更多資訊。 |
autoFocus | bool | false | 如果為 |
classes | object | - | 覆寫或擴展套用至元件的樣式。 請參閱下方的CSS 類別 API 以取得更多詳細資訊。 |
color | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning' | string | 'primary' | 元件的顏色。它同時支援預設和自訂主題顏色,可以按照調色盤自訂指南中所示的方式新增。 |
defaultValue | any | - | 預設值。在元件不受控制時使用。 |
disabled | bool | false | 如果為 |
error | bool | false | 如果為 |
FormHelperTextProps | object | - | 套用至 |
fullWidth | bool | false | 如果為 |
helperText | node | - | 輔助文字內容。 |
id | string | - |
|
InputLabelProps | object | - | 套用至 |
inputProps | object | - | 套用至 |
InputProps | object | - | 套用至 Input 元素的 Props。它將是 |
inputRef | ref | - | 將 ref 傳遞至 |
label | node | - | 標籤內容。 |
margin | 'dense' | 'none' | 'normal' | 'none' | 如果為 |
maxRows | number | string | - | 當 multiline 選項設定為 true 時,要顯示的最大列數。 |
minRows | number | string | - | 當 multiline 選項設定為 true 時,要顯示的最小列數。 |
multiline | bool | false | 如果為 |
name | string | - |
|
onChange | func | - | 值變更時觸發的回呼。 簽名: function(event: object) => void
|
placeholder | string | - | 使用者輸入值之前, |
required | bool | false | 如果為 |
rows | number | string | - | 當 multiline 選項設定為 true 時,要顯示的列數。 |
select | bool | false | 轉譯 |
SelectProps | object | - | 套用至 |
size | 'medium' | 'small' | string | 'medium' | 元件的大小。 |
slotProps | { formHelperText?: func | object, htmlInput?: func | object, input?: func | object, inputLabel?: func | object, select?: func | object } | {} | 用於每個插槽內部的 props。 |
slots | { formHelperText?: elementType, htmlInput?: elementType, input?: elementType, inputLabel?: elementType, root?: elementType, select?: elementType } | {} | 用於每個插槽內部的元件。 |
sx | Array<func | object | bool> | func | object | - | 系統 prop,允許定義系統覆寫以及其他 CSS 樣式。 請參閱 `sx` 頁面 以取得更多詳細資訊。 |
type | string | - |
|
value | any | - |
|
variant | 'filled' | 'outlined' | 'standard' | 'outlined' | 要使用的變體。 |
繼承
雖然上方未明確記錄,但 FormControl 元件的 props 也可在 TextField 中使用。您可以利用這一點定位巢狀元件。
主題預設 props
您可以使用 MuiTextField
透過主題變更此元件的預設 props。
插槽名稱 | 類別名稱 | 預設元件 | 描述 |
---|---|---|---|
root | .MuiTextField-root | FormControl | 轉譯根元件的元件。 |
input | OutlinedInput | 轉譯輸入的元件。 | |
inputLabel | InputLabel | 轉譯輸入標籤的元件。 | |
htmlInput | 'input' | html 輸入元素。 | |
formHelperText | FormHelperText | 轉譯輔助文字的元件。 | |
select | Select | 轉譯選取器的元件。 |
原始碼
如果您在此頁面中找不到資訊,請考慮查看元件的實作以取得更多詳細資訊。