Rate评分

评分组件。

何时使用#

  • 对评价进行展示。

  • 对事物进行快速的评级操作。

代码演示

最简单的用法。

expand codeexpand code
import React from 'react';
import { Rate } from 'antd';

const App: React.FC = () => <Rate />;

export default App;
normal

给评分组件加上文案展示。

expand codeexpand code
import React, { useState } from 'react';
import { Rate } from 'antd';

const desc = ['terrible', 'bad', 'normal', 'good', 'wonderful'];

const App: React.FC = () => {
  const [value, setValue] = useState(3);

  return (
    <span>
      <Rate tooltips={desc} onChange={setValue} value={value} />
      {value ? <span className="ant-rate-text">{desc[value - 1]}</span> : ''}
    </span>
  );
};

export default App;
allowClear: true
allowClear: false

支持允许或者禁用清除。

expand codeexpand code
import React from 'react';
import { Rate } from 'antd';

const App: React.FC = () => (
  <>
    <Rate defaultValue={3} />
    <span className="ant-rate-text">allowClear: true</span>
    <br />
    <Rate allowClear={false} defaultValue={3} />
    <span className="ant-rate-text">allowClear: false</span>
  </>
);

export default App;
  • 1
    1
  • 2
    2
  • 3
    3
  • 4
    4
  • 5
    5

可以使用 (RateProps) => ReactNode 的方式自定义每一个字符。

expand codeexpand code
import React from 'react';
import { Rate } from 'antd';
import { FrownOutlined, MehOutlined, SmileOutlined } from '@ant-design/icons';

const customIcons: Record<number, React.ReactNode> = {
  1: <FrownOutlined />,
  2: <FrownOutlined />,
  3: <MehOutlined />,
  4: <SmileOutlined />,
  5: <SmileOutlined />,
};

const App: React.FC = () => (
  <>
    <Rate defaultValue={2} character={({ index }: { index: number }) => index + 1} />
    <br />
    <Rate defaultValue={3} character={({ index }: { index: number }) => customIcons[index + 1]} />
  </>
);

export default App;

支持选中半星。

expand codeexpand code
import React from 'react';
import { Rate } from 'antd';

const App: React.FC = () => <Rate allowHalf defaultValue={2.5} />;

export default App;

只读,无法进行鼠标交互。

expand codeexpand code
import React from 'react';
import { Rate } from 'antd';

const App: React.FC = () => <Rate disabled defaultValue={2} />;

export default App;

  • A
    A
  • A
    A
  • A
    A
  • A
    A
  • A
    A

可以将星星替换为其他字符,比如字母,数字,字体图标甚至中文。

expand codeexpand code
import React from 'react';
import { Rate } from 'antd';
import { HeartOutlined } from '@ant-design/icons';

const App: React.FC = () => (
  <>
    <Rate character={<HeartOutlined />} allowHalf />
    <br />
    <Rate character="A" allowHalf style={{ fontSize: 36 }} />
    <br />
    <Rate character="" allowHalf />
  </>
);

export default App;

API#

属性说明类型默认值版本
allowClear是否允许再次点击后清除booleantrue
allowHalf是否允许半选booleanfalse
autoFocus自动获取焦点booleanfalse
character自定义字符ReactNode | (RateProps) => ReactNode<StarFilled />function(): 4.4.0
className自定义样式类名string-
countstar 总数number5
defaultValue默认值number0
disabled只读,无法进行交互booleanfalse
style自定义样式对象CSSProperties-
tooltips自定义每项的提示信息string[]-
value当前数,受控值number-
onBlur失去焦点时的回调function()-
onChange选择时的回调function(value: number)-
onFocus获取焦点时的回调function()-
onHoverChange鼠标经过时数值变化的回调function(value: number)-
onKeyDown按键回调function(event)-

方法#

名称描述
blur()移除焦点
focus()获取焦点