Switch 開關
LhSwitch 用於在兩個對立狀態之間切換。
基礎用法
基礎用法
vue
<script setup lang="ts">
import { ref } from 'vue'
const status = ref(false)
</script>
<template>
<!-- 基礎開關 -->
<LhSwitch v-model="status" />
</template>vue
<script>
export default {
data() {
return { status: false }
},
}
</script>
<template>
<!-- 基礎開關 -->
<LhSwitch v-model="status" />
</template>文本標籤
文本標籤
開
vue
<script setup lang="ts">
import { ref } from 'vue'
const notification = ref(true)
</script>
<template>
<LhSwitch v-model="notification" active-text="开" inactive-text="关" />
</template>vue
<script>
export default {
data() {
return { notification: true }
},
}
</script>
<template>
<LhSwitch v-model="notification" active-text="开" inactive-text="关" />
</template>尺寸
尺寸
vue
<script setup lang="ts">
import { ref } from 'vue'
const smallVal = ref(false)
const mediumVal = ref(false)
const largeVal = ref(false)
</script>
<template>
<LhSwitch v-model="smallVal" size="small" />
<LhSwitch v-model="mediumVal" size="medium" />
<LhSwitch v-model="largeVal" size="large" />
</template>vue
<script>
export default {
data() {
return {
smallVal: false,
mediumVal: false,
largeVal: false,
}
},
}
</script>
<template>
<LhSwitch v-model="smallVal" size="small" />
<LhSwitch v-model="mediumVal" size="medium" />
<LhSwitch v-model="largeVal" size="large" />
</template>停用與 Loading
停用與 Loading
vue
<script setup lang="ts">
import { ref } from 'vue'
const disabledVal = ref(true)
const loadingVal = ref(true)
const swName = ref(false)
</script>
<template>
<LhSwitch v-model="disabledVal" disabled />
<LhSwitch v-model="loadingVal" loading />
</template>vue
<script>
export default {
data() {
return {
disabledVal: true,
loadingVal: true,
}
},
}
</script>
<template>
<LhSwitch v-model="disabledVal" disabled />
<LhSwitch v-model="loadingVal" loading />
</template>自訂主題
為開關應用自訂主題色。
青花瓷主題
vue
<script setup lang="ts">
import { LhConfigProvider, LhSwitch } from '@lh-ui/vue3'
const cerblueTheme = {
'primary-color': '#2B4C7C',
'primary-color-hover': '#3D649E',
'primary-color-bg': '#E8EDF3',
}
</script>
<template>
<!-- 青花瓷主題 -->
<LhConfigProvider :theme-vars="cerblueTheme">
<LhSwitch v-model="status" />
</LhConfigProvider>
</template>vue
<script>
import { LhConfigProvider, LhSwitch } from '@lh-ui/vue3'
export default {
components: { LhConfigProvider, LhSwitch },
data() {
return {
status: false,
cerblueTheme: {
'primary-color': '#2B4C7C',
'primary-color-hover': '#3D649E',
'primary-color-bg': '#E8EDF3',
},
}
},
}
</script>
<template>
<!-- 青花瓷主題 -->
<LhConfigProvider :theme-vars="cerblueTheme">
<LhSwitch v-model="status" />
</LhConfigProvider>
</template>國際化
透過 LhConfigProvider 設定全域語言包。
國際化
vue
<script setup lang="ts">
import { ref, computed } from 'vue'
import { LhConfigProvider, LhSwitch, LhButton } from '@lh-ui/vue3'
import { zhCN } from '@lh-ui/vue3'
import { enUS } from '@lh-ui/vue3'
const locale = ref('zh-CN')
const messages = computed(() => locale.value === 'zh-CN' ? zhCN : enUS)
const status = ref(false)
</script>
<template>
<!-- 語言包設定 -->
<LhConfigProvider :locale="messages">
<LhSwitch v-model="status" />
<LhButton @click="locale = locale === 'zh-CN' ? 'en-US' : 'zh-CN'">
切换语言
</LhButton>
</LhConfigProvider>
</template>vue
<script>
import { ref, computed } from 'vue'
import { LhConfigProvider, LhSwitch, LhButton } from '@lh-ui/vue3'
import { zhCN } from '@lh-ui/vue3'
import { enUS } from '@lh-ui/vue3'
export default {
components: { LhConfigProvider, LhSwitch, LhButton },
data() {
return {
locale: 'zh-CN',
status: false,
}
},
computed: {
messages() {
return this.locale === 'zh-CN' ? zhCN : enUS
},
},
}
</script>
<template>
<!-- 語言包設定 -->
<LhConfigProvider :locale="messages">
<LhSwitch v-model="status" />
<LhButton @click="locale = locale === 'zh-CN' ? 'en-US' : 'zh-CN'">
切换语言
</LhButton>
</LhConfigProvider>
</template>原生 name
name 設定原生 input 的 name 屬性(表單提交用)。
name
vue
<script setup lang="ts">
import { ref } from 'vue'
const value = ref(false)
</script>
<template>
<LhSwitch v-model="value" name="notifications" />
</template>vue
<script>
export default {
data() {
return { value: false }
},
}
</script>
<template>
<LhSwitch v-model="value" name="notifications" />
</template>API
Props
| 屬性 | 說明 | 類型 | 預設值 |
|---|---|---|---|
| modelValue | 當前狀態 | boolean | false |
| disabled | 是否停用 | boolean | false |
| loading | 是否載入中 | boolean | false |
| name | 原生表單名稱 | string | '' |
| size | 尺寸 | "small" | "medium" | "large" | "mini" | "x-large" | 'medium' |
| activeText | 開啟時文本 | string | '' |
| inactiveText | 關閉時文本 | string | '' |
| sx | sx 快速版面配置屬性 | LhSxProps | — |
Events
| 事件名 | 說明 | 回呼參數 |
|---|---|---|
| update:modelValue | 狀態改變 | (value: boolean) |
| change | 狀態改變 | (value: boolean) |
觸發 update:modelValue(新的布林值)和 change 事件。支援鍵盤 Enter 和 Space 切換。
類型定義
本元件對外匯出的 TypeScript 類型結構如下(自動提取自 packages/components/switch/src/types.ts 與 @lh-ui/share/core/* 原始碼,點選各條目展開檢視完整定義;屬性說明見上方 Props 表):
LhSwitchProps
ts
export interface LhSwitchProps {
modelValue?: boolean
disabled?: boolean
loading?: boolean
name?: string
size?: LhComponentSize
activeText?: string
inactiveText?: string
/** sx 快速版面配置屬性 */
sx?: LhSxProps
}LhSwitchSize
ts
export type LhSwitchSize = LhComponentSizeLhComponentSize (來自 @lh-ui/share/core/shared)
ts
export type LhComponentSize = (typeof LH_SIZE_CONST)[keyof typeof LH_SIZE_CONST]