SegmentedButton分段按鈕
分段按鈕元件封裝了一組按鈕,用於提供選項、切換視圖或對元素進行排序等。
使用方法
按需引入元件:
import 'mdui/components/segmented-button-group.js';
import 'mdui/components/segmented-button.js';
按需引入元件的 TypeScript 型別:
import type { SegmentedButtonGroup } from 'mdui/components/segmented-button-group.js';
import type { SegmentedButton } from 'mdui/components/segmented-button.js';
使用範例:
<mdui-segmented-button-group>
<mdui-segmented-button>Day</mdui-segmented-button>
<mdui-segmented-button>Week</mdui-segmented-button>
<mdui-segmented-button>Month</mdui-segmented-button>
</mdui-segmented-button-group>單選模式
在 <mdui-segmented-button-group> 元素上指定 selects 屬性為 single,即可做成單選模式。此時 <mdui-segmented-button-group> 的 value 屬性值即為目前選取的 <mdui-segmented-button> 的 value 屬性的值。
多選模式
在 <mdui-segmented-button-group> 元素上指定 selects 屬性為 multiple,即可做成多選模式。此時 <mdui-segmented-button-group> 的 value 屬性值為目前選取的 <mdui-segmented-button> 的 value 屬性的值所組成的陣列。
注意:在多選模式下,<mdui-segmented-button-group> 的 value 屬性值為陣列,只能透過 JavaScript 屬性來讀取和設定該值。
圖示
在 <mdui-segmented-button> 元素上,透過設定 icon 和 end-icon 屬性,可以在按鈕的左側和右側加入 Material Icons 圖示。另外,也可以透過 icon 和 end-icon slot 在按鈕的左側和右側加入元素。
在 <mdui-segmented-button> 元素上,透過加上 selected-icon 屬性,可以設定選取狀態的 Material Icons 圖示。也可以透過 selected-icon slot 進行設定。
連結
在 <mdui-segmented-button> 元素上,透過設定 href 屬性,即可將按鈕轉換為連結。此時,還可使用下列與連結相關的屬性:download、target、rel。
停用及載入中狀態
在 <mdui-segmented-button-group> 元素上,透過加上 disabled 屬性,即可停用整個分段按鈕組。
在 <mdui-segmented-button> 元素上,透過加上 disabled 屬性,即可停用特定按鈕;透過加上 loading 屬性,則可為特定按鈕加入載入中狀態。
mdui-segmented-button-group API
屬性
| HTML 屬性 | JavaScript 屬性 | Reflect | 型別 | 預設值 |
|---|---|---|---|---|
full-width | fullWidth | boolean | false | |
是否填滿父元素寬度 | ||||
selects | selects | 'single' | 'multiple' | - | |
分段按鈕的可選取狀態,預設為不可選取。可選值包括:
| ||||
disabled | disabled | boolean | false | |
是否為停用狀態 | ||||
required | required | boolean | false | |
送出表單時,是否必須選取 | ||||
form | form | string | - | |
關聯的 若未指定此屬性,則該元素必須是 | ||||
name | name | string | '' | |
送出表單時的名稱,將與表單資料一起送出 | ||||
value | value | string | string[] | '' | |
目前選取的 Note:該屬性的 HTML 屬性始終為字串,且僅在 | ||||
defaultValue | string | string[] | '' | ||
預設選取的值。在重設表單時,將重設為該預設值。該屬性只能透過 JavaScript 屬性設定 | ||||
validity | ValidityState | - | ||
表單驗證狀態物件,具體參見 | ||||
validationMessage | string | - | ||
如果表單驗證未通過,此屬性將包含提示資訊。如果驗證通過,此屬性將為空字串 | ||||
方法
| 名稱 | 參數 | 回傳值 |
|---|---|---|
checkValidity | boolean | |
檢查表單欄位是否通過驗證。如果未通過,回傳 | ||
reportValidity | boolean | |
檢查表單欄位是否通過驗證。如果未通過,回傳 如果驗證未通過,還會在元件上顯示驗證失敗的提示。 | ||
setCustomValidity |
| void |
設定自訂的錯誤提示文字。只要這個文字不為空,就表示欄位未通過驗證 | ||
CSS 自訂屬性
| 名稱 |
|---|
--shape-corner |
元件的圓角大小。可以指定一個具體的像素值;但更推薦引用設計令牌 |
mdui-segmented-button API
屬性
| HTML 屬性 | JavaScript 屬性 | Reflect | 型別 | 預設值 |
|---|---|---|---|---|
icon | icon | string | - | |
左側的 Material Icons 圖示名。也可以透過 | ||||
end-icon | endIcon | string | - | |
右側的 Material Icons 圖示名。也可以透過 | ||||
selected-icon | selectedIcon | string | - | |
選取狀態的 Material Icons 圖示名。也可以透過 | ||||
href | href | string | - | |
連結的目標網址。 若設定此屬性,元件內部會渲染為 | ||||
download | download | string | - | |
下載目標。 Note:僅在設定了 | ||||
target | target | '_blank' | '_parent' | '_self' | '_top' | - | |
連結的開啟方式。可選值包括:
Note:僅在設定了 | ||||
rel | rel | 'alternate' | 'author' | 'bookmark' | 'external' | 'help' | 'license' | 'me' | 'next' | 'nofollow' | 'noreferrer' | 'opener' | 'prev' | 'search' | 'tag' | - | |
目前文件與被連結文件之間的關係。可選值包括:
Note:僅在指定了 | ||||
autofocus | autofocus | boolean | false | |
是否在頁面載入完成後自動取得焦點 | ||||
tabindex | tabIndex | number | - | |
元素在使用 Tab 鍵切換焦點時的順序 | ||||
disabled | disabled | boolean | false | |
是否停用 | ||||
loading | loading | boolean | false | |
是否處於載入中狀態 | ||||
name | name | string | '' | |
按鈕名稱,會與表單資料一起送出。 Note:僅在未設定 | ||||
value | value | string | '' | |
按鈕初始值,會與表單資料一起送出。 Note:僅在未設定 | ||||
type | type | 'submit' | 'reset' | 'button' | 'button' | |
按鈕的類型。預設類型為
Note:僅在未指定 | ||||
form | form | string | - | |
關聯的 若未指定此屬性,則該元素必須是 Note:僅在未指定 | ||||
formaction | formAction | string | - | |
指定送出表單的 URL。 若指定了此屬性,將覆蓋 Note:僅在未指定 | ||||
formenctype | formEnctype | 'application/x-www-form-urlencoded' | 'multipart/form-data' | 'text/plain' | - | |
指定送出表單到伺服器的內容類型。可選值包括:
若指定了此屬性,將覆蓋 Note:僅在未指定 | ||||
formmethod | formMethod | 'post' | 'get' | - | |
指定送出表單時使用的 HTTP 方法。可選值包括:
若設定了此屬性,將覆蓋 Note:僅在未設定 | ||||
formnovalidate | formNoValidate | boolean | false | |
若設定了此屬性,表單送出時將不執行表單驗證。 若設定了此屬性,將覆蓋 Note:僅在未設定 | ||||
formtarget | formTarget | '_self' | '_blank' | '_parent' | '_top' | - | |
送出表單後接收到的回應應顯示在何處。可選值包括:
若設定了此屬性,將覆蓋 Note:僅在未設定 | ||||
validity | ValidityState | - | ||
表單驗證狀態物件,具體參見 | ||||
validationMessage | string | - | ||
如果表單驗證未通過,此屬性將包含提示資訊。如果驗證通過,此屬性將為空字串 | ||||
方法
| 名稱 | 參數 | 回傳值 |
|---|---|---|
click | void | |
模擬滑鼠點擊元素 | ||
focus |
| void |
將焦點設定到目前元素。 可以傳入一個物件作為引數,該物件的屬性包括:
| ||
blur | void | |
移除目前元素的焦點 | ||
checkValidity | boolean | |
檢查表單欄位是否通過驗證。如果未通過,回傳 | ||
reportValidity | boolean | |
檢查表單欄位是否通過驗證。如果未通過,回傳 如果驗證未通過,還會在元件上顯示驗證失敗的提示。 | ||
setCustomValidity |
| void |
設定自訂的錯誤提示文字。只要這個文字不為空,就表示欄位未通過驗證 | ||
Slots
| 名稱 |
|---|
| 預設 |
分段按鈕項目的文字內容 |
icon |
分段按鈕項目的左側圖示 |
selected-icon |
選取狀態的左側圖示 |
end-icon |
分段按鈕項目的右側圖示 |