mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-06 03:58:05 +08:00
parent
98ce810986
commit
59f4b7c970
@ -5,8 +5,9 @@ Page({
|
|||||||
value1: 3,
|
value1: 3,
|
||||||
value2: 3,
|
value2: 3,
|
||||||
value3: 3,
|
value3: 3,
|
||||||
value4: 4,
|
value4: 2.5,
|
||||||
value5: 3
|
value5: 4,
|
||||||
|
value6: 3
|
||||||
},
|
},
|
||||||
|
|
||||||
onChange(event) {
|
onChange(event) {
|
||||||
|
@ -29,11 +29,25 @@
|
|||||||
/>
|
/>
|
||||||
</demo-block>
|
</demo-block>
|
||||||
|
|
||||||
<demo-block title="自定义数量">
|
<demo-block title="半星">
|
||||||
<van-rate
|
<van-rate
|
||||||
custom-class="van-rate"
|
custom-class="van-rate"
|
||||||
data-key="value4"
|
data-key="value4"
|
||||||
value="{{ value4 }}"
|
value="{{ value4 }}"
|
||||||
|
size="{{ 25 }}"
|
||||||
|
allow-half
|
||||||
|
color="#f44"
|
||||||
|
void-color="#eee"
|
||||||
|
void-icon="star"
|
||||||
|
bind:change="onChange"
|
||||||
|
/>
|
||||||
|
</demo-block>
|
||||||
|
|
||||||
|
<demo-block title="自定义数量">
|
||||||
|
<van-rate
|
||||||
|
custom-class="van-rate"
|
||||||
|
data-key="value5"
|
||||||
|
value="{{ value5 }}"
|
||||||
count="{{ 6 }}"
|
count="{{ 6 }}"
|
||||||
bind:change="onChange"
|
bind:change="onChange"
|
||||||
/>
|
/>
|
||||||
@ -42,8 +56,8 @@
|
|||||||
<demo-block title="禁用状态">
|
<demo-block title="禁用状态">
|
||||||
<van-rate
|
<van-rate
|
||||||
custom-class="van-rate"
|
custom-class="van-rate"
|
||||||
data-key="value5"
|
data-key="value6"
|
||||||
value="{{ value5 }}"
|
value="{{ value6 }}"
|
||||||
disabled
|
disabled
|
||||||
/>
|
/>
|
||||||
</demo-block>
|
</demo-block>
|
||||||
@ -51,8 +65,8 @@
|
|||||||
<demo-block title="只读状态">
|
<demo-block title="只读状态">
|
||||||
<van-rate
|
<van-rate
|
||||||
custom-class="van-rate"
|
custom-class="van-rate"
|
||||||
data-key="value5"
|
data-key="value6"
|
||||||
value="{{ value5 }}"
|
value="{{ value6 }}"
|
||||||
readonly
|
readonly
|
||||||
/>
|
/>
|
||||||
</demo-block>
|
</demo-block>
|
||||||
|
@ -123,3 +123,6 @@
|
|||||||
|
|
||||||
// NavBar
|
// NavBar
|
||||||
@nav-bar-height: 44px;
|
@nav-bar-height: 44px;
|
||||||
|
|
||||||
|
// Rate
|
||||||
|
@rate-horizontal-padding: 2px;
|
||||||
|
@ -63,6 +63,19 @@ Page({
|
|||||||
/>
|
/>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### 半星
|
||||||
|
```html
|
||||||
|
<van-rate
|
||||||
|
value="{{ value }}"
|
||||||
|
size="{{ 25 }}"
|
||||||
|
allow-half
|
||||||
|
color="#f44"
|
||||||
|
void-color="#eee"
|
||||||
|
void-icon="star"
|
||||||
|
bind:change="onChange"
|
||||||
|
/>
|
||||||
|
```
|
||||||
|
|
||||||
#### 自定义数量
|
#### 自定义数量
|
||||||
|
|
||||||
```html
|
```html
|
||||||
@ -105,6 +118,7 @@ Page({
|
|||||||
| void-color | 未选中时的颜色 | `String` | `#c7c7c7` |
|
| void-color | 未选中时的颜色 | `String` | `#c7c7c7` |
|
||||||
| icon | 选中时的图标名称或图片链接,可选值见 Icon 组件 | `String` | `star` |
|
| icon | 选中时的图标名称或图片链接,可选值见 Icon 组件 | `String` | `star` |
|
||||||
| void-icon | 未选中时的图标名称或图片链接,可选值见 Icon 组件 | `String` | `star-o` |
|
| void-icon | 未选中时的图标名称或图片链接,可选值见 Icon 组件 | `String` | `star-o` |
|
||||||
|
| allow-half | 是否允许半选 | `Boolean` | `false` |
|
||||||
| readonly | 是否为只读状态 | `Boolean` | `false` |
|
| readonly | 是否为只读状态 | `Boolean` | `false` |
|
||||||
| disabled | 是否禁用评分 | `Boolean` | `false` |
|
| disabled | 是否禁用评分 | `Boolean` | `false` |
|
||||||
| disabled-color | 禁用时的颜色 | `String` | `#bdbdbd` |
|
| disabled-color | 禁用时的颜色 | `String` | `#bdbdbd` |
|
||||||
|
@ -1,9 +1,24 @@
|
|||||||
|
@import '../common/style/var.less';
|
||||||
|
|
||||||
.van-rate {
|
.van-rate {
|
||||||
user-select: none;
|
user-select: none;
|
||||||
|
|
||||||
&__item {
|
&__item {
|
||||||
width: 1em;
|
position: relative;
|
||||||
padding: 0 2px;
|
display: inline-block;
|
||||||
box-sizing: content-box;
|
padding: 0 @rate-horizontal-padding;
|
||||||
|
}
|
||||||
|
|
||||||
|
&__icon {
|
||||||
|
display: block;
|
||||||
|
height: 1em;
|
||||||
|
|
||||||
|
&--half {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: @rate-horizontal-padding;
|
||||||
|
width: 0.5em;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,10 @@ VantComponent({
|
|||||||
classes: ['icon-class'],
|
classes: ['icon-class'],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
|
value: Number,
|
||||||
readonly: Boolean,
|
readonly: Boolean,
|
||||||
disabled: Boolean,
|
disabled: Boolean,
|
||||||
|
allowHalf: Boolean,
|
||||||
size: {
|
size: {
|
||||||
type: Number,
|
type: Number,
|
||||||
value: 20
|
value: 20
|
||||||
@ -35,10 +37,6 @@ VantComponent({
|
|||||||
count: {
|
count: {
|
||||||
type: Number,
|
type: Number,
|
||||||
value: 5
|
value: 5
|
||||||
},
|
|
||||||
value: {
|
|
||||||
type: Number,
|
|
||||||
value: 0
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -47,49 +45,46 @@ VantComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
watch: {
|
watch: {
|
||||||
value(value) {
|
value(value: number) {
|
||||||
if (value !== this.data.innerValue) {
|
if (value !== this.data.innerValue) {
|
||||||
this.set({ innerValue: value });
|
this.set({ innerValue: value });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
computed: {
|
|
||||||
list() {
|
|
||||||
const { count, innerValue } = this.data;
|
|
||||||
return Array.from({ length: count }, (_, index) => index < innerValue);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
onSelect(event: Weapp.Event) {
|
onSelect(event: Weapp.Event) {
|
||||||
const { data } = this;
|
const { data } = this;
|
||||||
const { index } = event.currentTarget.dataset;
|
const { score } = event.currentTarget.dataset;
|
||||||
if (!data.disabled && !data.readonly) {
|
if (!data.disabled && !data.readonly) {
|
||||||
this.set({ innerValue: index + 1 });
|
this.set({ innerValue: score + 1 });
|
||||||
this.$emit('input', index + 1);
|
this.$emit('input', score + 1);
|
||||||
this.$emit('change', index + 1);
|
this.$emit('change', score + 1);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
onTouchMove(event: Weapp.TouchEvent) {
|
onTouchMove(event: Weapp.TouchEvent) {
|
||||||
const { clientX, clientY } = event.touches[0];
|
const { clientX, clientY } = event.touches[0];
|
||||||
|
|
||||||
this.getRect('.van-rate__item', true).then(list => {
|
this.getRect('.van-rate__icon', true).then(
|
||||||
const target = list.find(
|
(list: wx.BoundingClientRectCallbackResult[]) => {
|
||||||
item =>
|
const target = list
|
||||||
clientX >= item.left &&
|
.sort(item => item.right - item.left)
|
||||||
clientX <= item.right &&
|
.find(
|
||||||
clientY >= item.top &&
|
item =>
|
||||||
clientY <= item.bottom
|
clientX >= item.left &&
|
||||||
);
|
clientX <= item.right &&
|
||||||
if (target != null) {
|
clientY >= item.top &&
|
||||||
this.onSelect({
|
clientY <= item.bottom
|
||||||
...event,
|
);
|
||||||
currentTarget: target
|
if (target != null) {
|
||||||
});
|
this.onSelect({
|
||||||
|
...event,
|
||||||
|
currentTarget: target
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,16 +1,32 @@
|
|||||||
|
<wxs src="../wxs/utils.wxs" module="utils" />
|
||||||
|
|
||||||
<view
|
<view
|
||||||
class="van-rate custom-class"
|
class="van-rate custom-class"
|
||||||
bind:touchmove="onTouchMove"
|
bind:touchmove="onTouchMove"
|
||||||
>
|
>
|
||||||
<van-icon
|
<view
|
||||||
wx:for="{{ list }}"
|
|
||||||
wx:key="index"
|
|
||||||
class="van-rate__item"
|
class="van-rate__item"
|
||||||
custom-class="icon-class"
|
wx:for="{{ count }}"
|
||||||
size="{{ size }}px"
|
wx:key="index"
|
||||||
data-index="{{ index }}"
|
style="font-size: {{ size}}px;"
|
||||||
name="{{ item ? icon : voidIcon }}"
|
>
|
||||||
color="{{ disabled ? disabledColor : item ? color : voidColor }}"
|
<van-icon
|
||||||
bind:click="onSelect"
|
name="{{ index + 1 <= innerValue ? icon : voidIcon }}"
|
||||||
/>
|
class="van-rate__icon"
|
||||||
|
custom-class="icon-class"
|
||||||
|
data-score="{{ index }}"
|
||||||
|
color="{{ disabled ? disabledColor : index + 1 <= innerValue ? color : voidColor }}"
|
||||||
|
bind:click="onSelect"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<van-icon
|
||||||
|
wx:if="{{ allowHalf }}"
|
||||||
|
name="{{ index + 0.5 <= innerValue ? icon : voidIcon }}"
|
||||||
|
class="{{ utils.bem('rate__icon', ['half']) }}"
|
||||||
|
custom-class="icon-class"
|
||||||
|
data-score="{{ index - 0.5 }}"
|
||||||
|
color="{{ disabled ? disabledColor : index + 0.5 <= innerValue ? color : voidColor }}"
|
||||||
|
bind:click="onSelect"
|
||||||
|
/>
|
||||||
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user