ts-universal-attributes-popup.md 5.3 KB
Newer Older
Z
zengyawen 已提交
1
# Popup Control
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
5 6 7 8
> This attribute is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.


## Required Permissions
Z
zengyawen 已提交
9 10 11 12

None


Z
zengyawen 已提交
13
## Attributes
Z
zengyawen 已提交
14

Z
zengyawen 已提交
15

E
ester.zhou 已提交
16
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
17
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
18
| bindPopup | show: boolean,<br/>popup: PopupOptions \| CustomPopupOptions | - | Settings of the popup bound to a component.<br/>**show**: whether to display the popup on the creation page by default. The default value is **false**.<br/>**popup**: parameters of the current popup. |
Z
zengyawen 已提交
19 20


E
ester.zhou 已提交
21 22
- PopupOptions attributes
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
23
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
24 25 26 27 28
  | message | string | Yes | - | Content of the popup message. |
  | placementOnTop | boolean | No | false | Whether to display the popup above the component. The default value is **false**. |
  | primaryButton | {<br/>value: string,<br/>action: () =&gt; void<br/>} | No | - | First button.<br/>**value**: text of the primary button in the popup.<br/>**action**: callback function for clicking the primary button. |
  | secondaryButton | {<br/>value: string,<br/>action: () =&gt; void<br/>} | No | - | Second button.<br/>**value**: text of the secondary button in the popup.<br/>**action**: callback function for clicking the secondary button. |
  | onStateChange | (isVisible: boolean) =&gt; void | No | - | Callback for the popup status change event. The parameter **isVisible** indicates the visibility of the popup. |
Z
zengyawen 已提交
29

E
ester.zhou 已提交
30
- CustomPopupOptions<sup>8+</sup>
E
esterzhou 已提交
31
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
32
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
33 34 35 36 37 38 39
  | builder | () =&gt; any | Yes | - | Builder of the tooltip content. |
  | placement | Placement | No | Placement.Bottom | Preferred position of the tooltip component. If the set position is insufficient for holding the component, it will be automatically adjusted. |
  | maskColor | [Color](../../ui/ts-types.md) | No | - | Color of the tooltip mask. |
  | popupColor | [Color](../../ui/ts-types.md) | No | - | Color of the tooltip. |
  | enableArrow | boolean | No | true | Whether to display arrows. Arrows are displayed only for tooltips in the up and down directions. |
  | autoCancel | boolean | No | true | Whether to automatically close the tooltip when an operation is performed on the page. |
  | onStateChange | (isVisible: boolean) =&gt; void | No | - | Callback for the popup status change event. The parameter **isVisible** indicates the visibility of the popup. |
Z
zengyawen 已提交
40

Z
zengyawen 已提交
41
- Placement<sup>8+</sup> enums
E
ester.zhou 已提交
42
    | Name | Description |
Z
zengyawen 已提交
43
  | -------- | -------- |
E
ester.zhou 已提交
44 45 46 47 48 49 50 51
  | Left | The tooltip is on the left of the component. |
  | Right | The tooltip is on the right of the component. |
  | Top | The tooltip is on the top of the component. |
  | Bottom | The tooltip is at the bottom of the component. |
  | TopLeft | The tooltip is in the upper left corner of the component. |
  | TopRight | The tooltip is in the upper right corner of the component. |
  | BottomLeft | The tooltip is in the lower left corner of the component. |
  | BottomRight | The tooltip is in the lower right corner of the component. |
Z
zengyawen 已提交
52 53


Z
zengyawen 已提交
54 55
## Example

Z
zengyawen 已提交
56 57 58 59 60 61 62

```
@Entry
@Component
struct PopupExample {
  @State noHandlePopup: boolean = false
  @State handlePopup: boolean = false
Z
zengyawen 已提交
63 64 65 66 67 68 69 70
  @State customPopup: boolean = false

  @Builder popupBuilder() {
    Row({ space: 2 }) {
      Image('/resource/ic_public_thumbsup.svg').width(24).height(24).margin({ left: -5 })
      Text('Custom Popup').fontSize(12)
    }.width(100).height(50).backgroundColor(Color.White)
  }
Z
zengyawen 已提交
71 72

  build() {
Z
zengyawen 已提交
73
    Flex({ direction: FlexDirection.Column }) {
Z
zengyawen 已提交
74 75 76 77 78
      Button('no handle popup')
        .onClick(() => {
          this.noHandlePopup = !this.noHandlePopup
        })
        .bindPopup(this.noHandlePopup, {
Z
zengyawen 已提交
79
          message: 'content1 content1',
Z
zengyawen 已提交
80 81 82 83 84 85 86
          placementOnTop: false,
          onStateChange: (e) => {
            console.info(e.isVisible.toString())
            if (!e.isVisible) {
              this.noHandlePopup = false
            }
          }
Z
zengyawen 已提交
87 88
        })
        .position({ x: 100, y: 50 })
Z
zengyawen 已提交
89 90 91 92 93 94

      Button('with handle popup')
        .onClick(() => {
          this.handlePopup = !this.handlePopup
        })
        .bindPopup(this.handlePopup, {
Z
zengyawen 已提交
95
          message: 'content2 content2',
Z
zengyawen 已提交
96
          placementOnTop: true,
Z
zengyawen 已提交
97
          primaryButton: {
Z
zengyawen 已提交
98 99 100 101 102 103 104 105 106 107
            value: 'ok',
            action: () => {
              this.handlePopup = !this.handlePopup
              console.info('secondaryButton click')
            }
          },
          onStateChange: (e) => {
            console.info(e.isVisible.toString())
          }
        })
Z
zengyawen 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        .position({ x: 100, y: 200 })

      Button('custom popup')
        .onClick(() => {
          this.customPopup = !this.customPopup
        })
        .bindPopup(this.customPopup, {
          builder: this.popupBuilder,
          placement: Placement.Bottom,
          maskColor: 0x33000000,
          popupColor: Color.White,
          enableArrow: true,
          onStateChange: (e) => {
            if (!e.isVisible) {
              this.customPopup = false
            }
          }
        })
        .position({ x: 100, y: 350 })
Z
zengyawen 已提交
127 128 129 130 131
    }.width('100%').padding({ top: 5 })
  }
}
```

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