diff --git a/packages/switch/__tests__/switch.spec.ts b/packages/switch/__tests__/switch.spec.ts
index fbea1055f4..c941f571ab 100644
--- a/packages/switch/__tests__/switch.spec.ts
+++ b/packages/switch/__tests__/switch.spec.ts
@@ -1,5 +1,8 @@
import { mount } from '@vue/test-utils'
import Switch from '../src/index.vue'
+import { nextTick } from 'vue'
+
+jest.useFakeTimers()
describe('Switch.vue', () => {
@@ -228,4 +231,103 @@ describe('Switch.vue', () => {
await vm.$nextTick()
expect(inputEl.checked).toEqual(false)
})
+
+ test('beforeChange function return promise', async () => {
+ const wrapper = mount({
+ components: {
+ 'el-switch': Switch,
+ },
+ template: `
+
+
+
+ `,
+ data() {
+ return {
+ value: true,
+ loading: false,
+ asyncResult: 'error',
+ }
+ },
+ methods: {
+ beforeChange() {
+ this.loading = true
+ return new Promise((resolve, reject) => {
+ setTimeout(() => {
+ this.loading = false
+ return this.asyncResult == 'success'
+ ? resolve(true)
+ : reject(new Error('error'))
+ }, 1000)
+ })
+ },
+ },
+ })
+ const vm = wrapper.vm
+
+ const coreWrapper = wrapper.find('.el-switch__core')
+
+ coreWrapper.trigger('click')
+ jest.runAllTimers()
+ await nextTick()
+ expect(vm.value).toEqual(true)
+
+ vm.asyncResult = 'success'
+
+ coreWrapper.trigger('click')
+ jest.runAllTimers()
+ await nextTick()
+ expect(vm.value).toEqual(false)
+
+ coreWrapper.trigger('click')
+ jest.runAllTimers()
+ await nextTick()
+ expect(vm.value).toEqual(true)
+ })
+
+ test('beforeChange function return boolean', async () => {
+ const wrapper = mount({
+ components: {
+ 'el-switch': Switch,
+ },
+ template: `
+
+
+
+ `,
+ data() {
+ return {
+ value: true,
+ result: false,
+ }
+ },
+ methods: {
+ beforeChange() {
+ // do something ...
+ return this.result
+ },
+ },
+ })
+ const vm = wrapper.vm
+
+ const coreWrapper = wrapper.find('.el-switch__core')
+
+ await coreWrapper.trigger('click')
+ expect(vm.value).toEqual(true)
+
+ vm.result = true
+
+ await coreWrapper.trigger('click')
+ expect(vm.value).toEqual(false)
+
+ await coreWrapper.trigger('click')
+ expect(vm.value).toEqual(true)
+ })
})
diff --git a/packages/switch/src/index.vue b/packages/switch/src/index.vue
index 4c65fb662b..963b6015f9 100644
--- a/packages/switch/src/index.vue
+++ b/packages/switch/src/index.vue
@@ -43,8 +43,12 @@
```
+
:::
### Text description
+
:::demo You can add `active-text` and `inactive-text` attribute to show texts.
```html
+ inactive-text="Pay by year"
+>
+ inactive-text="Pay by year"
+>
```
+
:::
### Extended value types
@@ -69,7 +71,8 @@ Switch is used for switching between two opposing states.
active-color="#13ce66"
inactive-color="#ff4949"
active-value="100"
- inactive-value="0">
+ inactive-value="0"
+ >
@@ -77,10 +80,10 @@ Switch is used for switching between two opposing states.
export default {
data() {
return {
- value: '100'
+ value: '100',
}
- }
- };
+ },
+ }
```
@@ -91,26 +94,21 @@ Switch is used for switching between two opposing states.
:::demo Adding the `disabled` attribute disables Switch.
```html
-
-
-
-
+
+
```
+
:::
### Loading
@@ -118,53 +116,110 @@ Switch is used for switching between two opposing states.
:::demo Setting the `loading` attribute to `true` indicates a loading state on the Switch.
```html
-
-
-
-
+
+
```
+
+:::
+
+### prevent switching
+
+:::demo set the `beforeChange` property, If `false` is returned or a `Promise` is returned and then is rejected, will stop switching.
+
+```html
+
+
+
+
+
+```
+
:::
### Attributes
-| Attribute | Description | Type | Accepted Values | Default |
-|-----| ----| ----| ----|---- |
-| value / v-model | binding value, it should be equivalent to either `active-value` or `inactive-value`, by default it's `boolean` type | boolean / string / number | — | — |
-| disabled | whether Switch is disabled | boolean | — | false |
-| loading | whether Switch is in loading state | boolean | — | false |
-| width | width of Switch | number | — | 40 |
-| active-icon-class | class name of the icon displayed when in `on` state, overrides `active-text` | string | — | — |
-| inactive-icon-class |class name of the icon displayed when in `off` state, overrides `inactive-text`| string | — | — |
-| active-text | text displayed when in `on` state | string | — | — |
-| inactive-text | text displayed when in `off` state | string | — | — |
-| active-value | switch value when in `on` state | boolean / string / number | — | true |
-| inactive-value | switch value when in `off` state | boolean / string / number | — | false |
-| active-color | background color when in `on` state | string | — | #409EFF |
-| inactive-color | background color when in `off` state | string | — | #C0CCDA |
-| name | input name of Switch | string | — | — |
-| validate-event | whether to trigger form validation | boolean | - | true |
+| Attribute | Description | Type | Accepted Values | Default |
+| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- | --------------- | ------- |
+| value / v-model | binding value, it should be equivalent to either `active-value` or `inactive-value`, by default it's `boolean` type | boolean / string / number | — | — |
+| disabled | whether Switch is disabled | boolean | — | false |
+| loading | whether Switch is in loading state | boolean | — | false |
+| width | width of Switch | number | — | 40 |
+| active-icon-class | class name of the icon displayed when in `on` state, overrides `active-text` | string | — | — |
+| inactive-icon-class | class name of the icon displayed when in `off` state, overrides `inactive-text` | string | — | — |
+| active-text | text displayed when in `on` state | string | — | — |
+| inactive-text | text displayed when in `off` state | string | — | — |
+| active-value | switch value when in `on` state | boolean / string / number | — | true |
+| inactive-value | switch value when in `off` state | boolean / string / number | — | false |
+| active-color | background color when in `on` state | string | — | #409EFF |
+| inactive-color | background color when in `off` state | string | — | #C0CCDA |
+| name | input name of Switch | string | — | — |
+| validate-event | whether to trigger form validation | boolean | — | true |
+| before-change | before-change hook before the switch state changes. If `false` is returned or a `Promise` is returned and then is rejected, will stop switching | function | — | — |
### Events
-| Event Name | Description | Parameters |
-| ---- | ----| ---- |
-| change | triggers when value changes | value after changing |
+| Event Name | Description | Parameters |
+| ---------- | --------------------------- | -------------------- |
+| change | triggers when value changes | value after changing |
### Methods
-| Method | Description | Parameters |
-| ------|--------|------- |
-| focus | focus the Switch component | — |
+
+| Method | Description | Parameters |
+| ------ | -------------------------- | ---------- |
+| focus | focus the Switch component | — |
diff --git a/website/docs/es/switch.md b/website/docs/es/switch.md
index 114de10e13..a9b7502c09 100644
--- a/website/docs/es/switch.md
+++ b/website/docs/es/switch.md
@@ -7,12 +7,8 @@ Switch es utilizado para realizar cambios entre dos estados opuestos.
:::demo Enlace `v-model` a una variable de tipo `Boolean`. Los atributos `active-color` y `inactive-color` deciden el color de fondo en cada estado.
```html
-
-
-
+
+
```
+
:::
### Texto de descripción
+
:::demo Puede agregar los atributos `active-text` y `inactive-text` para mostrar los textos.
```html
+ inactive-text="Pay by year"
+>
+ inactive-text="Pay by year"
+>
```
+
:::
### Tipos de valores extendidos
@@ -70,7 +71,8 @@ Switch es utilizado para realizar cambios entre dos estados opuestos.
active-color="#13ce66"
inactive-color="#ff4949"
active-value="100"
- inactive-value="0">
+ inactive-value="0"
+ >
@@ -78,10 +80,10 @@ Switch es utilizado para realizar cambios entre dos estados opuestos.
export default {
data() {
return {
- value: '100'
+ value: '100',
}
- }
- };
+ },
+ }
```
@@ -92,26 +94,21 @@ Switch es utilizado para realizar cambios entre dos estados opuestos.
:::demo Agregar el atributo `disabled` desactiva el componente Switch.
```html
-
-
-
-
+
+
```
+
:::
### Loading
@@ -119,54 +116,112 @@ Switch es utilizado para realizar cambios entre dos estados opuestos.
:::demo Setting the `loading` attribute to `true` indicates a loading state on the Switch.
```html
-
-
-
-
+
+
```
+
+:::
+
+### prevent switching
+
+:::demo set the `beforeChange` property, If `false` is returned or a `Promise` is returned and then is rejected, will stop switching.
+
+```html
+
+
+
+
+
+```
+
:::
### Atributos
-| Atributo | Descripción | Tipo | Valores aceptados | Por defecto |
-| ------------------- | ---------------------------------------- | ------------------------- | ----------------- | ----------- |
-| value / v-model |valor vinculante, debe ser equivalente al `active-value` o al `inactive-value`. El tipo por defecto es el tipo `boolean`. | boolean / string / number | — | — |
-| disabled | si Switch esta deshabilitado | boolean | — | false |
-| loading | whether Switch is in loading state | boolean | — | false |
-| width | ancho del componente Switch | number | — | 40 |
-| active-icon-class | nombre de la clase del icono mostrado en el estado `on`, sobrescribe `active-text` | string | — | — |
-| inactive-icon-class | nombre de la clase del icono mostrado en el estado `off`, sobrescribe `inactive-text` | string | — | — |
-| active-text | texto mostrado en el estado `on` | string | — | — |
-| inactive-text | texto mostrado en el estado `off` | string | — | — |
-| active-value | cambia su valor cuando se encuentra en el estado `on` | boolean / string / number | — | true |
-| inactive-value | cambia su valor cuando se encuentra en el estado `off` | boolean / string / number | — | false |
-| active-color | color de fondo cuando se encuentra en el estado `on` | string | — | #409EFF |
-| inactive-color | color de fondo cuando se encuentra en el estado `off` | string | — | #C0CCDA |
-| name | nombre de entrada del componente Switch | string | — | — |
-| validate-event | si se debe lanzar la validación de formulario | boolean | - | true |
+| Atributo | Descripción | Tipo | Valores aceptados | Por defecto |
+| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- | ----------------- | ----------- |
+| value / v-model | valor vinculante, debe ser equivalente al `active-value` o al `inactive-value`. El tipo por defecto es el tipo `boolean`. | boolean / string / number | — | — |
+| disabled | si Switch esta deshabilitado | boolean | — | false |
+| loading | whether Switch is in loading state | boolean | — | false |
+| width | ancho del componente Switch | number | — | 40 |
+| active-icon-class | nombre de la clase del icono mostrado en el estado `on`, sobrescribe `active-text` | string | — | — |
+| inactive-icon-class | nombre de la clase del icono mostrado en el estado `off`, sobrescribe `inactive-text` | string | — | — |
+| active-text | texto mostrado en el estado `on` | string | — | — |
+| inactive-text | texto mostrado en el estado `off` | string | — | — |
+| active-value | cambia su valor cuando se encuentra en el estado `on` | boolean / string / number | — | true |
+| inactive-value | cambia su valor cuando se encuentra en el estado `off` | boolean / string / number | — | false |
+| active-color | color de fondo cuando se encuentra en el estado `on` | string | — | #409EFF |
+| inactive-color | color de fondo cuando se encuentra en el estado `off` | string | — | #C0CCDA |
+| name | nombre de entrada del componente Switch | string | — | — |
+| validate-event | si se debe lanzar la validación de formulario | boolean | — | true |
+| before-change | before-change hook before the switch state changes. If `false` is returned or a `Promise` is returned and then is rejected, will stop switching | function | — | — |
### Eventos
| Nombre del evento | Descripción | Parametro |
| ----------------- | --------------------------------- | --------- |
| change | se dispara cuando el valor cambia | valor |
+
después de cambiar
### Metodos
+
| Metodo | Descripción | Parametro |
| ------ | ------------------------- | --------- |
| focus | foco al componente Switch | — |
diff --git a/website/docs/fr-FR/switch.md b/website/docs/fr-FR/switch.md
index 9c02d8a38c..70814f0141 100644
--- a/website/docs/fr-FR/switch.md
+++ b/website/docs/fr-FR/switch.md
@@ -7,12 +7,8 @@ Switch est utilisé pour choisir entre deux états opposés.
:::demo Liez `v-model` à une variable de type `Boolean`. Les attributs `active-color` et `inactive-color` déterminent les couleurs des deux états.
```html
-
-
-
+
+
```
+
:::
### Description
@@ -36,7 +33,8 @@ Switch est utilisé pour choisir entre deux états opposés.
+ inactive-text="Paiement annuel"
+>
+ inactive-text="Paiement annuel"
+>
```
+
:::
### Valeurs des états
@@ -71,7 +71,8 @@ Switch est utilisé pour choisir entre deux états opposés.
active-color="#13ce66"
inactive-color="#ff4949"
active-value="100"
- inactive-value="0">
+ inactive-value="0"
+ >
@@ -79,10 +80,10 @@ Switch est utilisé pour choisir entre deux états opposés.
export default {
data() {
return {
- value: '100'
+ value: '100',
}
- }
- };
+ },
+ }
```
@@ -93,26 +94,21 @@ Switch est utilisé pour choisir entre deux états opposés.
:::demo Ajoutez l'attribut `disabled` pour désactiver le switch.
```html
-
-
-
-
+
+
```
+
:::
### Loading
@@ -120,54 +116,110 @@ Switch est utilisé pour choisir entre deux états opposés.
:::demo Setting the `loading` attribute to `true` indicates a loading state on the Switch.
```html
-
-
-
-
+
+
```
+
+:::
+
+### Empêcher la commutation
+
+:::demo Définissez la propriété `beforeChange`. Si elle renvoie false ou renvoie une promesse et est rejetée, le commutateur s'arrêtera.
+
+```html
+
+
+
+
+
+```
+
:::
### Attributs
-| Attribut | Description | Type | Valeurs acceptées | Défaut |
-| ----| ----| ----| ----|---- |
-| value / v-model | Valeur liée. Elle doit être équivalente à `active-value` ou `inactive-value`, par défaut elle est de type `boolean`. | boolean / string / number | — | — |
-| disabled | Si le switch est désactivé. | boolean | — | false |
-| loading | whether Switch is in loading state | boolean | — | false |
-| width | Largeur du switch. | number | — | 40 |
-| active-icon-class | Classe de l'icône de l'état `on`, écrase `active-text`. | string | — | — |
-| inactive-icon-class | Classe de l'icône de l'état `off`, écrase `inactive-text`. | string | — | — |
-| active-text | Texte affiché dans l'état `on`. | string | — | — |
-| inactive-text | Texte affiché dans l'état `off`. | string | — | — |
-| active-value | Valeur du switch dans l'état `on`. | boolean / string / number | — | true |
-| inactive-value | Valeur du switch dans l'état `off`. | boolean / string / number | — | false |
-| active-color | Couleur de fond de l'état `on`. | string | — | #409EFF |
-| inactive-color | Couleur de fond de l'état `off`. | string | — | #C0CCDA |
-| name| Nom du champ d'input du switch. | string | — | — |
-| validate-event | Si la validation doit avoir lieu. | boolean | - | true |
+| Attribut | Description | Type | Valeurs acceptées | Défaut |
+| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- | ----------------- | ------- |
+| value / v-model | Valeur liée. Elle doit être équivalente à `active-value` ou `inactive-value`, par défaut elle est de type `boolean`. | boolean / string / number | — | — |
+| disabled | Si le switch est désactivé. | boolean | — | false |
+| loading | whether Switch is in loading state | boolean | — | false |
+| width | Largeur du switch. | number | — | 40 |
+| active-icon-class | Classe de l'icône de l'état `on`, écrase `active-text`. | string | — | — |
+| inactive-icon-class | Classe de l'icône de l'état `off`, écrase `inactive-text`. | string | — | — |
+| active-text | Texte affiché dans l'état `on`. | string | — | — |
+| inactive-text | Texte affiché dans l'état `off`. | string | — | — |
+| active-value | Valeur du switch dans l'état `on`. | boolean / string / number | — | true |
+| inactive-value | Valeur du switch dans l'état `off`. | boolean / string / number | — | false |
+| active-color | Couleur de fond de l'état `on`. | string | — | #409EFF |
+| inactive-color | Couleur de fond de l'état `off`. | string | — | #C0CCDA |
+| name | Nom du champ d'input du switch. | string | — | — |
+| validate-event | Si la validation doit avoir lieu. | boolean | — | true |
+| before-change | Le hook avant le changement d'état du commutateur. S'il renvoie false ou renvoie une promesse et est rejeté, le commutateur s'arrêtera. | function | — | — |
### Évènements
-| Nom | Description | Paramètres |
-| ---- | ----| ---- |
+| Nom | Description | Paramètres |
+| ------ | ------------------------------------ | --------------------------- |
| change | Se déclenche quand la valeur change. | La valeur après changement. |
### Méthodes
-| Méthode | Description | Paramètres |
-|-------|--------|------- |
-| focus | Donne le focus au switch. | — |
+| Méthode | Description | Paramètres |
+| ------- | ------------------------- | ---------- |
+| focus | Donne le focus au switch. | — |
diff --git a/website/docs/jp/switch.md b/website/docs/jp/switch.md
index 6773ac1b6a..4f8e13cb3b 100644
--- a/website/docs/jp/switch.md
+++ b/website/docs/jp/switch.md
@@ -1,17 +1,14 @@
## スイッチ
-スイッチは、2つの状態を切り替えるために使用されます。
+スイッチは、2 つの状態を切り替えるために使用されます。
### 基本的な使い方
-:::demo `v-model` を `Boolean` 型変数にバインドする。`active-color`と`inactive-color`属性は、2つの状態の背景色を決定する。
+
+:::demo `v-model` を `Boolean` 型変数にバインドする。`active-color`と`inactive-color`属性は、2 つの状態の背景色を決定する。
```html
-
-
-
+
+
```
+
:::
### テキストの説明
+
:::demo テキストを表示するために `active-color` と `inactive-color` 属性を追加することができます。
```html
+ inactive-text="Pay by year"
+>
+ inactive-text="Pay by year"
+>
```
+
:::
### 拡張された値型
@@ -69,7 +71,8 @@
active-color="#13ce66"
inactive-color="#ff4949"
active-value="100"
- inactive-value="0">
+ inactive-value="0"
+ >
@@ -77,10 +80,10 @@
export default {
data() {
return {
- value: '100'
+ value: '100',
}
- }
- };
+ },
+ }
```
@@ -91,26 +94,21 @@
:::demo `disabled`属性を追加すると、スイッチを無効にすることができます。
```html
-
-
-
-
+
+
```
+
:::
### ローディング
@@ -118,53 +116,110 @@
:::demo `loading`属性を`true`に設定すると、ロード状態を表示することができます。
```html
-
-
-
-
+
+
```
+
+:::
+
+### 切り替えを防ぐ
+
+:::demo `beforeChange`プロパティを設定します。false を返すか、Promise を返し、拒否された場合は、切り替えを停止します。
+
+```html
+
+
+
+
+
+```
+
:::
### 属性
-| Attribute | Description | Type | Accepted Values | Default |
-|-----| ----| ----| ----|---- |
-| value / v-model | バインド値は、"active-value "または "inactive-value "と等しくなければなりません。デフォルトの型は "boolean "です。 | boolean / string / number | — | — |
-| disabled | スイッチが無効になっているかどうか | boolean | — | false |
-| loading | スイッチがロード中になっているかどうか | boolean | — | false |
-| width | スイッチの幅 | number | — | 40 |
-| active-icon-class | `on` 状態のときに表示されるアイコンのクラス名で、`active-text` を上書きします。 | string | — | — |
-| inactive-icon-class |`off` 状態のときに表示されるアイコンのクラス名で、`inactive-text` を上書きします。| string | — | — |
-| active-text | `on` 状態のときに表示されるテキスト | string | — | — |
-| inactive-text | `off` 状態のときに表示されるテキスト | string | — | — |
-| active-value | `on` 状態のときのスイッチの値 | boolean / string / number | — | true |
-| inactive-value | `off` 状態のときのスイッチの値 | boolean / string / number | — | false |
-| active-color | `on` 状態のときの背景色 | string | — | #409EFF |
-| inactive-color | `off` 状態のときの背景色 | string | — | #C0CCDA |
-| name | スイッチのインプット名 | string | — | — |
-| validate-event | フォームバリデーションをトリガするかどうか | boolean | - | true |
+| Attribute | Description | Type | Accepted Values | Default |
+| ------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------------- | --------------- | ------- |
+| value / v-model | バインド値は、"active-value "または "inactive-value "と等しくなければなりません。デフォルトの型は "boolean "です。 | boolean / string / number | — | — |
+| disabled | スイッチが無効になっているかどうか | boolean | — | false |
+| loading | スイッチがロード中になっているかどうか | boolean | — | false |
+| width | スイッチの幅 | number | — | 40 |
+| active-icon-class | `on` 状態のときに表示されるアイコンのクラス名で、`active-text` を上書きします。 | string | — | — |
+| inactive-icon-class | `off` 状態のときに表示されるアイコンのクラス名で、`inactive-text` を上書きします。 | string | — | — |
+| active-text | `on` 状態のときに表示されるテキスト | string | — | — |
+| inactive-text | `off` 状態のときに表示されるテキスト | string | — | — |
+| active-value | `on` 状態のときのスイッチの値 | boolean / string / number | — | true |
+| inactive-value | `off` 状態のときのスイッチの値 | boolean / string / number | — | false |
+| active-color | `on` 状態のときの背景色 | string | — | #409EFF |
+| inactive-color | `off` 状態のときの背景色 | string | — | #C0CCDA |
+| name | スイッチのインプット名 | string | — | — |
+| validate-event | フォームバリデーションをトリガするかどうか | boolean | — | true |
+| before-change | スイッチの状態が変化する前のフックは、false を返すか、Promise を返し、切り替えを停止するために拒否されます | function | — | — |
### イベント
-| Event Name | Description | Parameters |
-| ---- | ----| ---- |
-| change | 値が変わるとトリガー | value after changing |
+| Event Name | Description | Parameters |
+| ---------- | -------------------- | -------------------- |
+| change | 値が変わるとトリガー | value after changing |
### メソッド
-| Method | Description | Parameters |
-| ------|--------|------- |
-| focus | スイッチコンポーネントにフォーカス | — |
+
+| Method | Description | Parameters |
+| ------ | ---------------------------------- | ---------- |
+| focus | スイッチコンポーネントにフォーカス | — |
diff --git a/website/docs/zh-CN/switch.md b/website/docs/zh-CN/switch.md
index 24176047dc..40b05b8fb2 100644
--- a/website/docs/zh-CN/switch.md
+++ b/website/docs/zh-CN/switch.md
@@ -7,22 +7,20 @@
:::demo 绑定`v-model`到一个`Boolean`类型的变量。可以使用`active-color`属性与`inactive-color`属性来设置开关的背景色。
```html
-
+
```
+
:::
### 文字描述
@@ -30,10 +28,7 @@
:::demo 使用`active-text`属性与`inactive-text`属性来设置开关的文字描述。
```html
-
+
+ inactive-text="按年付费"
+>
```
+
:::
### 扩展的 value 类型
@@ -68,7 +65,8 @@
active-color="#13ce66"
inactive-color="#ff4949"
active-value="100"
- inactive-value="0">
+ inactive-value="0"
+ >
@@ -76,10 +74,10 @@
export default {
data() {
return {
- value: '100'
+ value: '100',
}
- }
- };
+ },
+ }
```
@@ -89,81 +87,132 @@
:::demo 设置`disabled`属性,接受一个`Boolean`,设置`true`即可禁用。
-
```html
-
-
-
-
+
+
```
+
:::
### 加载中
:::demo 设置`loading`属性,接受一个`Boolean`,设置`true`即加载中状态。
-
```html
-
-
-
-
+
+
```
+
+:::
+
+### 阻止切换
+
+:::demo 设置`beforeChange`属性,若返回 false 或者返回 Promise 且被 reject,则停止切换。
+
+```html
+
+
+
+
+
+```
+
:::
### Attributes
-| 参数 | 说明 | 类型 | 可选值 | 默认值 |
-|---------- |-------- |---------- |------------- |-------- |
-| value / v-model | 绑定值,必须等于`active-value`或`inactive-value`,默认为`Boolean`类型 | boolean / string / number | — | — |
-| disabled | 是否禁用 | boolean | — | false |
-| loading | 是否显示加载中 | boolean | — | false |
-| width | switch 的宽度(像素) | number | — | 40 |
-| active-icon-class | switch 打开时所显示图标的类名,设置此项会忽略 `active-text` | string | — | — |
-| inactive-icon-class | switch 关闭时所显示图标的类名,设置此项会忽略 `inactive-text` | string | — | — |
-| active-text | switch 打开时的文字描述 | string | — | — |
-| inactive-text | switch 关闭时的文字描述 | string | — | — |
-| active-value | switch 打开时的值 | boolean / string / number | — | true |
-| inactive-value | switch 关闭时的值 | boolean / string / number | — | false |
-| active-color | switch 打开时的背景色 | string | — | #409EFF |
-| inactive-color | switch 关闭时的背景色 | string | — | #C0CCDA |
-| name | switch 对应的 name 属性 | string | — | — |
-| validate-event | 改变 switch 状态时是否触发表单的校验 | boolean | - | true |
+| 参数 | 说明 | 类型 | 可选值 | 默认值 |
+| ------------------- | --------------------------------------------------------------------------- | ------------------------- | ------ | ------- |
+| value / v-model | 绑定值,必须等于`active-value`或`inactive-value`,默认为`Boolean`类型 | boolean / string / number | — | — |
+| disabled | 是否禁用 | boolean | — | false |
+| loading | 是否显示加载中 | boolean | — | false |
+| width | switch 的宽度(像素) | number | — | 40 |
+| active-icon-class | switch 打开时所显示图标的类名,设置此项会忽略 `active-text` | string | — | — |
+| inactive-icon-class | switch 关闭时所显示图标的类名,设置此项会忽略 `inactive-text` | string | — | — |
+| active-text | switch 打开时的文字描述 | string | — | — |
+| inactive-text | switch 关闭时的文字描述 | string | — | — |
+| active-value | switch 打开时的值 | boolean / string / number | — | true |
+| inactive-value | switch 关闭时的值 | boolean / string / number | — | false |
+| active-color | switch 打开时的背景色 | string | — | #409EFF |
+| inactive-color | switch 关闭时的背景色 | string | — | #C0CCDA |
+| name | switch 对应的 name 属性 | string | — | — |
+| validate-event | 改变 switch 状态时是否触发表单的校验 | boolean | — | true |
+| before-change | switch 状态改变前的钩子,返回 false 或者返回 Promise 且被 reject 则停止切换 | function | — | — |
### Events
-| 事件名称 | 说明 | 回调参数 |
-|---------- |-------- |---------- |
-| change | switch 状态发生变化时的回调函数 | 新状态的值 |
+
+| 事件名称 | 说明 | 回调参数 |
+| -------- | ------------------------------- | ---------- |
+| change | switch 状态发生变化时的回调函数 | 新状态的值 |
### Methods
-| 方法名 | 说明 | 参数 |
-| ---- | ---- | ---- |
-| focus | 使 Switch 获取焦点 | - |
+
+| 方法名 | 说明 | 参数 |
+| ------ | ------------------ | ---- |
+| focus | 使 Switch 获取焦点 | - |