Rate

Rate component.

When To Use#

  • Show evaluation.

  • A quick rating operation on something.

Examples

The simplest usage.

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

ReactDOM.render(<Rate />, mountNode);
normal

Add copywriting in rate components.

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

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

class Rater extends React.Component {
  state = {
    value: 3,
  };

  handleChange = value => {
    this.setState({ value });
  };

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

ReactDOM.render(<Rater />, mountNode);
allowClear: true
allowClear: false

Support set allow to clear star when click again.

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

ReactDOM.render(
  <>
    <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>
  </>,
  mountNode,
);
  • 1
    1
  • 2
    2
  • 3
    3
  • 4
    4
  • 5
    5

Can customize each character using (RateProps) => ReactNode.

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

const customIcons = {
  1: <FrownOutlined />,
  2: <FrownOutlined />,
  3: <MehOutlined />,
  4: <SmileOutlined />,
  5: <SmileOutlined />,
};

ReactDOM.render(
  <>
    <Rate defaultValue={2} character={({ index }) => index + 1} />
    <br />
    <Rate defaultValue={3} character={({ index }) => customIcons[index + 1]} />
  </>,
  mountNode,
);

Support select half star.

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

ReactDOM.render(<Rate allowHalf defaultValue={2.5} />, mountNode);

Read only, can't use mouse to interact.

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

ReactDOM.render(<Rate disabled defaultValue={2} />, mountNode);

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

Replace the default star to other character like alphabet, digit, iconfont or even Chinese word.

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

ReactDOM.render(
  <>
    <Rate character={<HeartOutlined />} allowHalf />
    <br />
    <Rate character="A" allowHalf style={{ fontSize: 36 }} />
    <br />
    <Rate character="" allowHalf />
  </>,
  mountNode,
);

API#

PropertyDescriptiontypeDefaultVersion
allowClearWhether to allow clear when click againbooleantrue
allowHalfWhether to allow semi selectionbooleanfalse
autoFocusIf get focus when component mountedbooleanfalse
characterThe custom character of rateReactNode | (RateProps) => ReactNode<StarFilled />function(): 4.4.0
classNameThe custom class name of ratestring-
countStar countnumber5
defaultValueThe default valuenumber0
disabledIf read only, unable to interactbooleanfalse
styleThe custom style object of rateCSSProperties-
tooltipsCustomize tooltip by each characterstring[]-
valueThe current valuenumber-
onBlurCallback when component lose focusfunction()-
onChangeCallback when select valuefunction(value: number)-
onFocusCallback when component get focusfunction()-
onHoverChangeCallback when hover itemfunction(value: number)-
onKeyDownCallback when keydown on componentfunction(event)-

Methods#

NameDescription
blur()Remove focus
focus()Get focus