跳到主要內容跳到主要內容

SelectAPI

React Select 元件的 API 參考文件。了解此匯出模組的 props、CSS 和其他 API。

範例

Import

import Select from '@mui/joy/Select';
// or
import { Select } from '@mui/joy';

若要了解差異,請閱讀此關於最小化 bundle size 的指南

Props

名稱類型預設值描述
actionfunc
| { current?: { focusVisible: func } }
-

用於命令式動作的 ref。目前僅支援 focusVisible() 動作。

autoFocusboolfalse

如果 true,則在首次掛載期間會 focus select 元素

color'danger'
| 'neutral'
| 'primary'
| 'success'
| 'warning'
| string
'neutral'

元件的顏色。它支援適用於此元件的 theme colors。

若要了解如何新增自己的顏色,請查看Themed components—Extend colors

componentelementType-

用於 root node 的元件。可以是 HTML 元素的字串或元件。

defaultListboxOpenboolfalse

如果 true,select 將會初始開啟。

defaultValueany-

預設選取值。當元件不受控制時使用。

disabledboolfalse

如果 true,則停用元件。

endDecoratornode-

select 的尾隨裝飾。

getSerializedValuefunc-

將目前選取的值轉換為字串的函式。用於設定與 select 關聯的隱藏輸入值,以便可以透過表單張貼選取的值。

indicatornode-

select 的指示器 (*)。 ________________ [ 值 * ] ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾

listboxIdstring-

listbox 元素的 id 屬性。也用於衍生選項的 id 屬性。

listboxOpenboolundefined

控制 select 的 listbox 的開啟狀態。

multiplebool-

如果 true,則允許選取多個值。這會影響 valuedefaultValueonChange props 的類型。

namestring-

元素的名稱。例如,伺服器使用它來識別表單提交中的欄位。

onChangefunc-

選取選項時觸發的回呼。

onClosefunc-

當 focus 離開選單且選單應關閉時觸發。

onListboxOpenChangefunc-

當元件請求開啟時觸發的回呼。在受控模式中使用(請參閱 listboxOpen)。

placeholdernode-

當沒有選取值時顯示的文字。

renderValuefunc-

自訂選取值呈現方式的函式。

requiredboolfalse

如果 true,提交表單時 Select 不得為空。

size'sm'
| 'md'
| 'lg'
| string
-

元件的大小。

若要了解如何將自訂大小新增至元件,請查看Themed components—Extend sizes

slots{ button?: elementType, endDecorator?: elementType, indicator?: elementType, listbox?: elementType, root?: elementType, startDecorator?: elementType }{}

用於每個 slot 內部的元件。

請參閱下方的Slots API以取得更多詳細資訊。

startDecoratornode-

select 的前導裝飾。

sxArray<func
| object
| bool>
| func
| object
-

允許定義系統覆寫以及其他 CSS 樣式的系統 prop。

請參閱`sx` 頁面以取得更多詳細資訊。

valueany-

選取的值。設定為 null 以取消選取所有選項。

variant'outlined'
| 'plain'
| 'soft'
| 'solid'
| string
'outlined'

要使用的global variant

若要了解如何新增自己的 variants,請查看Themed components—Extend variants

ref 會轉發到 root 元素。

Theme default props

您可以使用 JoySelect 透過 theme 變更此元件的預設 props。

Slots

若要了解如何自訂 slot,請查看Overriding component structure指南。

Slot 名稱Class nameDefault component描述
root.MuiSelect-root'div'呈現 root 的元件。
button.MuiSelect-button'button'呈現 button 的元件。
startDecorator.MuiSelect-startDecorator'span'呈現 start decorator 的元件。
endDecorator.MuiSelect-endDecorator'span'呈現 end decorator 的元件。
indicator.MuiSelect-indicator'span'呈現 indicator 的元件。
listbox.MuiSelect-listbox'ul'呈現 listbox 的元件。

CSS classes

這些 class names 對於使用 CSS 設定樣式很有用。它們會在觸發特定狀態時套用至元件的 slots。

Class nameRule name描述
.Mui-disabled如果 disabled={true},則套用至 root slot 的狀態 class。
.Mui-expanded如果 listbox 開啟,則套用至 root slot 的狀態 class。
.Mui-focusVisible如果 button 是鍵盤 focus,則套用至 SelectBase root slot 的狀態 class。
.MuiSelect-colorContextcolorContext當觸發顏色反轉時,套用至 root 元素的 Class name。
.MuiSelect-colorDangercolorDanger如果 color="danger",則套用至 root slot 的 Class name。
.MuiSelect-colorNeutralcolorNeutral如果 color="neutral",則套用至 root slot 的 Class name。
.MuiSelect-colorPrimarycolorPrimary如果 color="primary",則套用至 root slot 的 Class name。
.MuiSelect-colorSuccesscolorSuccess如果 color="success",則套用至 root slot 的 Class name。
.MuiSelect-colorWarningcolorWarning如果 color="warning",則套用至 root slot 的 Class name。
.MuiSelect-multiplemultiple如果 multiple=true,則套用至 root slot 的 Class name
.MuiSelect-popperpopper套用至 popper slot 的 Class name。
.MuiSelect-sizeLgsizeLg如果 size="lg",則套用至 root slot 的 Class name。
.MuiSelect-sizeMdsizeMd如果 size="md",則套用至 root slot 的 Class name。
.MuiSelect-sizeSmsizeSm如果 size="sm",則套用至 root slot 的 Class name。
.MuiSelect-variantOutlinedvariantOutlined如果 variant="outlined",則套用至 root slot 的 Class name。
.MuiSelect-variantPlainvariantPlain如果 variant="plain",則套用至 root slot 的 Class name。
.MuiSelect-variantSoftvariantSoft如果 variant="soft",則套用至 root slot 的 Class name。
.MuiSelect-variantSolidvariantSolid如果 variant="solid",則套用至 root slot 的 Class name。

您可以使用以下其中一個客製化選項來覆寫元件的樣式

原始碼

如果您在此頁面中找不到資訊,請考慮查看元件的實作以取得更多詳細資訊。