mirror of
https://github.com/youzan/vant.git
synced 2026-05-05 01:00:55 +08:00
[improvement] rename packages dir to src (#3659)
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<template>
|
||||
<demo-section>
|
||||
<demo-block :title="$t('basicUsage')">
|
||||
<van-cell-group>
|
||||
<van-switch-cell
|
||||
v-model="checked"
|
||||
:title="$t('title')"
|
||||
/>
|
||||
</van-cell-group>
|
||||
</demo-block>
|
||||
|
||||
<demo-block :title="$t('disabled')">
|
||||
<van-cell-group>
|
||||
<van-switch-cell
|
||||
v-model="checked"
|
||||
disabled
|
||||
:title="$t('title')"
|
||||
/>
|
||||
</van-cell-group>
|
||||
</demo-block>
|
||||
|
||||
<demo-block :title="$t('loadingStatus')">
|
||||
<van-cell-group>
|
||||
<van-switch-cell
|
||||
v-model="checked"
|
||||
loading
|
||||
:title="$t('title')"
|
||||
/>
|
||||
</van-cell-group>
|
||||
</demo-block>
|
||||
</demo-section>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
checked: true
|
||||
};
|
||||
}
|
||||
};
|
||||
</script>
|
||||
@@ -0,0 +1,77 @@
|
||||
# SwitchCell
|
||||
|
||||
### Intro
|
||||
|
||||
`SwitchCell` component is an encapsulation of `Switch` and `Cell`.
|
||||
|
||||
### Install
|
||||
|
||||
``` javascript
|
||||
import { SwitchCell } from 'vant';
|
||||
|
||||
Vue.use(SwitchCell);
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```html
|
||||
<van-cell-group>
|
||||
<van-switch-cell v-model="checked" title="Title" />
|
||||
</van-cell-group>
|
||||
```
|
||||
|
||||
```javascript
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
checked: true
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Disabled
|
||||
|
||||
use `disabled` property to disable the component
|
||||
|
||||
```html
|
||||
<van-cell-group>
|
||||
<van-switch-cell v-model="checked" disabled title="Title" />
|
||||
</van-cell-group>
|
||||
```
|
||||
|
||||
### Loading
|
||||
|
||||
use `loading` property to keep component in loading state
|
||||
|
||||
```html
|
||||
<van-cell-group>
|
||||
<van-switch-cell v-model="checked" loading title="Title" />
|
||||
</van-cell-group>
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### Props
|
||||
|
||||
| Attribute | Description | Type | Default |
|
||||
|------|------|------|------|
|
||||
| v-model | on-off state of the switch | `any` | `false` |
|
||||
| title | the left side title | `String` | `''` |
|
||||
| border | whether to show cell border | `Boolean` | `true` |
|
||||
| cell-size | Cell size,can be set to `large` | `String` | - |
|
||||
| loading | whether switch is loading | `Boolean` | `false` |
|
||||
| disabled | whether to disable switch | `Boolean` | `false` |
|
||||
| size | Size of switch | `String` | `24px` |
|
||||
| active-color | Background of switch color when active | `String` | `#1989fa` |
|
||||
| inactive-color | Background of switch color when inactive | `String` | `#fff` |
|
||||
| active-value | Value when active | `any` | `true` |
|
||||
| inactive-value | Value when inactive | `any` | `false` |
|
||||
|
||||
### Events
|
||||
|
||||
| Event | Description | Arguments |
|
||||
|------|------|------|
|
||||
| change | triggered when the on-off state is changed | checked: switch is on or not |
|
||||
@@ -0,0 +1,15 @@
|
||||
@import '../style/var';
|
||||
|
||||
.van-switch-cell {
|
||||
padding-top: @switch-cell-padding-top;
|
||||
padding-bottom: @switch-cell-padding-bottom;
|
||||
|
||||
&--large {
|
||||
padding-top: @switch-cell-large-padding-top;
|
||||
padding-bottom: @switch-cell-large-padding-bottom;
|
||||
}
|
||||
|
||||
.van-switch {
|
||||
float: right;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { createNamespace } from '../utils';
|
||||
import { inherit } from '../utils/functional';
|
||||
import Cell from '../cell';
|
||||
import Switch, { SwitchEvents } from '../switch';
|
||||
import { switchProps, SharedSwitchProps } from '../switch/shared';
|
||||
|
||||
// Types
|
||||
import { CreateElement, RenderContext } from 'vue/types';
|
||||
import { DefaultSlots } from '../utils/types';
|
||||
|
||||
export type SwitchCellProps = SharedSwitchProps & {
|
||||
size: string;
|
||||
title?: string;
|
||||
border?: boolean;
|
||||
cellSize?: string;
|
||||
};
|
||||
|
||||
const [createComponent, bem] = createNamespace('switch-cell');
|
||||
|
||||
function SwitchCell(
|
||||
h: CreateElement,
|
||||
props: SwitchCellProps,
|
||||
slots: DefaultSlots,
|
||||
ctx: RenderContext<SwitchCellProps>
|
||||
) {
|
||||
return (
|
||||
<Cell
|
||||
center
|
||||
size={props.cellSize}
|
||||
title={props.title}
|
||||
border={props.border}
|
||||
class={bem([props.cellSize])}
|
||||
{...inherit(ctx)}
|
||||
>
|
||||
<Switch {...{ props, on: ctx.listeners }} />
|
||||
</Cell>
|
||||
);
|
||||
}
|
||||
|
||||
SwitchCell.props = {
|
||||
...switchProps,
|
||||
title: String,
|
||||
cellSize: String,
|
||||
border: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
size: {
|
||||
type: String,
|
||||
default: '24px'
|
||||
}
|
||||
};
|
||||
|
||||
export default createComponent<SwitchCellProps, SwitchEvents>(SwitchCell);
|
||||
@@ -0,0 +1,44 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`renders demo correctly 1`] = `
|
||||
<div>
|
||||
<div>
|
||||
<div class="van-cell-group van-hairline--top-bottom">
|
||||
<div class="van-cell van-cell--center van-switch-cell">
|
||||
<div class="van-cell__title"><span>标题</span></div>
|
||||
<div class="van-cell__value">
|
||||
<div role="switch" aria-checked="true" class="van-switch van-switch--on" style="font-size: 24px;">
|
||||
<div class="van-switch__node"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="van-cell-group van-hairline--top-bottom">
|
||||
<div class="van-cell van-cell--center van-switch-cell">
|
||||
<div class="van-cell__title"><span>标题</span></div>
|
||||
<div class="van-cell__value">
|
||||
<div role="switch" aria-checked="true" class="van-switch van-switch--on van-switch--disabled" style="font-size: 24px;">
|
||||
<div class="van-switch__node"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="van-cell-group van-hairline--top-bottom">
|
||||
<div class="van-cell van-cell--center van-switch-cell">
|
||||
<div class="van-cell__title"><span>标题</span></div>
|
||||
<div class="van-cell__value">
|
||||
<div role="switch" aria-checked="true" class="van-switch van-switch--on" style="font-size: 24px;">
|
||||
<div class="van-switch__node">
|
||||
<div class="van-loading van-loading--circular van-switch__loading"><span class="van-loading__spinner van-loading__spinner--circular" style="color: rgb(25, 137, 250);"><svg viewBox="25 25 50 50" class="van-loading__circular"><circle cx="50" cy="50" r="20" fill="none"></circle></svg></span></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
@@ -0,0 +1,21 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`border prop 1`] = `
|
||||
<div class="van-cell van-cell--center van-cell--borderless van-switch-cell">
|
||||
<div class="van-cell__value van-cell__value--alone">
|
||||
<div role="switch" aria-checked="false" class="van-switch" style="font-size: 24px;">
|
||||
<div class="van-switch__node"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
exports[`cell-size prop 1`] = `
|
||||
<div class="van-cell van-cell--center van-cell--large van-switch-cell van-switch-cell--large">
|
||||
<div class="van-cell__value van-cell__value--alone">
|
||||
<div role="switch" aria-checked="false" class="van-switch" style="font-size: 24px;">
|
||||
<div class="van-switch__node"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
@@ -0,0 +1,4 @@
|
||||
import Demo from '../demo';
|
||||
import demoTest from '../../../test/demo-test';
|
||||
|
||||
demoTest(Demo);
|
||||
@@ -0,0 +1,37 @@
|
||||
import SwitchCell from '..';
|
||||
import { mount } from '../../../test/utils';
|
||||
|
||||
test('change event', () => {
|
||||
const onChange = jest.fn();
|
||||
const wrapper = mount(SwitchCell, {
|
||||
context: {
|
||||
on: {
|
||||
change: onChange
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
wrapper.find('.van-switch').trigger('click');
|
||||
|
||||
expect(onChange).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
test('border prop', () => {
|
||||
const wrapper = mount(SwitchCell, {
|
||||
propsData: {
|
||||
border: false
|
||||
}
|
||||
});
|
||||
|
||||
expect(wrapper).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('cell-size prop', () => {
|
||||
const wrapper = mount(SwitchCell, {
|
||||
propsData: {
|
||||
cellSize: 'large'
|
||||
}
|
||||
});
|
||||
|
||||
expect(wrapper).toMatchSnapshot();
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
# SwitchCell 开关单元格
|
||||
|
||||
### 介绍
|
||||
|
||||
`SwitchCell`组件是对`Switch`和`Cell`组件的封装
|
||||
|
||||
### 引入
|
||||
|
||||
``` javascript
|
||||
import { SwitchCell } from 'vant';
|
||||
|
||||
Vue.use(SwitchCell);
|
||||
```
|
||||
|
||||
## 代码演示
|
||||
|
||||
### 基础用法
|
||||
|
||||
```html
|
||||
<van-cell-group>
|
||||
<van-switch-cell v-model="checked" title="标题" />
|
||||
</van-cell-group>
|
||||
```
|
||||
|
||||
```javascript
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
checked: true
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 禁用状态
|
||||
|
||||
通过`disabled`属性可以将组件设置为禁用状态
|
||||
|
||||
```html
|
||||
<van-cell-group>
|
||||
<van-switch-cell v-model="checked" disabled title="标题" />
|
||||
</van-cell-group>
|
||||
```
|
||||
|
||||
### 加载状态
|
||||
|
||||
通过`loading`属性可以将组件设置为加载状态
|
||||
|
||||
```html
|
||||
<van-cell-group>
|
||||
<van-switch-cell v-model="checked" loading title="标题" />
|
||||
</van-cell-group>
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### Props
|
||||
|
||||
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
||||
|------|------|------|------|------|
|
||||
| v-model | 开关状态 | `null` | `false` | - |
|
||||
| title | 左侧标题 | `String` | `''` | - |
|
||||
| border | 是否展示单元格内边框 | `Boolean` | `true` | 2.0.0 |
|
||||
| cell-size | 单元格大小,可选值为 `large` | `String` | 2.0.0 |
|
||||
| loading | 是否为加载状态 | `Boolean` | `false` | - |
|
||||
| disabled | 是否为禁用状态 | `Boolean` | `false` | - |
|
||||
| size | 开关尺寸 | `String` | `24px` | 1.1.11 |
|
||||
| active-color | 开关时的背景色 | `String` | `#1989fa` | 1.5.0 |
|
||||
| inactive-color | 开关时的背景色 | `String` | `#fff` | 1.5.0 |
|
||||
| active-value | 打开时的值 | `any` | `true` | 1.5.6 |
|
||||
| inactive-value | 关闭时的值 | `any` | `false` | 1.5.6 |
|
||||
|
||||
### Events
|
||||
|
||||
| 事件名 | 说明 | 回调参数 |
|
||||
|------|------|------|
|
||||
| change | 开关状态切换回调 | checked: 是否选中开关 |
|
||||
Reference in New Issue
Block a user