element/examples/docs/en-US/switch.md

144 lines
3.3 KiB
Markdown
Raw Normal View History

2016-11-13 11:58:45 +08:00
## Switch
Switch is used for switching between two opposing states.
2016-11-13 11:58:45 +08:00
### Basic usage
2017-10-19 15:54:01 +08:00
:::demo Bind `v-model` to a `Boolean` typed variable. The `active-color` and `inactive-color` attribute decides the background color in two states.
```html
2017-09-13 15:51:01 +08:00
<el-switch v-model="value1">
</el-switch>
<el-switch
v-model="value2"
2017-10-19 15:54:01 +08:00
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
<script>
export default {
data() {
return {
value1: true,
value2: true
}
}
};
</script>
```
:::
2017-09-13 15:51:01 +08:00
### Text description
2017-10-19 15:54:01 +08:00
:::demo You can add `active-text` and `inactive-text` attribute to show texts.
2017-09-13 15:51:01 +08:00
```html
<el-switch
v-model="value3"
2017-10-19 15:54:01 +08:00
active-text="Pay by month"
inactive-text="Pay by year">
2017-09-13 15:51:01 +08:00
</el-switch>
<el-switch
style="display: block"
v-model="value4"
2017-10-19 15:54:01 +08:00
active-color="#13ce66"
inactive-color="#ff4949"
active-text="Pay by month"
inactive-text="Pay by year">
2017-09-13 15:51:01 +08:00
</el-switch>
<script>
export default {
data() {
return {
value3: true,
value4: true
}
}
};
</script>
```
:::
2017-04-28 14:03:42 +08:00
### Extended value types
2017-04-23 14:28:29 +08:00
2017-10-19 15:54:01 +08:00
:::demo You can set `active-value` and `inactive-value` attributes. They both receive a `Boolean`, `String` or `Number` typed value.
2017-04-23 14:28:29 +08:00
```html
2017-09-13 15:51:01 +08:00
<el-tooltip :content="'Switch value: ' + value5" placement="top">
<el-switch
2017-09-13 15:51:01 +08:00
v-model="value5"
2017-10-19 15:54:01 +08:00
active-color="#13ce66"
inactive-color="#ff4949"
active-value="100"
inactive-value="0">
</el-switch>
</el-tooltip>
2017-04-23 14:28:29 +08:00
<script>
export default {
data() {
return {
2017-09-13 15:51:01 +08:00
value5: '100'
2017-04-23 14:28:29 +08:00
}
}
};
</script>
```
:::
2016-11-13 11:58:45 +08:00
### Disabled
:::demo Adding the `disabled` attribute disables Switch.
```html
<el-switch
2017-09-13 15:51:01 +08:00
v-model="value6"
disabled>
</el-switch>
<el-switch
2017-09-13 15:51:01 +08:00
v-model="value7"
disabled>
</el-switch>
<script>
export default {
data() {
return {
2017-09-13 15:51:01 +08:00
value6: true,
value7: false
}
}
};
</script>
```
:::
### Attributes
| Attribute | Description | Type | Accepted Values | Default |
|-----| ----| ----| ----|---- |
| value / v-model | binding value | boolean / string / number | — | — |
| disabled | whether Switch is disabled | 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 |
### Events
| Event Name | Description | Parameters |
| ---- | ----| ---- |
| change | triggers when value changes | value after changing |
### Methods
| Method | Description | Parameters |
| ------|--------|------- |
| focus | focus the Switch component | — |