ant-design/components/rate/index.tsx

51 lines
1.3 KiB
TypeScript
Raw Normal View History

import StarFilled from '@ant-design/icons/StarFilled';
import classNames from 'classnames';
2016-03-30 13:20:03 +08:00
import RcRate from 'rc-rate';
import type { RateProps as RcRateProps } from 'rc-rate/lib/Rate';
2022-05-21 22:14:15 +08:00
import * as React from 'react';
import { ConfigContext } from '../config-provider';
2022-05-21 22:14:15 +08:00
import Tooltip from '../tooltip';
import useStyle from './style';
2016-03-30 13:20:03 +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;
}
const Rate = React.forwardRef<unknown, RateProps>((props, ref) => {
const { prefixCls, tooltips, character = <StarFilled />, ...rest } = props;
const characterRender = (node: React.ReactElement, { index }: RateNodeProps) => {
if (!tooltips) {
return node;
}
2018-12-28 16:04:05 +08:00
return <Tooltip title={tooltips[index]}>{node}</Tooltip>;
};
const { getPrefixCls, direction } = React.useContext(ConfigContext);
const ratePrefixCls = getPrefixCls('rate', prefixCls);
// Style
const [wrapSSR, hashId] = useStyle(ratePrefixCls);
return wrapSSR(
<RcRate
ref={ref}
character={character}
characterRender={characterRender}
{...rest}
className={classNames(props.className, hashId)}
prefixCls={ratePrefixCls}
direction={direction}
/>,
);
});
if (process.env.NODE_ENV !== 'production') {
Rate.displayName = 'Rate';
}
export default Rate;