Skip to content
目录

Switch

The switch selector used for switching between two states.

Basic usage

v-model is the binding value, true is the checked state, false is the unchecked state, checked-color is the background color of the checked state, and unchecked-color is the background color of the unchecked state.

vue
<template>
  <div class="box" style="display: flex; gap: 10px">
    <dk-switch v-model="checkValue"></dk-switch>
    <dk-switch
      v-model="checkValue2"
      checked-color="#10b981"
      unchecked-color="#d33f3f"
    ></dk-switch>
  </div>
</template>
<script setup lang="ts">
  import { reactive, toRefs } from 'vue'
  const data = reactive({
    checkValue: false,
    checkValue2: true
  })
  const { checkValue, checkValue2 } = toRefs(data)
</script>
展开

Disabled

disabled indicates the disabled state.

vue
<template>
<div class="box" style="display: flex; gap: 10px">
  <dk-switch v-model="checkValue" disabled></dk-switch>
  <dk-switch v-model="checkValue2" disabled></dk-switch>
</div>
</template>
<script setup lang="ts">
import { reactive, toRefs } from 'vue'
const data = reactive({
  checkValue: false,
  checkValue2: true
})
const { checkValue, checkValue2 } = toRefs(data)
</script>
展开

Size

size is the size, optional values are large, medium, small, mini, default is small.

vue
<template>
<div class="box" style="display: flex; gap: 10px">
  <dk-switch v-model="checkValue" size="large"></dk-switch>
  <dk-switch v-model="checkValue" size="medium"></dk-switch>
  <dk-switch v-model="checkValue" size="small"></dk-switch>
  <dk-switch v-model="checkValue" size="mini"></dk-switch>
</div>
</template>
<script setup lang="ts">
import { reactive, toRefs } from 'vue'
const data = reactive({
  checkValue: false
})
const { checkValue } = toRefs(data)
</script>
展开

Text

checked-text is a selected copy, while unchecked-text is an unselected copy. Width is the width of the switch. When the width is not set, the width of the switch is adaptive. When the width is set, the width of the switch is the set width, and ellipses are displayed for any excess.

vue
<template>
<div class='box' style='display: flex; gap: 10px; align-items: center'>
  <dk-switch 
    v-model='checkValue'
    checked-text='open'
    unchecked-text='close'
  ></dk-switch>
  <dk-switch
    v-model='checkValue2'
    checked-text='open'
    unchecked-text='close'
  ></dk-switch>
</div>
<div class='box' style='display: flex; gap: 10px; align-items: center; margin-top:10px'>
  <dk-switch
    v-model='checkValue'
    checked-text='Set width'
    unchecked-text='Switch width exceeds display ellipsis'
    width="100px"
  ></dk-switch>
  <dk-switch
    v-model='checkValue2'
    checked-text='No width set'
    unchecked-text='Width adaptation of switches'
  ></dk-switch>
</div>
</template>
<script setup lang="ts">
import { reactive, toRefs } from 'vue'
const data = reactive({
  checkValue: false,
  checkValue2: true
})
const { checkValue, checkValue2 } = toRefs(data)
</script>
展开

Switch with icon

checked-icon is the icon of the selected state, and unchecked-icon is the icon of the unchecked state.

vue
<template>
<div class='box' style='display: flex; gap: 10px; align-items: center'>
  <dk-switch
    v-model='checkValue'
    checked-icon='check'
    unchecked-icon='close'
  ></dk-switch>
  <dk-switch
    v-model='checkValue2'
    checked-icon='check'
    unchecked-icon='close'
  ></dk-switch>
</div>
</template>
<script setup lang="ts">
import { reactive, toRefs } from 'vue'
const data = reactive({
  checkValue: false,
  checkValue2: true
})
const { checkValue, checkValue2 } = toRefs(data)
</script>
展开

Loading

loading is the Loading state, checked-custom-icon is the custom icon of the selected state, and unchecked-custom-icon is the custom icon of the unchecked state.

vue
<template>
<div class='box' style='display: flex; gap: 10px; align-items: center'>
  <dk-switch v-model='checkValue' loading></dk-switch>
  <dk-switch v-model='checkValue2' loading></dk-switch>
</div>
<div class='box' style='display: flex; gap: 10px; align-items: center; margin-top:10px'>
  <dk-switch
    v-model='checkValue'
    checked-custom-icon='IconCheck'
    unchecked-custom-icon='IconClose'
  ></dk-switch>
  <dk-switch
    v-model='checkValue2'
    checked-custom-icon='IconCheck'
    unchecked-custom-icon='IconClose'
  ></dk-switch>
</div>
</template>
<script setup lang="ts">
import { reactive, toRefs } from 'vue'
const data = reactive({
  checkValue: false,
  checkValue2: true
})
const { checkValue, checkValue2 } = toRefs(data)
</script>
展开

attribute

parameterexplaintypeOptional valuesDefault value
v-modelBinding valueboolean--
checked-colorBackground color when selectedstring-#409eff
unchecked-colorBackground color when not selectedstring-#c0ccda
checked-textSelected copystring--
unchecked-textCopy when not selectedstring--
checked-iconIcon when selectedstring--
unchecked-iconThe icon when it is not selectedstring--
checked-custom-iconCustom icon when selectedstring--
unchecked-custom-iconCustom icon when not selectedstring--
widthWidth of switchstring--
sizeSize of switchstringlarge medium small minismall
disabledWhether to disablebooleantrue falsefalse
loadingWhether it is loadedbooleantrue falsefalse

Event

EventexplainCallback
changeTriggered when the switch status changes(value: boolean) => value

Contributors