ts-basic-components-rating.md 2.1 KB
Newer Older
Z
zengyawen 已提交
1
# Rating
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

4
> **NOTE**<br>
Z
zengyawen 已提交
5
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

Z
zengyawen 已提交
7 8 9 10 11

The **&lt;Rating&gt;** component provides a rating bar.


## Required Permissions
Z
zengyawen 已提交
12 13 14

None

Z
zengyawen 已提交
15 16

## Child Components
Z
zengyawen 已提交
17 18 19

None

Z
zengyawen 已提交
20 21 22 23 24 25

## APIs

Rating(options?: { rating: number, indicator?: boolean })

- Parameters
26
  | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
27
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
28 29
  | rating | number | Yes | 0 | Value to rate. | 
  | indicator | boolean | No | false | Used only as an indicator and cannot be operated. | 
Z
zengyawen 已提交
30 31 32 33


## Attributes

34
| Name | Type | Default Value | Description | 
Z
zengyawen 已提交
35
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
36 37 38
| stars | number | 5 | Total number of stars. | 
| stepSize | number | 0.5 | Step of an operation. | 
| starStyle | {<br/>backgroundUri: string,<br/>foregroundUri: string,<br/>secondaryUri?: string<br/>} | - | **backgroundSrc**: image link of the unselected star. You can customize the image or use the default image. Only local images are supported.<br/>**foregroundSrc**: image path of the selected star. You can customize the image or use the default image. Only local images are supported.<br/>**secondarySrc**: image path of the partially selected star. You can customize the image or use the default image. Only local images are supported. | 
Z
zengyawen 已提交
39 40 41 42


## Events

43
| Name | Description | 
Z
zengyawen 已提交
44
| -------- | -------- |
E
esterzhou 已提交
45
| onChange(callback:(value: number) =&gt; void) | Triggered when the rating value changes. | 
Z
zengyawen 已提交
46 47 48 49


## Example

Z
zengyawen 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

```
@Entry
@Component
struct RatingExample {
  @State rating: number = 1
  @State indicator: boolean = false

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
      Text('current score is ' + this.rating).fontSize(20)
      Rating({ rating: this.rating, indicator: this.indicator })
        .stars(5)
        .stepSize(0.5)
        .onChange((value: number) => {
          this.rating = value
        })
    }.width(350).height(200).padding(35)
  }
}
```

Z
zengyawen 已提交
72
![en-us_image_0000001257058423](figures/en-us_image_0000001257058423.gif)