[improvement] rename packages dir to src (#3659)

This commit is contained in:
neverland
2019-06-27 11:25:57 +08:00
committed by GitHub
parent 8489918dca
commit 75c79b7044
619 changed files with 21 additions and 21 deletions
+138
View File
@@ -0,0 +1,138 @@
<template>
<demo-section>
<demo-block :title="$t('title1')">
<van-slider
v-model="value1"
@change="onChange"
/>
</demo-block>
<demo-block :title="$t('title2')">
<van-slider
v-model="value2"
:min="-50"
:max="50"
@change="onChange"
/>
</demo-block>
<demo-block :title="$t('title3')">
<van-slider
v-model="value3"
disabled
/>
</demo-block>
<demo-block :title="$t('title4')">
<van-slider
v-model="value4"
:step="10"
@change="onChange"
/>
</demo-block>
<demo-block :title="$t('customStyle')">
<van-slider
v-model="value5"
bar-height="4px"
active-color="#f44"
@change="onChange"
/>
</demo-block>
<demo-block :title="$t('customButton')">
<van-slider
v-model="value6"
active-color="#f44"
>
<template #button>
<div class="custom-button">{{ value6 }}</div>
</template>
</van-slider>
</demo-block>
<demo-block
v-if="!$attrs.weapp"
:title="$t('vertical')"
>
<div :style="{ height: '120px', paddingLeft: '30px' }">
<van-slider
v-model="value7"
vertical
@change="onChange"
/>
</div>
</demo-block>
</demo-section>
</template>
<script>
export default {
i18n: {
'zh-CN': {
title1: '基本用法',
title2: '指定选择范围',
title3: '禁用',
title4: '指定步长',
customStyle: '自定义样式',
customButton: '自定义按钮',
text: '当前值:',
vertical: '垂直方向'
},
'en-US': {
title1: 'Basic Usage',
title2: 'Range',
title3: 'Disabled',
title4: 'Step size',
customStyle: 'Custom Style',
customButton: 'Custom Button',
text: 'Current value: ',
vertical: 'Vertical'
}
},
data() {
return {
value1: 50,
value2: 0,
value3: 50,
value4: 50,
value5: 50,
value6: 50,
value7: 50
};
},
methods: {
onChange(value) {
this.$toast(this.$t('text') + value);
}
}
};
</script>
<style lang="less">
@import '../../style/var';
.demo-slider {
user-select: none;
.van-slider {
margin: 0 15px 30px;
}
.custom-button {
width: 26px;
color: #fff;
font-size: 10px;
line-height: 18px;
text-align: center;
background-color: @red;
border-radius: 100px;
}
.van-doc-demo-block__title {
padding-top: 25px;
}
}
</style>
+115
View File
@@ -0,0 +1,115 @@
# Slider
### Install
``` javascript
import { Slider } from 'vant';
Vue.use(Slider);
```
## Usage
### Basic Usage
```html
<van-slider v-model="value" @change="onChange" />
```
```js
export default {
data() {
return {
value: 50
};
},
methods: {
onChange(value) {
this.$toast('Current value' + value);
}
}
};
```
### Range
```html
<van-slider v-model="value" :min="-50" :max="50" />
```
### Disabled
```html
<van-slider v-model="value" disabled />
```
### Step size
```html
<van-slider v-model="value" :step="10" />
```
### Custom style
```html
<van-slider
v-model="value"
bar-height="4px"
active-color="#f44"
/>
```
### Custom button
```html
<van-slider
v-model="value"
active-color="#f44"
>
<div
slot="button"
class="custom-button"
>
{{ value }}
</div>
</van-slider>
```
### Vertical
```html
<div :style="{ height: '100px' }">
<van-slider v-model="value" vertical />
</div>
```
## API
### Props
| Attribute | Description | Type | Default |
|------|------|------|------|------|
| value | Current value | `Number` | `0` |
| disabled | Whether to disable slider | `Boolean` | `false` |
| max | Max value | `Number` | `100` |
| min | Min value | `Number` | `0` |
| step | Step size | `Number` | `1` |
| bar-height | Height of bar | `String` | `2px` |
| active-color | Active color of bar | `String` | `#1989fa` |
| inactive-color | Inactive color of bar | `String` | `#e5e5e5` |
| vertical | Whether to display vertical | `Boolean` | `false` |
### Events
| Event | Description | Arguments |
|------|------|------|
| change | Triggered after value change | value: current rate |
| drag-start | Triggered when start drag | - |
| drag-end | Triggered when end drag | - |
### Slots
| Name | Description |
|------|------|
| button | Custom button |
+153
View File
@@ -0,0 +1,153 @@
import { createNamespace } from '../utils';
import { TouchMixin } from '../mixins/touch';
import { preventDefault } from '../utils/dom/event';
const [createComponent, bem] = createNamespace('slider');
export default createComponent({
mixins: [TouchMixin],
props: {
min: Number,
value: Number,
disabled: Boolean,
vertical: Boolean,
activeColor: String,
inactiveColor: String,
max: {
type: Number,
default: 100
},
step: {
type: Number,
default: 1
},
barHeight: {
type: String,
default: '2px'
}
},
computed: {
range() {
return this.max - this.min;
}
},
methods: {
onTouchStart(event) {
if (this.disabled) {
return;
}
this.touchStart(event);
this.startValue = this.format(this.value);
this.dragStatus = 'start';
},
onTouchMove(event) {
if (this.disabled) {
return;
}
if (this.dragStatus === 'start') {
this.$emit('drag-start');
}
preventDefault(event, true);
this.touchMove(event);
this.dragStatus = 'draging';
const rect = this.$el.getBoundingClientRect();
const delta = this.vertical ? this.deltaY : this.deltaX;
const total = this.vertical ? rect.height : rect.width;
const diff = (delta / total) * this.range;
this.newValue = this.startValue + diff;
this.updateValue(this.newValue);
},
onTouchEnd() {
if (this.disabled) {
return;
}
if (this.dragStatus === 'draging') {
this.updateValue(this.newValue, true);
this.$emit('drag-end');
}
this.dragStatus = '';
},
onClick(event) {
event.stopPropagation();
if (this.disabled) return;
const rect = this.$el.getBoundingClientRect();
const delta = this.vertical ? event.clientY - rect.top : event.clientX - rect.left;
const total = this.vertical ? rect.height : rect.width;
const value = (delta / total) * this.range + this.min;
this.updateValue(value, true);
},
updateValue(value, end) {
value = this.format(value);
this.$emit('input', value);
if (end) {
this.$emit('change', value);
}
},
format(value) {
return (
Math.round(Math.max(this.min, Math.min(value, this.max)) / this.step) * this.step
);
}
},
render(h) {
const { vertical } = this;
const style = {
background: this.inactiveColor
};
const mainAxis = vertical ? 'height' : 'width';
const crossAxis = vertical ? 'width' : 'height';
const barStyle = {
[mainAxis]: `${((this.value - this.min) * 100) / this.range}%`,
[crossAxis]: this.barHeight,
background: this.activeColor
};
return (
<div
style={style}
class={bem({ disabled: this.disabled, vertical })}
onClick={this.onClick}
>
<div class={bem('bar')} style={barStyle}>
<div
role="slider"
tabindex={this.disabled ? -1 : 0}
aria-valuemin={this.min}
aria-valuenow={this.value}
aria-valuemax={this.max}
aria-orientation={this.vertical ? 'vertical' : 'horizontal'}
class={bem('button-wrapper')}
onTouchstart={this.onTouchStart}
onTouchmove={this.onTouchMove}
onTouchend={this.onTouchEnd}
onTouchcancel={this.onTouchEnd}
>
{this.slots('button') || <div class={bem('button')} />}
</div>
</div>
</div>
);
}
});
+53
View File
@@ -0,0 +1,53 @@
@import '../style/var';
.van-slider {
position: relative;
background-color: @slider-inactive-background-color;
border-radius: 999px;
&__bar {
position: relative;
background-color: @slider-active-background-color;
border-radius: inherit;
}
&__button {
width: @slider-button-width;
height: @slider-button-height;
background-color: @slider-button-background-color;
border-radius: @slider-button-border-radius;
box-shadow: @slider-button-box-shadow;
&-wrapper {
position: absolute;
top: 50%;
right: 0;
transform: translate3d(50%, -50%, 0);
/* use pseudo element to expand touch area */
&::after {
position: absolute;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
content: '';
}
}
}
&--disabled {
opacity: @slider-disabled-opacity;
}
&--vertical {
display: inline-block;
height: 100%;
.van-slider__button-wrapper {
top: auto;
bottom: 0;
transform: translate3d(50%, 50%, 0);
}
}
}
@@ -0,0 +1,71 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`renders demo correctly 1`] = `
<div>
<div>
<div class="van-slider">
<div class="van-slider__bar" style="width: 50%; height: 2px;">
<div role="slider" tabindex="0" aria-valuemin="0" aria-valuenow="50" aria-valuemax="100" aria-orientation="horizontal" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
</div>
<div>
<div class="van-slider">
<div class="van-slider__bar" style="width: 50%; height: 2px;">
<div role="slider" tabindex="0" aria-valuemin="-50" aria-valuenow="0" aria-valuemax="50" aria-orientation="horizontal" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
</div>
<div>
<div class="van-slider van-slider--disabled">
<div class="van-slider__bar" style="width: 50%; height: 2px;">
<div role="slider" tabindex="-1" aria-valuemin="0" aria-valuenow="50" aria-valuemax="100" aria-orientation="horizontal" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
</div>
<div>
<div class="van-slider">
<div class="van-slider__bar" style="width: 50%; height: 2px;">
<div role="slider" tabindex="0" aria-valuemin="0" aria-valuenow="50" aria-valuemax="100" aria-orientation="horizontal" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
</div>
<div>
<div class="van-slider">
<div class="van-slider__bar" style="width: 50%; height: 4px; background: rgb(255, 68, 68);">
<div role="slider" tabindex="0" aria-valuemin="0" aria-valuenow="50" aria-valuemax="100" aria-orientation="horizontal" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
</div>
<div>
<div class="van-slider">
<div class="van-slider__bar" style="width: 50%; height: 2px; background: rgb(255, 68, 68);">
<div role="slider" tabindex="0" aria-valuemin="0" aria-valuenow="50" aria-valuemax="100" aria-orientation="horizontal" class="van-slider__button-wrapper">
<div class="custom-button">50</div>
</div>
</div>
</div>
</div>
<div>
<div style="height: 120px; padding-left: 30px;">
<div class="van-slider van-slider--vertical">
<div class="van-slider__bar" style="height: 50%; width: 2px;">
<div role="slider" tabindex="0" aria-valuemin="0" aria-valuenow="50" aria-valuemax="100" aria-orientation="vertical" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
</div>
</div>
</div>
`;
@@ -0,0 +1,61 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`click bar 1`] = `
<div class="van-slider van-slider--disabled">
<div class="van-slider__bar" style="width: 50%; height: 2px;">
<div role="slider" tabindex="-1" aria-valuemin="0" aria-valuenow="50" aria-valuemax="100" aria-orientation="horizontal" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
`;
exports[`click bar 2`] = `
<div class="van-slider">
<div class="van-slider__bar" style="width: 100%; height: 2px;">
<div role="slider" tabindex="0" aria-valuemin="0" aria-valuenow="100" aria-valuemax="100" aria-orientation="horizontal" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
`;
exports[`click vertical 1`] = `
<div class="van-slider van-slider--vertical">
<div class="van-slider__bar" style="height: 100%; width: 2px;">
<div role="slider" tabindex="0" aria-valuemin="0" aria-valuenow="100" aria-valuemax="100" aria-orientation="vertical" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
`;
exports[`drag button 1`] = `
<div class="van-slider van-slider--disabled">
<div class="van-slider__bar" style="width: 50%; height: 2px;">
<div role="slider" tabindex="-1" aria-valuemin="0" aria-valuenow="50" aria-valuemax="100" aria-orientation="horizontal" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
`;
exports[`drag button 2`] = `
<div class="van-slider">
<div class="van-slider__bar" style="width: 100%; height: 2px;">
<div role="slider" tabindex="0" aria-valuemin="0" aria-valuenow="100" aria-valuemax="100" aria-orientation="horizontal" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
`;
exports[`drag button vertical 1`] = `
<div class="van-slider van-slider--vertical">
<div class="van-slider__bar" style="height: 100%; width: 2px;">
<div role="slider" tabindex="0" aria-valuemin="0" aria-valuenow="100" aria-valuemax="100" aria-orientation="vertical" class="van-slider__button-wrapper">
<div class="van-slider__button"></div>
</div>
</div>
</div>
`;
+4
View File
@@ -0,0 +1,4 @@
import Demo from '../demo';
import demoTest from '../../../test/demo-test';
demoTest(Demo);
+107
View File
@@ -0,0 +1,107 @@
import Slider from '..';
import { mount, trigger, triggerDrag, mockGetBoundingClientRect } from '../../../test/utils';
function mockRect(vertical) {
return mockGetBoundingClientRect({
width: vertical ? 0 : 100,
height: vertical ? 100 : 0,
top: vertical ? 0 : 100,
left: vertical ? 100 : 0
});
}
test('drag button', () => {
const restoreMock = mockRect();
const wrapper = mount(Slider, {
propsData: {
value: 50,
disabled: true
}
});
wrapper.vm.$on('input', value => {
wrapper.setProps({ value });
});
const button = wrapper.find('.van-slider__button');
triggerDrag(button, 50, 0);
expect(wrapper).toMatchSnapshot();
expect(wrapper.emitted('drag-start')).toBeFalsy();
expect(wrapper.emitted('drag-end')).toBeFalsy();
wrapper.setData({ disabled: false });
trigger(button, 'touchstart', 0, 0);
trigger(button, 'touchend', 0, 0);
triggerDrag(button, 50, 0);
expect(wrapper).toMatchSnapshot();
expect(wrapper.emitted('drag-start')).toBeTruthy();
expect(wrapper.emitted('drag-end')).toBeTruthy();
restoreMock();
});
it('click bar', () => {
const restoreMock = mockRect();
const wrapper = mount(Slider, {
propsData: {
value: 50,
disabled: true
}
});
wrapper.vm.$on('input', value => {
wrapper.setProps({ value });
});
trigger(wrapper, 'click', 100, 0);
expect(wrapper).toMatchSnapshot();
wrapper.setData({ disabled: false });
trigger(wrapper, 'click', 100, 0);
expect(wrapper).toMatchSnapshot();
restoreMock();
});
test('drag button vertical', () => {
const restoreMock = mockRect(true);
const wrapper = mount(Slider, {
propsData: {
value: 50,
vertical: true
}
});
wrapper.vm.$on('input', value => {
wrapper.setProps({ value });
});
const button = wrapper.find('.van-slider__button');
triggerDrag(button, 0, 50);
expect(wrapper).toMatchSnapshot();
restoreMock();
});
it('click vertical', () => {
const restoreMock = mockRect(true);
const wrapper = mount(Slider, {
propsData: {
value: 50,
vertical: true
}
});
wrapper.vm.$on('input', value => {
wrapper.setProps({ value });
});
trigger(wrapper, 'click', 0, 100);
expect(wrapper).toMatchSnapshot();
restoreMock();
});
+117
View File
@@ -0,0 +1,117 @@
# Slider 滑块
### 引入
``` javascript
import { Slider } from 'vant';
Vue.use(Slider);
```
## 代码演示
### 基本用法
```html
<van-slider v-model="value" @change="onChange" />
```
```js
export default {
data() {
return {
value: 50
};
},
methods: {
onChange(value) {
this.$toast('当前值:' + value);
}
}
};
```
### 指定选择范围
```html
<van-slider v-model="value" :min="-50" :max="50" />
```
### 禁用
```html
<van-slider v-model="value" disabled />
```
### 指定步长
```html
<van-slider v-model="value" :step="10" />
```
### 自定义样式
```html
<van-slider
v-model="value"
bar-height="4px"
active-color="#f44"
/>
```
### 自定义按钮
```html
<van-slider
v-model="value"
active-color="#f44"
>
<div
slot="button"
class="custom-button"
>
{{ value }}
</div>
</van-slider>
```
### 垂直方向
Slider 垂直展示时,高度为 100% 父元素高度
```html
<div :style="{ height: '100px' }">
<van-slider v-model="value" vertical />
</div>
```
## API
### Props
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|------|------|------|------|------|
| value | 当前进度百分比 | `Number` | `0` | 1.1.0 |
| disabled | 是否禁用滑块 | `Boolean` | `false` | 1.1.0 |
| max | 最大值 | `Number` | `100` | 1.1.0 |
| min | 最小值 | `Number` | `0` | 1.1.0 |
| step | 步长 | `Number` | `1` | 1.1.0 |
| bar-height | 进度条高度 | `String` | `2px` | 1.1.0 |
| active-color | 进度条激活态颜色 | `String` | `#1989fa` | 1.5.1 |
| inactive-color | 进度条默认颜色 | `String` | `#e5e5e5` | 1.5.1 |
| vertical | 是否垂直展示 | `Boolean` | `false` | 1.6.13 |
### Events
| 事件名 | 说明 | 回调参数 |
|------|------|------|
| change | 进度值改变后触发 | value: 当前进度 |
| drag-start | 开始拖动时触发 | - |
| drag-end | 结束拖动时触发 | - |
### Slots
| 名称 | 说明 |
|------|------|
| button | 自定义滑动按钮 |