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

E
ester.zhou 已提交
3
The **\<Rating>** component provides a rating bar.
Z
zengyawen 已提交
4

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

Z
zengyawen 已提交
9 10

## Child Components
Z
zengyawen 已提交
11

E
ester.zhou 已提交
12
Not supported
Z
zengyawen 已提交
13

Z
zengyawen 已提交
14 15 16 17 18

## APIs

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

E
ester.zhou 已提交
19 20 21 22 23 24
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| rating | number | Yes| Value to rate.<br>Default value: **0**|
| indicator | boolean | No| Used only as an indicator and cannot be operated.<br>Default value: **false**|
Z
zengyawen 已提交
25 26 27 28


## Attributes

E
ester.zhou 已提交
29 30 31 32 33
| Name| Type| Description|
| -------- | -------- | -------- |
| stars | number | Total number of stars.<br>Default value: **5**|
| stepSize | number | Step of an operation.<br>Default value: **0.5**|
| starStyle | {<br>backgroundUri: string,<br>foregroundUri: string,<br>secondaryUri?: string<br>} | **backgroundUri**: image link of the unselected star. You can use the default image or a custom local image.<br>**foregroundUri**: image path of the selected star. You can use the default image or a custom local image.<br>**secondaryUir**: image path of the partially selected star. You can use the default image or a custom local image.|
Z
zengyawen 已提交
34 35 36 37


## Events

E
ester.zhou 已提交
38
| Name| Description|
Z
zengyawen 已提交
39
| -------- | -------- |
E
ester.zhou 已提交
40
| onChange(callback:(value: number) =&gt; void) | Triggered when the rating value changes.|
Z
zengyawen 已提交
41 42 43 44


## Example

E
ester.zhou 已提交
45 46
```ts
// xxx.ets
Z
zengyawen 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
@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 已提交
67
![en-us_image_0000001257058423](figures/en-us_image_0000001257058423.gif)