2024-04-01 15:49:45 +08:00
|
|
|
import * as React from 'react';
|
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';
|
2023-06-29 19:30:34 +08:00
|
|
|
import type { RateRef, RateProps as RcRateProps } from 'rc-rate/lib/Rate';
|
2024-04-01 15:49:45 +08:00
|
|
|
import type { StarProps as RcStarProps } from 'rc-rate/lib/Star';
|
|
|
|
|
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 {
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2018-12-28 16:04:05 +08:00
|
|
|
tooltips?: Array<string>;
|
2016-08-10 09:46:56 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 16:41:43 +08:00
|
|
|
const Rate = React.forwardRef<RateRef, RateProps>((props, ref) => {
|
2023-01-20 11:03:50 +08:00
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
className,
|
|
|
|
rootClassName,
|
2023-06-29 19:30:34 +08:00
|
|
|
style,
|
2023-01-20 11:03:50 +08:00
|
|
|
tooltips,
|
|
|
|
character = <StarFilled />,
|
|
|
|
...rest
|
|
|
|
} = props;
|
2023-06-29 19:30:34 +08:00
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
const characterRender: RcStarProps['characterRender'] = (node, { index }) => {
|
2022-09-30 17:44:24 +08:00
|
|
|
if (!tooltips) {
|
|
|
|
return node;
|
|
|
|
}
|
2024-04-01 15:49:45 +08:00
|
|
|
return <Tooltip title={tooltips[index as number]}>{node}</Tooltip>;
|
2018-12-28 16:04:05 +08:00
|
|
|
};
|
|
|
|
|
2023-06-29 19:30:34 +08:00
|
|
|
const { getPrefixCls, direction, rate } = 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
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(ratePrefixCls);
|
2022-03-10 10:47:33 +08:00
|
|
|
|
2023-06-29 19:30:34 +08:00
|
|
|
const mergedStyle: React.CSSProperties = { ...rate?.style, ...style };
|
|
|
|
|
2023-11-13 15:33:55 +08:00
|
|
|
return wrapCSSVar(
|
2020-04-27 13:42:04 +08:00
|
|
|
<RcRate
|
|
|
|
ref={ref}
|
2022-09-30 17:44:24 +08:00
|
|
|
character={character}
|
2020-04-27 13:42:04 +08:00
|
|
|
characterRender={characterRender}
|
2022-09-30 17:44:24 +08:00
|
|
|
{...rest}
|
2023-12-14 14:58:53 +08:00
|
|
|
className={classNames(className, rootClassName, hashId, cssVarCls, rate?.className)}
|
2023-06-29 19:30:34 +08:00
|
|
|
style={mergedStyle}
|
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
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Rate.displayName = 'Rate';
|
|
|
|
}
|
2020-04-27 13:42:04 +08:00
|
|
|
|
|
|
|
export default Rate;
|