Rate
Rate component.
When To Use#
Show evaluation.
A quick rating operation on something.
Examples
TypeScript
JavaScript
import { Rate } from 'infrad';
import React from 'react';
const App: React.FC = () => <Rate />;
export default App;
TypeScript
JavaScript
import { Rate } from 'infrad';
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 'infrad';
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 'infra-design-icons';
import { Rate } from 'infrad';
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 'infrad';
import React from 'react';
const App: React.FC = () => <Rate allowHalf defaultValue={2.5} />;
export default App;
TypeScript
JavaScript
import { Rate } from 'infrad';
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 'infra-design-icons';
import { Rate } from 'infrad';
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#
Property | Description | type | Default | Version |
---|---|---|---|---|
allowClear | Whether to allow clear when click again | boolean | true | |
allowHalf | Whether to allow semi selection | boolean | false | |
autoFocus | If get focus when component mounted | boolean | false | |
character | The custom character of rate | ReactNode | (RateProps) => ReactNode | <StarFilled /> | function(): 4.4.0 |
className | The custom class name of rate | string | - | |
count | Star count | number | 5 | |
defaultValue | The default value | number | 0 | |
disabled | If read only, unable to interact | boolean | false | |
style | The custom style object of rate | CSSProperties | - | |
tooltips | Customize tooltip by each character | 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 |