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 | 3x 3x 3x 5x 22x 22x 22x 22x 22x 115x 1x 2x 2x 2x 1x 1x 22x 22x 22x 22x 1x | /*
Copyright (c) 2018-2019 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
import type {StarRatingPropsT, RatingStateT} from './types.js';
import {StyledRoot, StyledStar} from './styled-components.js';
import {getOverrides} from '../helpers/overrides.js';
import {ENTER_KEY_CODE, SPACE_KEY_CODE} from './utils.js';
class StarRating extends React.Component<StarRatingPropsT, RatingStateT> {
static defaultProps = {
overrides: {},
numItems: 5,
};
state = {};
selectItem = (value: number) => {
const {onChange} = this.props;
onChange && onChange({value});
this.setState({previewIndex: undefined});
};
updatePreview = (previewIndex?: number) => {
this.setState({previewIndex});
};
renderRatingContents = () => {
const {overrides = {}, value = -1, numItems} = this.props;
const {previewIndex} = this.state;
const [Star, starProps] = getOverrides(overrides.Item, StyledStar);
const ratings = [];
for (let x = 1; x <= numItems; x++) {
ratings.push(
<Star
key={x}
role="radio"
tabIndex={0}
aria-setsize={numItems}
aria-checked={x <= value}
aria-posinset={x}
$index={x}
$isActive={
previewIndex !== undefined ? x <= previewIndex : x <= value
}
$isSelected={x === previewIndex}
onClick={() => this.selectItem(x)}
onKeyDown={e => {
Eif (e.keyCode === SPACE_KEY_CODE || e.keyCode === ENTER_KEY_CODE) {
this.selectItem(x);
}
}}
onFocus={() => this.updatePreview(x)}
onMouseOver={() => this.updatePreview(x)}
onBlur={() => this.updatePreview(undefined)}
{...starProps}
/>,
);
}
return ratings;
};
render() {
const {overrides = {}} = this.props;
const [Root, rootProps] = getOverrides(overrides.Root, StyledRoot);
return (
<Root
data-baseweb="star-rating"
tabIndex={0}
role="radiogroup"
onBlur={() => this.updatePreview(undefined)}
onMouseLeave={() => this.updatePreview(undefined)}
{...rootProps}
>
{this.renderRatingContents()}
</Root>
);
}
}
export default StarRating;
|