별점 평가
별점 평가 (Rate)
별점을 이용해 어떤 대상에 대한 평가를 보여주거나 빠르게 남길 수 있는 컴포넌트입니다. 옆에서 설명하듯이, 단순한 별점부터 커스텀 캐릭터까지 폭넓게 활용할 수 있어요.
출처: 문서
본문
언제 사용하나요 (When To Use)
- 평가(evaluation)를 보여줄 때
- 어떤 대상에 대해 빠르게 평가를 남길 때
예제 (Examples)
기본 (Basic)
가장 간단한 사용법입니다.
import React from 'react';
import { Rate } from 'antd';
const App: React.FC = () => <Rate />;
export default App;
크기 (Sizes)
세 가지 크기를 지원합니다.
import React from 'react';
import { Flex, Rate } from 'antd';
const App: React.FC = () => (
<Flex vertical gap="medium">
<Rate size="large" />
<Rate />
<Rate size="small" />
</Flex>
);
export default App;
별 반 개 (Half star)
별 반 개 선택을 지원합니다.
import React from 'react';
import { Rate } from 'antd';
const App: React.FC = () => <Rate allowHalf defaultValue={2.5} />;
export default App;
설명 문구 표시 (Show copywriting)
Rate 컴포넌트에 설명 문구를 추가할 수 있습니다.
import React, { useState } from 'react';
import { Flex, Rate } from 'antd';
import type { RateProps } from 'antd';
const desc: RateProps['tooltips'] = [
'terrible',
{ placement: 'top', title: 'bad', trigger: 'hover' },
'normal',
'good',
'wonderful',
];
function getDescTitle(value: number, desc: RateProps['tooltips']) {
const item = desc?.[value - 1];
return item && typeof item === 'object' ? item.title : item;
}
const App: React.FC = () => {
const [value, setValue] = useState(3);
return (
<Flex gap="medium" vertical>
<Rate tooltips={desc} onChange={setValue} value={value} />
{value ? <span>{getDescTitle(value, desc) as React.ReactNode}</span> : null}
</Flex>
);
};
export default App;
읽기 전용 (Read only)
읽기 전용으로, 마우스로 상호작용할 수 없습니다.
import React from 'react';
import { Rate } from 'antd';
const App: React.FC = () => <Rate disabled defaultValue={2} />;
export default App;
별점 지우기 (Clear star)
클릭했던 별을 다시 클릭하면 별점을 지울 수 있게 해줍니다.
import React from 'react';
import { Flex, Rate } from 'antd';
const App: React.FC = () => (
<Flex gap="medium" vertical>
<Flex gap="medium">
<Rate defaultValue={3} />
<span>allowClear: true</span>
</Flex>
<Flex gap="medium">
<Rate defaultValue={3} allowClear={false} />
<span>allowClear: false</span>
</Flex>
</Flex>
);
export default App;
다른 문자 (Other Character)
기본 별을 알파벳, 숫자, iconfont 또는 한자어 같은 다른 문자로 바꿀 수 있습니다.
import React from 'react';
import { HeartOutlined } from '@ant-design/icons';
import { Flex, Rate } from 'antd';
const App: React.FC = () => (
<Flex vertical gap="medium">
<Rate character={<HeartOutlined />} allowHalf />
<Rate character="A" allowHalf style={{ fontSize: 36 }} />
<Rate character="好" allowHalf />
</Flex>
);
export default App;
캐릭터 커스터마이즈 (Customize character)
(RateProps) => ReactNode를 이용해 각 캐릭터를 개별적으로 커스터마이즈할 수 있습니다.
import React from 'react';
import { FrownOutlined, MehOutlined, SmileOutlined } from '@ant-design/icons';
import { Flex, Rate } from 'antd';
const customIcons: Record<number, React.ReactNode> = {
1: <FrownOutlined />,
2: <FrownOutlined />,
3: <MehOutlined />,
4: <SmileOutlined />,
5: <SmileOutlined />,
};
const App: React.FC = () => (
<Flex gap="medium" vertical>
<Rate defaultValue={2} character={({ index = 0 }) => index + 1} />
<Rate defaultValue={3} character={({ index = 0 }) => customIcons[index + 1]} />
</Flex>
);
export default App;
API
Common props ref:Common props
| Property | Description | Type | Default | Version | Global Config |
|---|---|---|---|---|---|
| allowClear | Whether to allow clear when click again | boolean | true | × | |
| allowHalf | Whether to allow semi selection | boolean | false | × | |
| character | The custom character of rate | ReactNode | (RateProps) => ReactNode | <StarFilled /> | function(): 4.4.0 | × |
| count | Star count | number | 5 | × | |
| defaultValue | The default value | number | 0 | × | |
| disabled | If read only, unable to interact | boolean | false | × | |
| keyboard | Support keyboard operation | boolean | true | 5.18.0 | × |
| size | Star size | 'small' | 'medium' | 'large' | 'medium' | × | |
| tooltips | Customize tooltip by each character | TooltipProps[] | string[] | - | × | |
| value | The current value | number | - | × | |
| onBlur | Callback when component lose focus | function() | - | × | |
| onChange | Callback when select value | function(value: number) | - | × | |
| onFocus | Callback when component get focus | function() | - | × | |
| onHoverChange | Callback when hover item | function(value: number) | - | × | |
| onKeyDown | Callback when keydown on component | function(event) | - | × |
메서드 (Methods)
| Name | Description |
|---|---|
| blur() | Remove focus |
| focus() | Get focus |
디자인 토큰 (Design Token)
컴포넌트 토큰 (Component Token - Rate)
| Token Name | Description | Type | Default Value |
|---|---|---|---|
| starBg | Star background color | string | rgba(0,0,0,0.06) |
| starColor | Star color | string | #fadb14 |
| starHoverScale | Scale of star when hover | readonly string[] | Transform | readonly Transform[] | { multi_value?: boolean; skip_check?: boolean; value: readonly string[] | Transform | (readonly string[] | Transform | undefined)[] | undefined } | undefined | scale(1.1) |
| starSize | Star size | number | 20 |
| starSizeLG | Large star size | number | 25 |
| starSizeSM | Small star size | number | 15 |
글로벌 토큰 (Global Token)
| Token Name | Description | Type | Default Value |
|---|---|---|---|
| colorText | Default text color which comply with W3C standards, and this color is also the darkest neutral color. | string | |
| fontFamily | The font family of Ant Design prioritizes the default interface font of the system, and provides a set of alternative font libraries that are suitable for screen display to maintain the readability and readability of the font under different platforms and browsers, reflecting the friendly, stable and professional characteristics. | string | |
| fontSize | The most widely used font size in the design system, from which the text gradient will be derived. | number | |
| lineHeight | Line height of text. | number | |
| marginXS | Control the margin of an element, with a small size. | number | |
| motionDurationMid | Motion speed, medium speed. Used for medium element animation interaction. | string |