mirror of
https://github.com/youzan/vant.git
synced 2025-10-22 11:54:02 +00:00
[improvement] Rate: jsx (#2619)
This commit is contained in:
94
packages/rate/index.js
Normal file
94
packages/rate/index.js
Normal file
@@ -0,0 +1,94 @@
|
||||
/* eslint-disable prefer-spread */
|
||||
import { use } from '../utils';
|
||||
import Icon from '../icon';
|
||||
|
||||
const [sfc, bem] = use('rate');
|
||||
|
||||
export default sfc({
|
||||
props: {
|
||||
value: Number,
|
||||
readonly: Boolean,
|
||||
disabled: Boolean,
|
||||
size: {
|
||||
type: Number,
|
||||
default: 20
|
||||
},
|
||||
icon: {
|
||||
type: String,
|
||||
default: 'star'
|
||||
},
|
||||
voidIcon: {
|
||||
type: String,
|
||||
default: 'star-o'
|
||||
},
|
||||
color: {
|
||||
type: String,
|
||||
default: '#ffd21e'
|
||||
},
|
||||
voidColor: {
|
||||
type: String,
|
||||
default: '#c7c7c7'
|
||||
},
|
||||
disabledColor: {
|
||||
type: String,
|
||||
default: '#bdbdbd'
|
||||
},
|
||||
count: {
|
||||
type: Number,
|
||||
default: 5
|
||||
}
|
||||
},
|
||||
|
||||
computed: {
|
||||
list() {
|
||||
return Array.apply(null, { length: this.count }).map((value, index) => index < this.value);
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
onSelect(index) {
|
||||
if (!this.disabled && !this.readonly) {
|
||||
this.$emit('input', index + 1);
|
||||
this.$emit('change', index + 1);
|
||||
}
|
||||
},
|
||||
|
||||
onTouchMove(event) {
|
||||
if (!document.elementFromPoint) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
const { clientX, clientY } = event.touches[0];
|
||||
const target = document.elementFromPoint(clientX, clientY);
|
||||
if (target && target.dataset) {
|
||||
const { index } = target.dataset;
|
||||
|
||||
/* istanbul ignore else */
|
||||
if (index) {
|
||||
this.onSelect(+index);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
render(h) {
|
||||
return (
|
||||
<div class={bem()} onTouchmove={this.onTouchMove}>
|
||||
{this.list.map((full, index) => (
|
||||
<Icon
|
||||
key={index}
|
||||
class={bem('item')}
|
||||
size={`${this.size}px`}
|
||||
data-index={index}
|
||||
name={full ? this.icon : this.voidIcon}
|
||||
color={this.disabled ? this.disabledColor : full ? this.color : this.voidColor}
|
||||
onClick={() => {
|
||||
this.onSelect(index);
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user