Rate

Rate component.

When To Use#

  • Show evaluation.

  • A quick rating operation on something.

Examples

The simplest usage.

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

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

export default App;
normal

Add copywriting in rate components.

expand codeexpand code
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;
allowClear: true
allowClear: false

Support set allow to clear star when click again.

expand codeexpand code
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;
  • 1
    1
  • 2
    2
  • 3
    3
  • 4
    4
  • 5
    5

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

expand codeexpand code
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;

Support select half star.

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

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

export default App;

Read only, can't use mouse to interact.

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

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

export default App;

  • 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 { 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#

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