Switch スイッチ
LhSwitch は、2 つの対立する状態を切り替えるために使用します。
基本的な使い方
基本的な使い方
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]