All files / src/Rating Rating.tsx

86.96% Statements 20/23
50% Branches 8/16
66.67% Functions 6/9
86.96% Lines 20/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                      1x                             19x 19x   19x   19x   19x               19x             95x       90x                                                             1x   18x 18x                                   1x   90x   90x   90x               90x                   1x   90x 90x                            
import * as React from 'react';
import { Box as ReakitBox } from 'reakit';
import _get from 'lodash/get';
import _times from 'lodash/times';
 
import { Size } from '../types';
import { useClassName, createComponent, createElement, createHook } from '../utils';
import { Box, BoxProps } from '../Box';
import { Rover } from '../Rover';
import { Icon } from '../Icon';
 
import * as styles from './styles';
 
export type LocalRatingProps = {
  color?: string;
  disabled?: boolean;
  item?: React.ReactElement<any>;
  items?: Array<React.ReactElement<any>>;
  isSingular?: boolean;
  maxValue?: number;
  onChange: (index: number) => void;
  roverProps?: { baseId?: string; stopId?: string };
  size?: Size;
  value: number | void;
};
export type RatingProps = BoxProps & LocalRatingProps;
 
const useProps = createHook<RatingProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const {
      color,
      disabled,
      item,
      items,
      isSingular,
      onChange,
      overrides,
      maxValue,
      roverProps,
      size,
      value,
      ...restProps
    } = props;
    const boxProps = Box.useProps(restProps);
 
    const rover = Rover.useState();
 
    const [hoveringIndex, setHoveringIndex] = React.useState(-1);
 
    const className = useClassName({
      style: styles.Rating,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
 
    return {
      ...boxProps,
      className,
      onMouseLeave: !disabled ? () => setHoveringIndex(-1) : undefined,
      overrides,
      role: 'radiogroup',
      children: _times(items ? items.length : maxValue, index => (
        <Rover {...rover} {...roverProps} disabled={disabled}>
          {props => (
            /*
            // @ts-ignore */
            <RatingItem
              key={index}
              {...props}
              aria-checked={value === index + 1}
              aria-posinset={index + 1}
              aria-setsize={maxValue}
              color={color}
              isActive={
                hoveringIndex >= 0
                  ? isSingular
                    ? hoveringIndex === index
                    : hoveringIndex >= index
                  : isSingular
                  ? value === index + 1
                  : value > index
              }
              onClick={!disabled ? () => onChange(index + 1) : undefined}
              onMouseEnter={!disabled ? () => setHoveringIndex(index) : undefined}
              overrides={overrides}
              size={size}
            >
              {item || _get(items, `[${index}]`)}
            </RatingItem>
          )}
        </Rover>
      ))
    };
  },
  { defaultProps: { maxValue: 5, value: 0 }, themeKey: 'Rating' }
);
 
export const Rating = createComponent<RatingProps>(
  props => {
    const ratingProps = useProps(props);
    return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: ratingProps });
  },
  {
    attach: {
      useProps
    },
    themeKey: 'Rating'
  }
);
 
////////////////////////////////////////
 
export type LocalRatingItemProps = {
  isActive?: boolean;
  size?: Size;
};
export type RatingItemProps = BoxProps & LocalRatingItemProps;
 
const useRatingItemProps = createHook<RatingItemProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const { children, color, ...restProps } = props;
 
    const boxProps = Box.useProps(restProps);
 
    const className = useClassName({
      style: styles.RatingItem,
      styleProps: { ...props, color },
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
 
    return {
      ...boxProps,
      className,
      role: 'radio',
      children
    };
  },
  { defaultProps: { children: <Icon icon="star" />, color: 'gold' }, themeKey: 'Rating.Item' }
);
 
export const RatingItem = createComponent<RatingItemProps>(
  props => {
    const ratingItemProps = useRatingItemProps(props);
    return createElement({
      children: props.children,
      component: ReakitBox,
      use: props.use,
      htmlProps: ratingItemProps
    });
  },
  {
    attach: {
      useProps
    },
    themeKey: 'Rating.Item'
  }
);