import ConfigProvider from '../config-provider'; import React, { Component } from 'react'; import cls from 'classnames'; import { Rating as NextRating } from '@alifd/next'; import { RatingProps as NextRatingProps } from '@alifd/next/types/rating'; interface RatingAddProps { shape?: 'star' | 'favorite' | 'number' } interface RatingProps extends NextRatingProps, RatingAddProps { } interface IRatingState { value: string, hoverValue: number, iconSpace: number, iconSize: number, clicked: boolean, } class Rating extends Component { static currentValue(min, max, hoverValue, stateValue) { let value = hoverValue ? hoverValue : stateValue; value = value >= max ? max : value; value = value <= min ? min : value; return value || 0; } constructor(props) { super(props); this.state = { value: 'value' in props ? props.value : props.defaultValue, hoverValue: 0, iconSpace: 0, iconSize: 0, clicked: false, // 标记组件是否被点击过 }; } genElements = values => { if (!values) { return null; } return values.map((value, index) => { return this.addEle(value, index); }); }; addEle = (value, index) => { const { prefix = 'next-' } = this.props; return ; }; // 获取overlay的长度 getOverlayWidth(size) { const { hoverValue } = this.state; let iconSize, iconSpace; if (size === 'medium') { iconSize = 12; iconSpace = 12; } else if (size === 'large') { iconSize = 14; iconSpace = 14; } else if (size === 'small') { iconSize = 10; iconSpace = 10; } if (!iconSpace || !iconSize) { return 'auto'; } const value = Rating.currentValue(0, this.props.count, hoverValue, this.state.value); const floorValue = Math.floor(value); return iconSize * value + (floorValue + 1) * iconSpace; } handleHover = value => { this.setState({ hoverValue: value }); }; handleLeave = () => { this.setState({ hoverValue: 0 }); }; render() { const { prefix = 'next-', className, shape, count = 5, size } = this.props; const arrayNew = [ ...Array(count + 1).keys() ].splice(1); const overlayStyle = { width: this.getOverlayWidth(size), top: '0', position: 'absolute', height: '24px', overflow: 'hidden', }; if (shape === 'number') { return (
this.setState({ hoverValue: val })} onHoverChange={val => this.setState({ hoverValue: val })} style={{ zIndex: 1001 }} allowHalf={false} className={cls(className, { [`${prefix}rate-number`]: true, })} />
{this.genElements(arrayNew)}
{this.genElements(arrayNew)}
); } return ( ); } } export default ConfigProvider.config(Rating);