feat: initialize Vue component library and aligned documentation
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
---
|
||||
title: Switch 开关
|
||||
description: 用于布尔状态的开关组件。
|
||||
component: switch
|
||||
group: 组件
|
||||
---
|
||||
|
||||
## 用法
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
Switch,
|
||||
SwitchContent,
|
||||
SwitchControl,
|
||||
SwitchThumb,
|
||||
Description,
|
||||
Label,
|
||||
SwitchIcon,
|
||||
SwitchGroup,
|
||||
FieldError,
|
||||
} from "@heroui-vue/components";
|
||||
</script>
|
||||
```
|
||||
|
||||
<ComponentPreview name="switch-basic" />
|
||||
## 组件结构
|
||||
|
||||
```vue
|
||||
<Switch>
|
||||
<SwitchContent>
|
||||
<SwitchControl><SwitchThumb /></SwitchControl>
|
||||
启用通知
|
||||
</SwitchContent>
|
||||
<Description>允许接收通知</Description>
|
||||
</Switch>
|
||||
```
|
||||
|
||||
请把 SwitchControl 与标签文本放入 SwitchContent;Description 作为其兄弟节点。Vue 底座使用原生 button 承载 role="switch",不是 React Aria 的 label 结构。
|
||||
|
||||
## 示例
|
||||
|
||||
### 尺寸
|
||||
|
||||
<ComponentPreview name="switch-sizes" />
|
||||
|
||||
### 带图标
|
||||
|
||||
<ComponentPreview name="switch-with-icons" />
|
||||
|
||||
### 禁用
|
||||
|
||||
<ComponentPreview name="switch-disabled" />
|
||||
|
||||
### 无标签
|
||||
|
||||
<ComponentPreview name="switch-without-label" />
|
||||
|
||||
### 带描述
|
||||
|
||||
<ComponentPreview name="switch-with-description" />
|
||||
|
||||
### 默认选中
|
||||
|
||||
<ComponentPreview name="switch-default-selected" />
|
||||
|
||||
### 受控组件
|
||||
|
||||
<ComponentPreview name="switch-controlled" />
|
||||
|
||||
### 标签位置
|
||||
|
||||
<ComponentPreview name="switch-label-position" />
|
||||
|
||||
### 分组
|
||||
|
||||
<ComponentPreview name="switch-group" />
|
||||
|
||||
### 横向分组
|
||||
|
||||
<ComponentPreview name="switch-group-horizontal" />
|
||||
|
||||
### 表单集成
|
||||
|
||||
<ComponentPreview name="switch-form" />
|
||||
|
||||
### 渲染属性
|
||||
|
||||
Vue 使用作用域插槽读取选择状态。
|
||||
|
||||
<ComponentPreview name="switch-render-props" />
|
||||
|
||||
### 渲染函数
|
||||
|
||||
Vue 使用 `asChild` 合并根容器属性,默认插槽必须只有一个自定义根元素;内部仍需放置 SwitchContent。
|
||||
|
||||
<ComponentPreview name="switch-render-function" />
|
||||
|
||||
## 自定义样式
|
||||
|
||||
### Tailwind CSS
|
||||
|
||||
<ComponentPreview name="switch-custom-styles" />
|
||||
|
||||
### 全局 CSS
|
||||
|
||||
```css
|
||||
@layer components {
|
||||
.hv-switch__control {
|
||||
border-radius: 12px;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 样式参考
|
||||
|
||||
保留字段根容器、Content、Control、Thumb、Icon 的组合结构和 data-slot;类名前缀为 `hv-`。选中状态使用 `data-selected="true"`,禁用使用 `data-disabled="true"`。尺寸与滑块比例移植自基线 CSS。
|
||||
|
||||
## API 参考
|
||||
|
||||
### Switch
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| -------------------- | --------------------------------------------------------------------- | ---------- | -------------------------------- |
|
||||
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Switch 尺寸 |
|
||||
| `checked` | `boolean` | — | Vue 受控值,使用 v-model:checked |
|
||||
| `isSelected` | `boolean` | — | 上游同名受控属性,优先于 checked |
|
||||
| `defaultSelected` | `boolean` | `false` | 非受控初始值 |
|
||||
| `isDisabled` | `boolean` | `false` | 是否禁用 |
|
||||
| `isReadOnly` | `boolean` | `false` | 是否只读 |
|
||||
| `isInvalid` | `boolean` | `false` | ARIA 无效状态 |
|
||||
| `isRequired` | `boolean` | `false` | 是否必须开启 |
|
||||
| `name` | `string` | — | 表单字段名称 |
|
||||
| `value` | `string` | — | 表单提交值 |
|
||||
| `asChild` | `boolean` | `false` | 合并根属性到唯一自定义容器 |
|
||||
| `validate` | `(value: boolean) => string \| string[] \| true \| null \| undefined` | — | 自定义校验 |
|
||||
| `validationBehavior` | `'native' \| 'aria'` | `'native'` | 校验与错误展示方式 |
|
||||
|
||||
### SwitchContent / SwitchControl / SwitchThumb / SwitchIcon
|
||||
|
||||
分别对应上游 Content、Control、Thumb、Icon。Content 提供可点击的开关语义,其余为可组合样式部件。空默认插槽会自动渲染轨道与滑块;使用自定义插槽时由调用者组合部件。
|
||||
|
||||
### 事件与插槽
|
||||
|
||||
`update:checked`、`update:isSelected` 与 `change` 都传递 boolean。根作用域插槽提供 isSelected、isDisabled、isReadOnly、isInvalid、isRequired。根 attrs 的 class/style/data-* 应用到字段容器,ARIA 与 DOM 事件透传到 Content 按钮。
|
||||
|
||||
### 表单与校验
|
||||
|
||||
`validate(value)` 返回错误字符串、字符串数组,或 `true` / `null` / `undefined` 表示通过。`validationBehavior="native"`(默认)通过原生 checkbox 的约束阻止无效表单提交,校验失败后展示错误并将焦点交给开关;`"aria"` 立即设置 ARIA 错误状态,提交由调用者处理。
|
||||
|
||||
```vue
|
||||
<Switch
|
||||
name="consent"
|
||||
is-required
|
||||
:validate="(value) => (value ? true : '请先开启此选项。')"
|
||||
>
|
||||
<SwitchContent><SwitchControl><SwitchThumb /></SwitchControl>同意接收通知</SwitchContent>
|
||||
<Description>可随时在设置中关闭。</Description>
|
||||
<FieldError />
|
||||
</Switch>
|
||||
```
|
||||
|
||||
Description 与 FieldError 自动生成唯一 ID,并关联到 Content 的 aria-describedby。表单 reset 恢复非受控初值;受控值由父组件管理。
|
||||
|
||||
### SwitchGroup
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------- | ---------------------------- | ------------ | ---------- |
|
||||
| `orientation` | `'horizontal' \| 'vertical'` | `'vertical'` | 组布局方向 |
|
||||
|
||||
SwitchGroup 是布局容器,每个 Switch 独立维护值;请提供 aria-label 或 aria-labelledby 标注分组。
|
||||
|
||||
### 当前差异
|
||||
|
||||
`disabled` / `required` / `defaultChecked` 作为兼容别名保留。`change` 事件传递 boolean;React Aria 的 PressEvent 仍不等价于 DOM click。根组件的 class/style/data-* 作用于字段容器,ARIA 与 DOM 事件作用于 Content 按钮;Content 上的显式属性可覆盖默认透传。`asChild` 替代 React render prop。FieldError 当前仅接入 Switch 字段,尚未提供完整 TextField / Form 体系。
|
||||
|
||||
## 相关组件
|
||||
|
||||
[Button 按钮](./button) · [Input 输入框](./input)
|
||||
Reference in New Issue
Block a user