2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-03-30 13:20:03 +08:00
|
|
|
import RcRate from 'rc-rate';
|
2018-12-28 16:04:05 +08:00
|
|
|
import omit from 'omit.js';
|
2020-03-02 12:09:38 +08:00
|
|
|
import StarFilled from '@ant-design/icons/StarFilled';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2018-12-28 16:04:05 +08:00
|
|
|
import Tooltip from '../tooltip';
|
2020-04-27 13:42:04 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2016-03-30 13:20:03 +08:00
|
|
|
|
2016-08-10 09:46:56 +08:00
|
|
|
export interface RateProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
count?: number;
|
|
|
|
value?: number;
|
|
|
|
defaultValue?: number;
|
|
|
|
allowHalf?: boolean;
|
2017-12-15 15:30:38 +08:00
|
|
|
allowClear?: boolean;
|
2016-08-10 09:46:56 +08:00
|
|
|
disabled?: boolean;
|
2018-12-28 16:04:05 +08:00
|
|
|
tooltips?: Array<string>;
|
2019-06-16 20:51:47 +08:00
|
|
|
onChange?: (value: number) => void;
|
|
|
|
onHoverChange?: (value: number) => void;
|
2017-03-06 14:23:38 +08:00
|
|
|
character?: React.ReactNode;
|
2017-02-21 14:44:13 +08:00
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
2016-08-10 09:46:56 +08:00
|
|
|
}
|
|
|
|
|
2018-12-28 16:04:05 +08:00
|
|
|
interface RateNodeProps {
|
|
|
|
index: number;
|
|
|
|
}
|
|
|
|
|
2020-04-27 13:42:04 +08:00
|
|
|
const Rate = React.forwardRef<unknown, RateProps>((props, ref) => {
|
|
|
|
const characterRender = (node: React.ReactElement, { index }: RateNodeProps) => {
|
|
|
|
const { tooltips } = props;
|
2018-12-28 16:04:05 +08:00
|
|
|
if (!tooltips) return node;
|
|
|
|
return <Tooltip title={tooltips[index]}>{node}</Tooltip>;
|
|
|
|
};
|
|
|
|
|
2020-04-27 13:42:04 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2020-04-27 21:02:16 +08:00
|
|
|
const { prefixCls, ...restProps } = props;
|
2020-04-27 13:42:04 +08:00
|
|
|
const rateProps = omit(restProps, ['tooltips']);
|
|
|
|
const ratePrefixCls = getPrefixCls('rate', prefixCls);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RcRate
|
|
|
|
ref={ref}
|
|
|
|
characterRender={characterRender}
|
|
|
|
{...rateProps}
|
|
|
|
prefixCls={ratePrefixCls}
|
2020-04-27 21:02:16 +08:00
|
|
|
direction={direction}
|
2020-04-27 13:42:04 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Rate.displayName = 'Rate';
|
|
|
|
|
|
|
|
Rate.defaultProps = {
|
|
|
|
character: <StarFilled />,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Rate;
|