-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Rating.js
203 lines (187 loc) · 5.66 KB
/
Rating.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import IconButton from '@material-ui/core/IconButton'
import orange from '@material-ui/core/colors/orange'
import grey from '@material-ui/core/colors/grey'
import { withStyles } from '@material-ui/core/styles'
import Star from '@material-ui/icons/Star'
import StarBorder from '@material-ui/icons/StarBorder'
const noPointerEvents = {
pointerEvents: 'none'
}
const styles = theme => ({
root: {
direction: theme.direction
},
iconButton: {
padding: 8,
marginLeft: -8,
transform: theme.direction === 'rtl' ? 'scaleX(-1)' : 'none',
'&:first-child': {
marginLeft: 0
}
},
icon: {},
disabled: noPointerEvents,
readOnly: noPointerEvents
})
/**
* Material design star rating component for your star application.
* @see [Card UI controls](https://material.io/guidelines/components/cards.html#cards-actions)
*/
class Rating extends Component {
constructor (props) {
super(props)
this.state = {
hoverValue: props.value
}
}
getIcon (type, index) {
const {
classes,
disabled,
iconNormal,
iconNormalRenderer,
iconHovered,
iconHoveredRenderer,
iconFilled,
iconFilledRenderer
} = this.props
const iconProps = { className: classes.icon }
if (disabled) {
iconProps.htmlColor = grey[300]
}
switch (type) {
case 'normal':
return iconNormalRenderer
? iconNormalRenderer({ ...this.props, index })
: React.cloneElement(iconNormal, iconProps)
case 'hovered':
return iconHoveredRenderer
? iconHoveredRenderer({ ...this.props, index })
: React.cloneElement(iconHovered, iconProps)
case 'filled':
return iconFilledRenderer
? iconFilledRenderer({ ...this.props, index })
: React.cloneElement(iconFilled, iconProps)
}
}
renderIcon (i) {
const { value, disableHover } = this.props
const rest = value >= i - 1 && value < i ? value - i + 1 : 0
const filled = rest > 0 || i <= value
const hovered = rest > 0 || i <= Math.floor(this.state.hoverValue)
if (rest > 0) {
return (
<React.Fragment>
{React.cloneElement(Math.floor(this.state.hoverValue) < value ? this.getIcon('normal', i) : this.getIcon('hovered', i), {
style: { position: 'absolute' }
})}
{React.cloneElement(this.state.hoverValue < value ? this.getIcon('hovered', i) : this.getIcon('filled', i), {
style: {
clipPath: `polygon(0% 0%, ${rest * 100}% 0%, ${rest * 100}% 100%, 0% 100%)`
}
})}
</React.Fragment>
)
}
if (!disableHover && ((hovered && !filled) || (!hovered && filled))) {
return this.getIcon('hovered', i)
} else if (filled) {
return this.getIcon('filled', i)
} else {
return this.getIcon('normal', i)
}
}
render () {
const {
className,
classes,
component: Component,
disabled,
disableHover,
iconFilled,
iconFilledRenderer,
iconHovered,
iconHoveredRenderer,
iconNormal,
iconNormalRenderer,
max,
onChange,
readOnly,
value,
...other
} = this.props
const rating = []
for (let i = 1; i <= max; i++) {
rating.push(
<IconButton
key={i}
className={classNames(
classes.iconButton,
{
[classes.disabled]: disabled,
[classes.readOnly]: readOnly
}
)}
disabled={disabled}
onMouseEnter={() => this.setState({ hoverValue: i })}
onMouseLeave={() => this.setState({ hoverValue: value })}
onClick={() => {
if (!readOnly && onChange) {
onChange(i)
}
}}
>
{this.renderIcon(i)}
</IconButton>
)
}
return (<Component className={classNames(className, classes.root)} {...other}>{rating}</Component>)
}
}
Rating.defaultProps = {
component: 'div',
disabled: false,
disableHover: false,
iconHovered: <StarBorder htmlColor={orange[500]} />,
iconFilled: <Star htmlColor={orange[500]} />,
iconNormal: <StarBorder htmlColor={grey[300]} />,
max: 5,
readOnly: false,
value: 0
}
Rating.propTypes = {
/** CSS classname to apply to the root component. */
className: PropTypes.string,
/** Useful to extend the style applied to components. See the repository README for the accepted keys. */
classes: PropTypes.object.isRequired,
/** The component used for the root node. Either a string to use a DOM element or a component. */
component: PropTypes.elementType,
/** Disables the rating and gray it out if set to true. */
disabled: PropTypes.bool,
/** Disable hover effects if set to true. */
disableHover: PropTypes.bool,
/** This is the icon to be used as an icon in value range. */
iconFilled: PropTypes.node,
/** Overrides filled icon renderer. */
iconFilledRenderer: PropTypes.func,
/** This is the icon to be used as an hovered icon. */
iconHovered: PropTypes.node,
/** Overrides hovered icon renderer. */
iconHoveredRenderer: PropTypes.func,
/** This is the icon to be used as an normal icon. */
iconNormal: PropTypes.node,
/** Overrides normal icon renderer. */
iconNormalRenderer: PropTypes.func,
/** The max value of the rating bar. */
max: PropTypes.number,
/** Fired when a value is clicked. */
onChange: PropTypes.func,
/** Don't allow input if set to true. */
readOnly: PropTypes.bool,
/** The value of the rating bar. */
value: PropTypes.number
}
export default withStyles(styles)(Rating)