-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
145 lines (128 loc) · 3.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React, { Component } from 'react';
import PropTypes from 'prop-types';
const Icon = ({ toggled, toggledClassName, untoggledClassName, onMouseEnter, onClickRating }) => {
const iStyle = {
cursor: 'pointer',
};
const className = toggled ? toggledClassName : untoggledClassName;
return (
<a
href="rating" onClick={e => {
e.preventDefault();
onClickRating();
}}
onMouseMove={onMouseEnter}>
<i
className={className}
style={iStyle} />
</a>
);
};
Icon.propTypes = {
toggled: PropTypes.bool,
toggledClassName: PropTypes.string,
untoggledClassName: PropTypes.string,
onMouseEnter: PropTypes.func,
onClickRating: PropTypes.func,
};
Icon.defaultProps = {
toggled: false,
toggledClassName: null,
untoggledClassName: null,
onMouseEnter: null,
onClickRating: null,
};
class IconRating extends Component {
constructor(props) {
super(props);
this.state = {
currentRating: props.currentRating,
max: props.max,
currentRatingHover: 0,
hovering: false,
};
this.onMouseEnter = this.onMouseEnter.bind(this);
this.onMouseLeave = this.onMouseLeave.bind(this);
this.onClickRating = this.onClickRating.bind(this);
}
onMouseEnter(e, currentRating) {
let rating = currentRating;
if ((e.nativeEvent.clientX) < (e.target.offsetLeft + (e.target.offsetWidth / 2))) {
rating -= 0.5;
}
this.setState({
currentRatingHover: rating,
hovering: true,
});
}
onMouseLeave() {
this.setState({
hovering: false,
});
}
onClickRating() {
const { currentRating, currentRatingHover } = this.state;
const newRating = (currentRatingHover === currentRating) ? 0 : currentRatingHover;
this.setState({
currentRating: newRating,
});
if (this.props.onChange) {
this.props.onChange(newRating);
}
}
render() {
const { viewOnly, halfClassName, toggledClassName, untoggledClassName, className } = this.props;
const ratings = [];
let toggled = false;
let rating = 0;
let usedHalfClassName = null;
const f = () => {};
const onMouseEnter = viewOnly ? f : this.onMouseEnter;
const onClickRating = viewOnly ? f : this.onClickRating;
for (let i = 1; i <= this.state.max; ++i) {
rating = this.state[`currentRating${(this.state.hovering ? 'Hover' : '')}`];
toggled = i <= Math.round(rating);
usedHalfClassName = null;
if (halfClassName &&
Math.round(rating) === i &&
Math.floor(rating) !== rating) {
usedHalfClassName = halfClassName;
}
ratings.push(
<Icon
key={i}
toggledClassName={usedHalfClassName || toggledClassName}
untoggledClassName={untoggledClassName}
onMouseEnter={e => onMouseEnter(e, i)}
onClickRating={e => onClickRating(e, i)}
toggled={toggled} />
);
}
return (
<div className={className} onMouseLeave={this.onMouseLeave}>
{ratings}
</div>
);
}
}
IconRating.propTypes = {
currentRating: PropTypes.number,
max: PropTypes.number,
onChange: PropTypes.func,
viewOnly: PropTypes.bool,
halfClassName: PropTypes.string,
toggledClassName: PropTypes.string,
untoggledClassName: PropTypes.string,
className: PropTypes.string,
};
IconRating.defaultProps = {
currentRating: 0,
max: 5,
onChange: () => {},
viewOnly: false,
halfClassName: null,
toggledClassName: null,
untoggledClassName: null,
className: null,
};
export default IconRating;