@uiw/react-rate
TypeScript icon, indicating that this package has built-in type declarations

4.22.3 • Public • Published

Rate 评分

Buy me a coffee Open in unpkg NPM Downloads npm version

评分组件

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

基本用法

按钮样式的单选组合。

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 /> 表单中应用 <Rate /> 组件

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>
  )
}

受控

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>
  )
}

只读

按钮样式的单选组合。

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>
  );
}

文本

按钮样式的单选组合。

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>
  );
}

半选

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>
  );
}

尺寸

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) -

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.22.3
100latest
5.0.0-bate.2.10bate

Version History

VersionDownloads (Last 7 Days)Published
4.22.3
100
4.22.2
0
4.22.1
0
4.22.022
4.21.284
4.21.270
4.21.260
4.21.250
5.0.0-bate.2.10
4.21.240
5.0.0-bate.2.00
4.21.230
4.21.220
4.21.210
5.0.0-bate-190
4.21.200
5.0.0-bate-180
5.0.0-bate-170
5.0.0-bate-140
5.0.0-bate-120
5.0.0-bate-110
5.0.0-bate-100
5.0.0-bate-90
5.0.0-bate-80
5.0.0-bate-70
5.0.0-bate-60
4.21.190
4.21.180
5.0.0-bate-50
5.0.0-bate-40
5.0.0-bate-30
4.21.170
5.0.0-bate-20
5.0.0-bate-10
4.21.160
4.21.150
5.0.0-bate-01
4.21.140
4.21.130
4.21.120
4.21.110
4.21.100
4.21.90
4.21.80
4.21.70
4.21.60
4.21.50
4.21.40
4.21.30
4.21.20
4.21.10
4.21.00
4.20.00
4.19.00
4.18.10
4.18.00
4.17.00
4.16.20
4.16.10
4.16.00
4.15.10
4.15.00
4.14.20
4.14.10
4.14.00
4.13.120
4.13.110
4.13.100
4.13.90
4.13.80
4.13.70
4.13.60
4.13.50
4.13.40
4.13.30
4.13.20
4.13.10
4.13.00
4.12.20
4.12.10
4.12.00
4.11.60
4.11.50
4.11.40
4.11.30
4.11.20
4.11.10
4.11.00
4.10.70
4.10.60
4.10.50
4.10.40
4.10.38
4.10.20
4.10.10
4.10.00
4.9.110
4.9.100
4.9.90
4.9.70
4.9.60
4.9.40
4.9.30
4.9.20
4.9.10
4.9.00
4.8.90
4.8.80
4.8.70
4.8.60
4.8.50
4.8.40
4.8.30
4.8.20
4.8.10
4.8.00
4.7.160
4.7.150
4.7.140
4.7.130
4.7.120
4.7.110
4.7.100
4.7.90
4.7.80
4.7.70
4.7.60
4.7.50
4.7.40
4.7.30
4.7.20
4.7.00
4.6.190
4.6.180
4.6.170
4.6.160
4.6.150
4.6.140
4.6.130
4.6.120
4.6.111
4.6.101
4.6.90
4.6.80
4.6.70
4.6.60
4.6.40
4.6.30
4.6.20
4.6.10
4.6.00
4.5.00
4.4.10
4.4.00
4.3.10
4.3.00
4.2.140
4.2.50
4.2.30
4.2.10
4.1.30
4.1.20
4.1.10
4.1.00
4.0.00
4.0.0-alpha.80
4.0.0-alpha.70
4.0.0-alpha.60
4.0.0-alpha.30
4.0.0-alpha.20
4.0.0-alpha.10
4.0.0-alpha.00

Package Sidebar

Install

npm i @uiw/react-rate

Weekly Downloads

137

Version

4.22.3

License

MIT

Unpacked Size

22.7 kB

Total Files

12

Last publish

Collaborators

  • uiwjs
  • wcjiang