Admin 元件

Switch

參考API
布林狀態開關。

安裝

Terminal
pnpm dlx shadcn-vue add @admin/switch
import { AdminSwitch } from '@/components/admin/switch'

用法

<script setup lang="ts">import { ref } from 'vue'import { AdminSwitch } from '@/components/admin/switch'​const on = ref(false)</script>​<template>  <admin-switch v-model="on" /></template>

場景

帶文字標籤

<script setup lang="ts">import { ref } from 'vue'import { AdminSwitch } from '@/components/admin/switch'​const on = ref(true)</script>​<template>  <admin-switch v-model="on" checked-text="ON" unchecked-text="OFF" /></template>

停用狀態

<script setup lang="ts">import { AdminSwitch } from '@/components/admin/switch'</script>​<template>  <div class="flex items-center gap-6">    <admin-switch disabled :model-value="false" />    <admin-switch disabled :model-value="true" />  </div></template>

API 參考

組成

<template>  <admin-switch v-model="value" /></template>

admin-switch

Props

名稱型別預設值說明
defaultValueboolean | undefined—The state of the switch when it is initially rendered. Use when you do not need to control its state.
modelValueboolean | null | undefined—The controlled state of the switch. Can be bind as `v-model`.
disabledboolean | undefined—When `true`, prevents the user from interacting with the switch.
idstring | undefined——
valuestring | undefined—The value given as data when submitted with a `name`.
trueValueboolean | undefined—The value used when the switch is on. Defaults to `true`.
falseValueboolean | undefined—The value used when the switch is off. Defaults to `false`.
asChildboolean | undefined—Change the default rendered element for the one passed as a child, merging their props and behavior. Read our [Composition](https://www.reka-ui.com/docs/guides/composition) guide for more details.
asglobalThis.Component | AsTag | undefined"div"The element or component this component should render as. Can be overwritten by `asChild`.
namestring | undefined—The name of the field. Submitted with its owning form as part of a name/value pair.
requiredboolean | undefined—When `true`, indicates that the user must set the value before the owning form can be submitted.
classClassValue—根元素之自訂 class
checkedTextstring | undefined—開啟狀態顯示的文字(亦可用 `#checkedText` slot)
uncheckedTextstring | undefined—關閉狀態顯示的文字(亦可用 `#uncheckedText` slot)
isErrorboolean | undefined—設為 `true` 時套用錯誤狀態樣式(顯示 `data-is-error` 屬性供樣式 hook)

Events

名稱Payload說明
update:modelValueboolean—

Slots

名稱Bindings說明
checkedTextany自訂開啟狀態文字內容(取代 `checkedText` prop)
uncheckedTextany自訂關閉狀態文字內容(取代 `uncheckedText` prop)
thumb{ modelValue: boolean; }自訂 thumb 內容,bindings 含當前 `modelValue`

更新紀錄

  • fix: data-is-error 於 isError=false 時不再渲染屬性29644ac