透過於文字輸入兩側加入文字、按鈕或按鈕群組,輕鬆擴充表單控制項。
<div>
<b-input-group size="lg" prepend="$" append=".00">
<b-form-input></b-form-input>
</b-input-group>
<b-input-group class="mt-3">
<template #append>
<b-input-group-text><strong class="text-danger">!</strong></b-input-group-text>
</template>
<b-form-input></b-form-input>
</b-input-group>
<b-input-group prepend="Username" class="mt-3">
<b-form-input></b-form-input>
<b-input-group-append>
<b-button variant="outline-success">Button</b-button>
<b-button variant="info">Button</b-button>
</b-input-group-append>
</b-input-group>
</div>
使用
你可以使用屬性、命名插槽和/或子元件附加外掛程式。
使用 prepend
和 append
屬性
數值會在內部透過 <b-input-group-text>
包裝,以正確顯示。
<div>
<b-input-group prepend="$" append=".00">
<b-form-input></b-form-input>
</b-input-group>
<b-input-group prepend="0" append="100" class="mt-3">
<b-form-input type="range" min="0" max="100"></b-form-input>
</b-input-group>
</div>
使用命名插槽
如果你想更靈活地控制外掛程式,你可以改用 prepend
和 append
插槽。
插槽內容會自動透過 <b-input-group-prepend>
或 <b-input-group-append>
進行包裝,以正確顯示。
<div>
<b-input-group>
<template #prepend>
<b-input-group-text >Username</b-input-group-text>
</template>
<b-form-input></b-form-input>
<template #append>
<b-dropdown text="Dropdown" variant="success">
<b-dropdown-item>Action A</b-dropdown-item>
<b-dropdown-item>Action B</b-dropdown-item>
</b-dropdown>
</template>
</b-input-group>
</div>
使用子元件
使用 <b-input-group-prepend>
或 <b-input-group-append>
來新增任意附加元件,並使用這些組件來在輸入群組中對按鈕進行分組。為了正確套用樣式,必須始終將單一按鈕封裝在這些組件中。
<div>
<b-input-group>
<b-input-group-prepend>
<b-button variant="outline-info">Button</b-button>
</b-input-group-prepend>
<b-form-input type="number" min="0.00"></b-form-input>
<b-input-group-append>
<b-button variant="outline-secondary">Button</b-button>
<b-button variant="outline-primary">Button</b-button>
<b-input-group-text>
<b-icon icon="x" />
</b-input-group-text>
</b-input-group-append>
</b-input-group>
</div>
如果內容本質上是文字,請在 <b-input-group-prepend>
或 <b-input-group-append>
上設定 is-text
屬性以套用正確的樣式。或者,將 <b-input-group-text>
子組件置於 <b-input-group-prepend>
或 <b-input-group-append>
內部。在您想使用 BootstrapVue 的圖示 之一時,此方法也適用。
以下是支援作為輸入群組主輸入元素的表單控制元件
注意事項
- 當
<b-form-file>
輸入置於有指定 size
的 <b-input-group>
中時,BootstrapVue 會使用自訂 SCSS/CSS 來處理縮放。 - 當
<b-form-input type="range">
置於 <b-input-group>
中時,BootstrapVue 會使用自訂 SCSS/CSS。 - BootstrapVue 的自訂組件 (例如
<b-form-spinbutton>
、<b-form-rating>
、<b-form-tags>
等) 需要 BootstrapVue 的自訂 SCSS/CSS。
核取方塊和單選按鈕附加元件
將核取方塊或單選按鈕置於輸入群組的附加元件中,而不是文字。
注意:Bootstrap v4.x 建議使用原生單選按鈕和核取方塊輸入,而不是自訂單選按鈕和核取方塊,但還是可以使用 <b-form-radio>
和 <b-form-checkbox>
搭配套用一些公用程式類別。
原生核取方塊和單選按鈕附加元件
<div>
<b-input-group class="mb-2">
<b-input-group-prepend is-text>
<input type="checkbox" aria-label="Checkbox for following text input">
</b-input-group-prepend>
<b-form-input aria-label="Text input with checkbox"></b-form-input>
</b-input-group>
<b-input-group>
<b-input-group-prepend is-text>
<input type="radio" aria-label="Radio for following text input">
</b-input-group-prepend>
<b-form-input aria-label="Text input with radio input"></b-form-input>
</b-input-group>
</div>
自訂單選按鈕、核取方塊和開關附加元件
使用 <b-form-checkbox>
和 <b-form-radio>
組件當作附加元件,搭配使用 Bootstrap 公用程式類別,以取得其他樣式,使它們「適合」附加元件
<div>
<b-input-group class="mb-2">
<b-input-group-prepend is-text>
<b-form-checkbox class="mr-n2">
<span class="sr-only">Checkbox for following text input</span>
</b-form-checkbox>
</b-input-group-prepend>
<b-form-input aria-label="Text input with checkbox"></b-form-input>
</b-input-group>
<b-input-group class="mb-2">
<b-input-group-prepend is-text>
<b-form-radio class="mr-n2">
<span class="sr-only">Radio for following text input</span>
</b-form-radio>
</b-input-group-prepend>
<b-form-input aria-label="Text input with radio input"></b-form-input>
</b-input-group>
<b-input-group>
<b-input-group-prepend is-text>
<b-form-checkbox switch class="mr-n2">
<span class="sr-only">Switch for following text input</span>
</b-form-checkbox>
</b-input-group-prepend>
<b-form-input aria-label="Text input with switch"></b-form-input>
</b-input-group>
</div>
在上述範例中,我們在 <span>
上使用 .sr-only
類別,以視覺上隱藏自訂控制項的標籤內容 (同時仍讓螢幕閱讀器使用者可以存取),並使用工具程式類別 .mr-n2
加入負右邊界,以彌補控制項和隱藏標籤之間的「隔間」空間。
<div>
<b-input-group prepend="First and last name" class="mb-2">
<b-form-input aria-label="First name"></b-form-input>
<b-form-input aria-label="Last name"></b-form-input>
</b-input-group>
</div>
多個附加元件
支援多個加入項,並且可以與核取方塊和無線按鈕輸入元件版本混用。
<div>
<b-input-group prepend="Item">
<b-input-group-prepend is-text>
<input type="checkbox" aria-label="Checkbox for following text input">
</b-input-group-prepend>
<b-input-group-prepend is-text><b>$</b></b-input-group-prepend>
<b-form-input type="number" aria-label="Text input with checkbox"></b-form-input>
</b-input-group>
</div>
下拉式加入項
<div>
<b-input-group>
<template #prepend>
<b-dropdown text="Dropdown" variant="info">
<b-dropdown-item>Action A</b-dropdown-item>
<b-dropdown-item>Action B</b-dropdown-item>
</b-dropdown>
</template>
<b-form-input></b-form-input>
<template #append>
<b-dropdown text="Dropdown" variant="outline-secondary" v-for="i in 2" :key="i">
<b-dropdown-item>Action C</b-dropdown-item>
<b-dropdown-item>Action D</b-dropdown-item>
</b-dropdown>
</template>
</b-input-group>
</div>
控制大小調整
使用 size
屬性將高度設定為 sm
或 lg
,分別表示小型或大型。無需設定個別輸入元件或按鈕的大小。但是,請注意您必須同時設定下拉式大小。
<div>
<b-input-group
v-for="size in ['sm','','lg']"
:key="size"
:size="size"
class="mb-3"
prepend="Label"
>
<b-form-input></b-form-input>
<b-input-group-append>
<b-button size="sm" text="Button" variant="success">Button</b-button>
</b-input-group-append>
</b-input-group>
</div>
若要控制寬度,請將輸入元件置於標準的 Bootstrap 網格欄中。
設定自訂無線按鈕、核取方塊和開關加入項的大小
如果您將 <b-form-radio>
或 <b-form-checkbox>
作為加入項使用,視所選擇的大小,可能會需要額外的工具程式類別,才能讓所有內容正確組合。
<div>
<b-input-group size="sm" prepend="Small" class="mb-2">
<b-form-input aria-label="Small text input with custom switch"></b-form-input>
<b-input-group-append is-text>
<b-form-checkbox switch class="mr-n2 mb-n1">
<span class="sr-only">Checkbox for previous text input</span>
</b-form-checkbox>
</b-input-group-append>
</b-input-group>
<b-input-group size="lg" prepend="Large" class="mb-2">
<b-form-input aria-label="Large text input with switch"></b-form-input>
<b-input-group-append is-text>
<b-form-checkbox switch class="mr-n2">
<span class="sr-only">Switch for previous text input</span>
</b-form-checkbox>
</b-input-group-append>
</b-input-group>
</div>
具體而言,當在 <b-input-group>
上使用 sm
大小時,您需要使用 .mb-n1
工具程式類別加入負下邊界。
情境狀態
Bootstrap v4 目前不支援輸入群組的狀態樣式 (即有效或無效)。不過,輸入群組內的輸入元件的確支援情境狀態。