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([ = lowerBound ? 'starIcon--empty' : 'starIcon--full' )} aria-hidden="true" width="24" height="24" > = lowerBound ? 'starEmptySymbol' : 'starSymbol' )}`} /> , ' ', ]); } // 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 (
  • {icons} {' '} {count}
  • ); } render() { const { min, max, translate, count, createURL, canRefine, className } = this.props; // min & max are always set when there is a results, otherwise it means // that we don't want to render anything since we don't have any values. const limitMin = min !== undefined && min >= 0 ? min : 1; const limitMax = max !== undefined && max >= 0 ? max : 0; const inclusiveLength = limitMax - limitMin + 1; const values = count .map((item) => ({ ...item, value: parseFloat(item.value) })) .filter((item) => item.value >= limitMin && item.value <= limitMax); const items = range({ start: 0, end: Math.max(inclusiveLength, 0) }) .map((index) => { const element = find(values, (item) => item.value === limitMax - index); const placeholder = { value: limitMax - index, count: 0, total: 0 }; return element || placeholder; }) .reduce( (acc, item, index) => acc.concat({ ...item, total: index === 0 ? item.count : acc[index - 1].total + item.count, }), [] ) .map((item, index, arr) => this.buildItem({ lowerBound: item.value, count: item.total, isLastSelectableItem: arr.length - 1 === index, max: limitMax, translate, createURL, }) ); return (
    ); } } export default translatable({ ratingLabel: ' & Up', })(RatingMenu);