diff --git a/components/rate/__tests__/index.test.js b/components/rate/__tests__/index.test.js index e215eb28c9..f1ffb45c3e 100644 --- a/components/rate/__tests__/index.test.js +++ b/components/rate/__tests__/index.test.js @@ -4,7 +4,7 @@ import mountTest from '../../../tests/shared/mountTest'; import rtlTest from '../../../tests/shared/rtlTest'; describe('Rate', () => { - focusTest(Rate); + focusTest(Rate, { refFocus: true }); mountTest(Rate); rtlTest(Rate); }); diff --git a/components/rate/index.tsx b/components/rate/index.tsx index 2c272a31c7..c658a3ab09 100644 --- a/components/rate/index.tsx +++ b/components/rate/index.tsx @@ -5,7 +5,7 @@ import classNames from 'classnames'; import StarFilled from '@ant-design/icons/StarFilled'; import Tooltip from '../tooltip'; -import { ConfigConsumer, ConfigConsumerProps } from '../config-provider'; +import { ConfigContext } from '../config-provider'; export interface RateProps { prefixCls?: string; @@ -27,53 +27,36 @@ interface RateNodeProps { index: number; } -export default class Rate extends React.Component { - static defaultProps = { - character: , - }; - - private rcRate: any; - - saveRate = (node: any) => { - this.rcRate = node; - }; - - characterRender = (node: React.ReactElement, { index }: RateNodeProps) => { - const { tooltips } = this.props; +const Rate = React.forwardRef((props, ref) => { + const characterRender = (node: React.ReactElement, { index }: RateNodeProps) => { + const { tooltips } = props; if (!tooltips) return node; - return {node}; }; - 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 ( - - ); - }; - - render() { - return {this.renderRate}; - } -} + const { getPrefixCls, direction } = React.useContext(ConfigContext); + const { prefixCls, className, ...restProps } = props; + const rateProps = omit(restProps, ['tooltips']); + const ratePrefixCls = getPrefixCls('rate', prefixCls); + const rateClassNames = classNames(className, { + [`${ratePrefixCls}-rtl`]: direction === 'rtl', + }); + + return ( + + ); +}); + +Rate.displayName = 'Rate'; + +Rate.defaultProps = { + character: , +}; + +export default Rate;