import React, { Component } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import { translatable } from 'react-instantsearch-core'; import { createClassNames, find, range } from '../core/utils'; const cx = createClassNames('RatingMenu'); class RatingMenu extends Component { static propTypes = { translate: PropTypes.func.isRequired, refine: PropTypes.func.isRequired, createURL: PropTypes.func.isRequired, min: PropTypes.number, max: PropTypes.number, currentRefinement: PropTypes.shape({ min: PropTypes.number, max: PropTypes.number, }), count: PropTypes.arrayOf( PropTypes.shape({ value: PropTypes.string, count: PropTypes.number, }) ), canRefine: PropTypes.bool.isRequired, className: PropTypes.string, }; static defaultProps = { className: '', }; onClick(min, max, e) { e.preventDefault(); e.stopPropagation(); if ( min === this.props.currentRefinement.min && max === this.props.currentRefinement.max ) { this.props.refine({ min: this.props.min, max: this.props.max }); } else { this.props.refine({ min, max }); } } buildItem({ max, lowerBound, count, translate, createURL, isLastSelectableItem, }) { const disabled = !count; const isCurrentMinLower = this.props.currentRefinement.min < lowerBound; const selected = (isLastSelectableItem && isCurrentMinLower) || (!disabled && lowerBound === this.props.currentRefinement.min && max === this.props.currentRefinement.max); const icons = []; let rating = 0; for (let icon = 0; icon < max; icon++) { if (icon < lowerBound) { rating++; } icons.push([ , ' ', ]); } // The last item of the list (the default item), should not // be clickable if it is selected. const isLastAndSelect = isLastSelectableItem && selected; const onClickHandler = disabled || isLastAndSelect ? {} : { href: createURL({ min: lowerBound, max }), onClick: this.onClick.bind(this, lowerBound, max), }; return (