Skip to content

Feature/rate component #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/rate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Rate from './rate.vue';

export default Rate;
181 changes: 181 additions & 0 deletions src/components/rate/rate.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<template>
<div
class="cd-rate"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要写死cd这个前缀

>
<span
v-for="item in max"
@mousemove="setHoverIndex(item, $event)"
@mouseleave="clearHoverIndex()"
@click="setCurrentValue(item, $event)"
:style="{ cursor: rateDisabled ? 'auto' : 'pointer' }"
>
<i class="cd-icon"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用我们的icon组件

:class="iconType[item - 1]"
:style="iconStyle"
></i>
</span>
</div>
</template>

<script>
export default {
data () {
return {
ifIconHalf: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成isIconHalf

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成isIconHalf

ifHoverChange: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

pointerAtLeftHalf: false,
iconStates: [],
hoverStates: [],
hoverIndex: 0,
currentValue: this.value,
};
},

created () {
this.iconStates = this.updateItemStates(this.currentValue);
},

props: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

放到最上面,让大家一眼就看到需要传哪些props

value: {
type: Number,
default: 0
},

allowHalf: {
type: Boolean,
default: false
},

lowThreshold: {
type: Number,
default: 2
},

highThreshold: {
type: Number,
default: 4
},

max: {
type: Number,
default: 5
},

rateDisabled: {
type: Boolean,
default: false
},

colors: {
type: Array,
default() {
return ['#F7BA2A', '#F7BA2A', '#F7BA2A']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

默认色值应该用的是我们自己的配色,http://design.codeages.com/#/component/rate

}
},

classNames: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成iconClasses,

type: Array,
default() {
return ['cd-icon-star-o', 'cd-icon-star-half', 'cd-icon-star']
}
}
},

computed: {
iconStyle() {
return {
color: this.activeColor
};
},

iconType() {
if (this.ifHoverChange) {
return this.hoverStates;
} else {
return this.iconStates;
}
},

activeColor() {
return this.getValueFromMap(this.hoverIndex, this.colorMap);
},

colorMap() {
return {
lowColor: this.colors[0],
mediumColor: this.colors[1],
highColor: this.colors[2],
}
},
},

watch: {
currentValue() {
this.ifHoverChange = false;
},

hoverIndex() {
this.ifHoverChange = true;
}
},

methods: {
judgeHalf(e) {
return e.offsetX * 2 <= e.target.clientWidth;
},

updateItemStates(value) {
let starNum = Math.floor(value);
let iconStates = [];
let oNum = Math.floor(this.max - value);
for (let i = 1; i <= starNum; i++) {
iconStates.push(this.classNames[2]);
}
if (value - starNum) {
iconStates.push(this.classNames[1]);
}
for (let i = 1; i <= oNum; i++) {
iconStates.push(this.classNames[0]);
}
return iconStates;
},

setHoverIndex(item, e) {
if (this.allowHalf && this.judgeHalf(e)) {
item = item - 0.5;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.5 应该是一个常量

this.ifIconHalf = true;
}
this.hoverIndex = item;
this.hoverStates = this.updateItemStates(item);
},

clearHoverIndex() {
this.ifHoverChange = false;
},

setCurrentValue(item, e) {
if (this.allowHalf && this.judgeHalf(e)) {
item = item - 0.5;
this.ifIconHalf = true;
}
this.currentValue = item;
this.$emit('input', this.currentValue);
this.iconStates = this.updateItemStates(item);
console.log(this.currentValue, 'this.currentValue');
},

getValueFromMap(value, map) {
let result = '';
if (value <= this.lowThreshold) {
result = map.lowColor;
} else if (value >= this.highThreshold) {
result = map.highColor;
} else {
result = map.mediumColor;
}
return result;
}
}
}

</script>
8 changes: 8 additions & 0 deletions src/styles/components/rate.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.cd-icon {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同样不要写死cd这个前缀

display: inline-block;
font-size: 18px;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要写死,放到variables.less里

transition: 0.3s;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个有统一的方法的

&:hover {
transform: scale(1.2);
}
}
1 change: 1 addition & 0 deletions src/styles/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@import './components/row.less';
@import './components/alert.less';
@import './components/panel.less';
@import './components/rate.less';
@import './components/message.less';
@import './components/sidebar.less';
@import './components/v-menu.less';
Expand Down