2020-03-02 12:09:38 +08:00
|
|
|
import StarFilled from '@ant-design/icons/StarFilled';
|
2022-03-10 10:47:33 +08:00
|
|
|
import classNames from 'classnames';
|
2016-03-30 13:20:03 +08:00
|
|
|
import RcRate from 'rc-rate';
|
2022-05-19 09:46:26 +08:00
|
|
|
import type { RateProps as RcRateProps } from 'rc-rate/lib/Rate';
|
2022-05-21 22:14:15 +08:00
|
|
|
import * as React from 'react';
|
2020-04-27 13:42:04 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-21 22:14:15 +08:00
|
|
|
import Tooltip from '../tooltip';
|
2022-03-10 10:47:33 +08:00
|
|
|
import useStyle from './style';
|
2016-03-30 13:20:03 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export interface RateProps extends RcRateProps {
|
2018-12-28 16:04:05 +08:00
|
|
|
tooltips?: Array<string>;
|
2016-08-10 09:46:56 +08:00
|
|
|
}
|
|
|
|
|
2018-12-28 16:04:05 +08:00
|
|
|
interface RateNodeProps {
|
|
|
|
index: number;
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:21:40 +08:00
|
|
|
const Rate = React.forwardRef<unknown, RateProps>(({ prefixCls, tooltips, ...props }, ref) => {
|
2020-04-27 13:42:04 +08:00
|
|
|
const characterRender = (node: React.ReactElement, { index }: RateNodeProps) => {
|
2018-12-28 16:04:05 +08:00
|
|
|
if (!tooltips) return node;
|
|
|
|
return <Tooltip title={tooltips[index]}>{node}</Tooltip>;
|
|
|
|
};
|
|
|
|
|
2022-04-08 11:10:46 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2020-04-27 13:42:04 +08:00
|
|
|
const ratePrefixCls = getPrefixCls('rate', prefixCls);
|
|
|
|
|
2022-03-10 10:47:33 +08:00
|
|
|
// Style
|
2022-04-08 11:10:46 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(ratePrefixCls);
|
2022-03-10 10:47:33 +08:00
|
|
|
|
|
|
|
return wrapSSR(
|
2020-04-27 13:42:04 +08:00
|
|
|
<RcRate
|
|
|
|
ref={ref}
|
|
|
|
characterRender={characterRender}
|
2020-05-28 15:21:40 +08:00
|
|
|
{...props}
|
2022-03-10 10:47:33 +08:00
|
|
|
className={classNames(props.className, hashId)}
|
2020-04-27 13:42:04 +08:00
|
|
|
prefixCls={ratePrefixCls}
|
2020-04-27 21:02:16 +08:00
|
|
|
direction={direction}
|
2022-03-10 10:47:33 +08:00
|
|
|
/>,
|
2020-04-27 13:42:04 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Rate.displayName = 'Rate';
|
|
|
|
|
|
|
|
Rate.defaultProps = {
|
|
|
|
character: <StarFilled />,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Rate;
|