mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-15 09:21:22 +08:00
889a9ba9fd
* fix: fix tooltip trigger type * fix: use extends over duck type * update rc-tooltip deps * fix rate ts define Co-authored-by: 二货机器人 <smith3816@gmail.com>
80 lines
1.8 KiB
TypeScript
80 lines
1.8 KiB
TypeScript
import * as React from 'react';
|
|
import RcRate from 'rc-rate';
|
|
import omit from 'omit.js';
|
|
import classNames from 'classnames';
|
|
import StarFilled from '@ant-design/icons/StarFilled';
|
|
|
|
import Tooltip from '../tooltip';
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
|
|
|
export interface RateProps {
|
|
prefixCls?: string;
|
|
count?: number;
|
|
value?: number;
|
|
defaultValue?: number;
|
|
allowHalf?: boolean;
|
|
allowClear?: boolean;
|
|
disabled?: boolean;
|
|
tooltips?: Array<string>;
|
|
onChange?: (value: number) => void;
|
|
onHoverChange?: (value: number) => void;
|
|
character?: React.ReactNode;
|
|
className?: string;
|
|
style?: React.CSSProperties;
|
|
}
|
|
|
|
interface RateNodeProps {
|
|
index: number;
|
|
}
|
|
|
|
export default class Rate extends React.Component<RateProps, any> {
|
|
static defaultProps = {
|
|
character: <StarFilled />,
|
|
};
|
|
|
|
private rcRate: any;
|
|
|
|
saveRate = (node: any) => {
|
|
this.rcRate = node;
|
|
};
|
|
|
|
characterRender = (node: React.ReactElement, { index }: RateNodeProps) => {
|
|
const { tooltips } = this.props;
|
|
if (!tooltips) return node;
|
|
|
|
return <Tooltip title={tooltips[index]}>{node}</Tooltip>;
|
|
};
|
|
|
|
focus() {
|
|
this.rcRate.focus();
|
|
}
|
|
|
|
blur() {
|
|
this.rcRate.blur();
|
|
}
|
|
|
|
renderRate = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
|
const { prefixCls, className, ...restProps } = this.props;
|
|
|
|
const rateProps = omit(restProps, ['tooltips']);
|
|
const ratePrefixCls = getPrefixCls('rate', prefixCls);
|
|
const rateClassNames = classNames(className, {
|
|
[`${ratePrefixCls}-rtl`]: direction === 'rtl',
|
|
});
|
|
|
|
return (
|
|
<RcRate
|
|
ref={this.saveRate}
|
|
characterRender={this.characterRender}
|
|
{...rateProps}
|
|
prefixCls={ratePrefixCls}
|
|
className={rateClassNames}
|
|
/>
|
|
);
|
|
};
|
|
|
|
render() {
|
|
return <ConfigConsumer>{this.renderRate}</ConfigConsumer>;
|
|
}
|
|
}
|