mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-12 15:19:58 +08:00
1cd79e3b0a
Co-authored-by: Nicolò Zandarin <nicolo.zandarin@atroos.com>
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import * as React from 'react';
|
|
import StarFilled from '@ant-design/icons/StarFilled';
|
|
import classNames from 'classnames';
|
|
import RcRate from 'rc-rate';
|
|
import type { RateRef, RateProps as RcRateProps } from 'rc-rate/lib/Rate';
|
|
import type { StarProps as RcStarProps } from 'rc-rate/lib/Star';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
|
import Tooltip from '../tooltip';
|
|
import useStyle from './style';
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
|
|
|
export interface RateProps extends RcRateProps {
|
|
rootClassName?: string;
|
|
tooltips?: Array<string>;
|
|
}
|
|
|
|
const Rate = React.forwardRef<RateRef, RateProps>((props, ref) => {
|
|
const {
|
|
prefixCls,
|
|
className,
|
|
rootClassName,
|
|
style,
|
|
tooltips,
|
|
character = <StarFilled />,
|
|
disabled: customDisabled,
|
|
...rest
|
|
} = props;
|
|
|
|
const characterRender: RcStarProps['characterRender'] = (node, { index }) => {
|
|
if (!tooltips) {
|
|
return node;
|
|
}
|
|
return <Tooltip title={tooltips[index as number]}>{node}</Tooltip>;
|
|
};
|
|
|
|
const { getPrefixCls, direction, rate } = React.useContext(ConfigContext);
|
|
const ratePrefixCls = getPrefixCls('rate', prefixCls);
|
|
|
|
// Style
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(ratePrefixCls);
|
|
|
|
const mergedStyle: React.CSSProperties = { ...rate?.style, ...style };
|
|
|
|
// ===================== Disabled =====================
|
|
const disabled = React.useContext(DisabledContext);
|
|
const mergedDisabled = customDisabled ?? disabled;
|
|
|
|
return wrapCSSVar(
|
|
<RcRate
|
|
ref={ref}
|
|
character={character}
|
|
characterRender={characterRender}
|
|
disabled={mergedDisabled}
|
|
{...rest}
|
|
className={classNames(className, rootClassName, hashId, cssVarCls, rate?.className)}
|
|
style={mergedStyle}
|
|
prefixCls={ratePrefixCls}
|
|
direction={direction}
|
|
/>,
|
|
);
|
|
});
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
Rate.displayName = 'Rate';
|
|
}
|
|
|
|
export default Rate;
|