UNPKG

995 BJavaScriptView Raw
1import React from 'react'
2import propTypes from 'prop-types'
3import { observer } from 'startupjs'
4import Div from './../Div'
5import Row from './../Row'
6import H6 from './../typography/H6'
7import Star from './Star'
8import './index.styl'
9const AMOUNT = 5
10const ITEMS = Array(AMOUNT).fill(null)
11
12function Rating ({
13 style,
14 value,
15 readonly,
16 onChange
17}) {
18 return pug`
19 Row.root(style=style vAlign='center' align='between' styleName={readonly})
20 if readonly
21 Star(active)
22 H6(bold)= Number.isInteger(value) ? value : value.toFixed(1)
23 else
24 each ITEM, index in ITEMS
25 Div(key=index onPress=() => onChange && onChange(index + 1))
26 Star(active=index < Math.round(value))
27 `
28}
29
30Rating.defaultProps = {
31 value: 0,
32 readonly: false
33}
34
35Rating.propTypes = {
36 style: propTypes.oneOfType([propTypes.object, propTypes.array]),
37 value: propTypes.number,
38 readonly: propTypes.bool,
39 onChange: propTypes.func
40}
41
42export default observer(Rating)