Rate评分
评分组件。
何时使用#
对评价进行展示。
对事物进行快速的评级操作。
代码演示
TypeScript
JavaScript
import { Rate } from 'antd';
import React from 'react';
const App: React.FC = () => <Rate />;
export default App;
TypeScript
JavaScript
import { Rate } from 'antd';
import React, { useState } from 'react';
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;
TypeScript
JavaScript
import { Rate } from 'antd';
import React from 'react';
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;
- 11
- 22
- 33
- 44
- 55
TypeScript
JavaScript
import { FrownOutlined, MehOutlined, SmileOutlined } from '@ant-design/icons';
import { Rate } from 'antd';
import React from 'react';
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;
TypeScript
JavaScript
import { Rate } from 'antd';
import React from 'react';
const App: React.FC = () => <Rate allowHalf defaultValue={2.5} />;
export default App;
TypeScript
JavaScript
import { Rate } from 'antd';
import React from 'react';
const App: React.FC = () => <Rate disabled defaultValue={2} />;
export default App;
- AA
- AA
- AA
- AA
- AA
- 好好
- 好好
- 好好
- 好好
- 好好
TypeScript
JavaScript
import { HeartOutlined } from '@ant-design/icons';
import { Rate } from 'antd';
import React from 'react';
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 | 是否允许再次点击后清除 | boolean | true | |
allowHalf | 是否允许半选 | boolean | false | |
autoFocus | 自动获取焦点 | boolean | false | |
character | 自定义字符 | ReactNode | (RateProps) => ReactNode | <StarFilled /> | function(): 4.4.0 |
className | 自定义样式类名 | string | - | |
count | star 总数 | number | 5 | |
defaultValue | 默认值 | number | 0 | |
disabled | 只读,无法进行交互 | boolean | false | |
style | 自定义样式对象 | CSSProperties | - | |
tooltips | 自定义每项的提示信息 | string[] | - | |
value | 当前数,受控值 | number | - | |
onBlur | 失去焦点时的回调 | function() | - | |
onChange | 选择时的回调 | function(value: number) | - | |
onFocus | 获取焦点时的回调 | function() | - | |
onHoverChange | 鼠标经过时数值变化的回调 | function(value: number) | - | |
onKeyDown | 按键回调 | function(event) | - |
方法#
名称 | 描述 |
---|---|
blur() | 移除焦点 |
focus() | 获取焦点 |