# @uiw/react-rate

> Rate component

Latest version **4.22.3** (published 2023-11-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @uiw/react-rate
pnpm add @uiw/react-rate
yarn add @uiw/react-rate
bun add @uiw/react-rate
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; has provenance.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.22.3 |
| Published | 2023-11-29 |
| First published | 2020-05-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 22.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 740 |
| Author | Kenny Wong |
| Maintainers | uiwjs, wcjiang |
| Keywords | rate, react-rate, design, uiw, uiw-react, react.js, react, react-component, component, components, ui, css, uikit, react-ui, framework, front-end, frontend |

## Links

- npm: https://www.npmjs.com/package/@uiw/react-rate
- Repository: https://github.com/uiwjs/uiw
- Homepage: https://uiwjs.github.io/#/components/rate
- Issues: https://github.com/uiwjs/uiw/issues
- Funding: https://jaywcjlove.github.io/#/sponsor
- npm.io page: https://npm.io/package/@uiw/react-rate

## Dependencies (1)

- [@uiw/utils](https://npm.io/package/@uiw/utils.md) ^4.22.3

## Alternatives

- [@progress/kendo-ooxml](https://npm.io/package/@progress/kendo-ooxml.md) — 152.1K weekly downloads
- [@progress/kendo-react-ripple](https://npm.io/package/@progress/kendo-react-ripple.md) — 8.0K weekly downloads
- [@progress/kendo-react-orgchart](https://npm.io/package/@progress/kendo-react-orgchart.md) — 4.3K weekly downloads
- [@praxisui/dynamic-fields](https://npm.io/package/@praxisui/dynamic-fields.md) — 2.4K weekly downloads
- [@progress/kendo-vue-listbox](https://npm.io/package/@progress/kendo-vue-listbox.md) — 932 weekly downloads

## Recent versions

- 4.22.3 (latest) — 2023-11-29
- 5.0.0-bate.2.1 (bate) — 2023-03-10
- 4.22.2 — 2023-11-09
- 4.22.1 — 2023-11-08
- 4.22.0 — 2023-09-19
- 4.21.28 — 2023-05-05
- 4.21.27 — 2023-04-21
- 4.21.26 — 2023-04-20
- 4.21.25 — 2023-03-14
- 4.21.24 — 2023-03-08
- 5.0.0-bate.2.0 — 2023-03-02
- 4.21.23 — 2022-12-21
- 4.21.22 — 2022-12-19
- 4.21.21 — 2022-12-19
- 5.0.0-bate-19 — 2022-12-12
- … 157 more at https://npm.io/package/@uiw/react-rate/versions

## README

Rate 评分
===

[![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor)
[![Open in unpkg](https://img.shields.io/badge/Open%20in-unpkg-blue)](https://uiwjs.github.io/npm-unpkg/#/pkg/@uiw/react-rate/file/README.md)
[![NPM Downloads](https://img.shields.io/npm/dm/@uiw/react-rate.svg?style=flat)](https://www.npmjs.com/package/@uiw/react-rate)
[![npm version](https://img.shields.io/npm/v/@uiw/react-rate.svg?label=@uiw/react-rate)](https://npmjs.com/@uiw/react-rate)

评分组件

```jsx
import { Rate } from 'uiw';
// or
import Rate from '@uiw/react-rate';
```

### 基本用法

按钮样式的单选组合。

```jsx mdx:preview&bg=#fff
import React from 'react';
import { Rate, Divider, Icon } from 'uiw';

export default function Demo() {
  return (
    <div>
      <Rate value={3} />
      <Divider />
      <Rate allowHalf character={<Icon type="star-on" />} value={2.5} />
      <Divider />
      <Rate color="#393E48" character="☆" value={4} />
      <Divider />
      <Rate color="#28a745" character={<Icon type="heart-on" />} value={3} />
      <Divider />
      <Rate color="#dc3545" character="✿" value={4} />
    </div>
  )
}
```

### 在表单中使用

在 [`<Form />`](#/components/form) 表单中应用 [`<Rate />`](#/components/rate) 组件

```jsx mdx:preview&bg=#fff
import React from 'react';
import { Form, Row, Col, Icon, Rate, Button, Notify } from 'uiw';

export default function Demo() {
  return (
    <div>
      <Form
        resetOnSubmit={false}
        onSubmitError={(error) => {
          if (error.filed) {
            return { ...error.filed };
          }
          return null;
        }}
        onSubmit={({initial, current}) => {
          const errorObj = {};
          if (current.rate < 2) {
            errorObj.rate = '评分低于 2 拒绝提交评分！';
          }
          if(Object.keys(errorObj).length > 0) {
            const err = new Error();
            err.filed = errorObj;
            Notify.error({ title: '提交失败！', description: '请确认提交表单是否正确！' });
            throw err;
          }
          Notify.success({
            title: '提交成功！',
            description: `表单提交成功，选择值为：${current.rate}！`,
          });
        }}
        fields={{
          rate: {
            initialValue: 3,
            children: <Rate style={{ fontSize: 14 }} character={<Icon type="star-on" />} />
          },
        }}
      >
        {({ fields, state, canSubmit }) => {
          return (
            <div>
              <Row>
                <Col fixed>{fields.rate}</Col>
              </Row>
              <Row>
                <Col fixed>
                  <Button size="small" disabled={!canSubmit()} type="primary" htmlType="submit">提交</Button>
                </Col>
              </Row>
              <Row>
                <Col>
                  <pre style={{ padding: 10, marginTop: 10 }}>
                    {JSON.stringify(state.current, null, 2)}
                  </pre>
                </Col>
              </Row>
            </div>
          )
        }}
      </Form>
    </div>
  )
}
```

### 受控

```jsx mdx:preview&bg=#fff
import React from 'react';
import { Rate, Divider, Icon, RadioGroup, Radio } from 'uiw';

export default function Demo() {
  const [value, setValue] = React.useState(4);
  return (
    <div>
      <Rate readOnly value={value} character={<Icon type="star-on" />} />
      <Divider />
      <RadioGroup value={value} onChange={(e) => {
        setValue(e.target.value);
      }}>
        <Radio value="0">选择 0 星</Radio>
        <Radio value="1">选择 1 星</Radio>
        <Radio value="2">选择 2 星</Radio>
        <Radio value="3">选择 3 星</Radio>
      </RadioGroup>
    </div>
  )
}
```

### 只读

按钮样式的单选组合。

```jsx mdx:preview&bg=#fff
import React from 'react';
import { Rate, Divider, Icon } from 'uiw';

export default function Demo() {
  return (
    <div>
      <Rate readOnly value={3} />
      <Divider />
      <Rate readOnly character="☆" value={4} />
      <Divider />
      <Rate readOnly character={<Icon type="heart-on" />} value={3.5} />
      <Divider />
      <Rate readOnly character="✿" value={4} />
    </div>
  );
}
```

### 文本

按钮样式的单选组合。

```jsx mdx:preview&bg=#fff
import React from 'react';
import { Rate, Divider } from 'uiw';

export default function Demo() {
  return (
    <div>
      <Rate character="美" value={4} />
      <Divider />
      <Rate color="#d80000" character="好" value={3} />
      <Divider />
      <Rate character="传" value={4} />
    </div>
  );
}
```

### 半选

```jsx mdx:preview&bg=#fff
import React from 'react';
import { Rate, Divider, Icon } from 'uiw';

export default function Demo() {
  return (
    <div>
      <Rate allowHalf character={<Icon type="heart-on" />} value={3.3} />
      <Divider />
      <Rate allowHalf color="#d80000" character={<Icon type="heart-on" />} value={4.3} />
    </div>
  );
}
```

### 尺寸

```jsx mdx:preview&bg=#fff
import React from 'react';
import { Rate, Divider, Icon } from 'uiw';

export default function Demo() {
  return (
    <div>
      <Rate style={{ fontSize: 34 }} character={<Icon type="heart-on" />} value={3} />
    </div>
  )
}
```

## Rate

| 参数 | 说明 | 类型 | 默认值 |
|--------- |-------- |--------- |-------- |
| value | 根据 value 进行比较，判断是否选中 | Number | - |
| count | star 总数 | Number | `5` |
| readOnly | 只读，无法进行交互 | Boolean | `false` |
| allowHalf | 是否允许半选 | Boolean | `false` |
| character | 自定义字符 | ReactNode | - |
| color | 自定义 Star 的颜色 | String | - |
| className | 自定义样式类名 | String | - |
| onChange | 数值改变时的回调，返回当前值 | Funtcion(e:Even,value) | - |
| onHoverChange | 鼠标经过时数值变化的回调 | Funtcion(e:Even,value) | - |

---
_Source: https://npm.io/package/@uiw/react-rate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
