Skip to content
API 风格:

Interrupteur ​

LhSwitch permet de basculer entre deux états opposés.

Utilisation de base ​

Utilisation de base
vue
<script setup lang="ts">
import { ref } from 'vue'

const status = ref(false)
</script>

<template>
  <!-- Interrupteur de base -->
  <LhSwitch v-model="status" />
</template>
vue
<script>
export default {
  data() {
    return { status: false }
  },
}
</script>

<template>
  <!-- Interrupteur de base -->
  <LhSwitch v-model="status" />
</template>

Libellés ​

Libellés
Marche
vue
<script setup lang="ts">
import { ref } from 'vue'

const notification = ref(true)
</script>

<template>
  <LhSwitch v-model="notification" active-text="Marche" inactive-text="Arrêt" />
</template>
vue
<script>
export default {
  data() {
    return { notification: true }
  },
}
</script>

<template>
  <LhSwitch v-model="notification" active-text="Marche" inactive-text="Arrêt" />
</template>

Tailles ​

Tailles
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>

Désactivé et chargement ​

Désactivé et chargement
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>

Thème personnalisé ​

Appliquez une couleur de thème personnalisée à l'interrupteur.

Thème porcelaine bleue
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>
  <!-- Thème porcelaine bleue -->
  <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>
  <!-- Thème porcelaine bleue -->
  <LhConfigProvider :theme-vars="cerblueTheme">
    <LhSwitch v-model="status" />
  </LhConfigProvider>
</template>

Internationalisation ​

Configurez le pack de langue global via LhConfigProvider.

Internationalisation
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>
  <!-- Configuration du pack de langue -->
  <LhConfigProvider :locale="messages">
    <LhSwitch v-model="status" />
    <LhButton @click="locale = locale === 'zh-CN' ? 'en-US' : 'zh-CN'">
      Changer de langue
    </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>
  <!-- Configuration du pack de langue -->
  <LhConfigProvider :locale="messages">
    <LhSwitch v-model="status" />
    <LhButton @click="locale = locale === 'zh-CN' ? 'en-US' : 'zh-CN'">
      Changer de langue
    </LhButton>
  </LhConfigProvider>
</template>

Attribut name natif ​

name définit l'attribut name du champ natif (pour la soumission de formulaire).

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 ​

PropDescriptionTypeValeur par défaut
modelValueÉtat actuelbooleanfalse
disabledDésactivé ou nonbooleanfalse
loadingEn cours de chargement ou nonbooleanfalse
nameNom natif du formulairestring''
sizeTaille"small" | "medium" | "large" | "mini" | "x-large"'medium'
activeTextTexte à l'état activéstring''
inactiveTextTexte à l'état désactivéstring''
sxProps de mise en page sxLhSxProps—

Events ​

ÉvénementDescriptionParamètre
update:modelValueL'état change(value: boolean)
changeL'état change(value: boolean)

Déclenche les événements update:modelValue (nouvelle valeur booléenne) et change. La bascule est également possible au clavier avec les touches Entrée et Espace.

Définitions de types ​

Ce composant exporte les structures de types TypeScript suivantes (extraites automatiquement de packages/components/switch/src/types.ts et des sources @lh-ui/share/core/* ; cliquez sur chaque entrée pour déplier la définition complète ; la description des props se trouve dans le tableau Props ci-dessus) :

LhSwitchProps
ts
export interface LhSwitchProps {
  modelValue?: boolean
  disabled?: boolean
  loading?: boolean
  name?: string
  size?: LhComponentSize
  activeText?: string
  inactiveText?: string
  /** Props de mise en page sx */
  sx?: LhSxProps
}
LhSwitchSize
ts
export type LhSwitchSize = LhComponentSize
LhComponentSize (provenant de @lh-ui/share/core/shared)
ts
export type LhComponentSize = (typeof LH_SIZE_CONST)[keyof typeof LH_SIZE_CONST]

La beauté de la tradition, la fondation du code