ts-universal-attributes-menu.md 3.4 KB
Newer Older
E
esterzhou 已提交
1
# Menu Control
Z
zengyawen 已提交
2

E
esterzhou 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
5

E
esterzhou 已提交
6 7

## Required Permissions
Z
zengyawen 已提交
8 9 10

None

E
esterzhou 已提交
11 12 13 14 15 16

## Attributes


| Name| Type| Default Value | Description|
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
17 18
| bindMenu | Array<MenuItem&gt; \| [CustomBuilder](../../ui/ts-types.md)<sup>8+</sup> | - | Menu bound to the component, which is displayed when you click the component. Textual and custom menu items are supported.|
| bindContextMenu<sup>8+</sup> | content: [CustomBuilder](../../ui/ts-types.md)<br>responseType: ResponseType | - | Context menu bound to the component, which is displayed when you long-press or right-click the component. Only custom menu items are supported.|
E
esterzhou 已提交
19 20 21 22 23 24


- MenuItem
  | Name| Type| Description|
  | -------- | -------- | -------- |
  | value | string | Menu item text.|
E
esterzhou 已提交
25
  | action | () =&gt; void | Action triggered when a menu item is clicked.|
E
esterzhou 已提交
26 27 28 29 30 31 32 33 34 35

- ResponseType<sup>8+</sup>
  | Value| Description|
  | -------- | -------- |
  | LongPress | The menu is displayed when the component is long-pressed.   |
  | RightClick | The menu is displayed when the component is right-clicked.|

## Example

#### Menu with Textual Menu Items
Z
zengyawen 已提交
36 37 38 39

```
@Entry
@Component
Z
zengyawen 已提交
40
struct MenuExample {
Z
zengyawen 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  build() {
    Column() {
      Text('click for Menu')
    }
    .width('100%')
    .margin({ top: 5 })
    .bindMenu([
      {
        value: 'Menu1',
        action: () => {
          console.info('handle Menu1 select')
        }
      },
      {
        value: 'Menu2',
        action: () => {
          console.info('handle Menu2 select')
        }
      },
    ])
  }
}
```

E
esterzhou 已提交
65 66 67
![en_image_0000001174582862](figures/en_image_0000001174582862.gif)

#### Menu with Custom Menu Items
Z
zengyawen 已提交
68

Z
zengyawen 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
```
import router from '@system.router';

@Entry
@Component
struct MenuExample {
  @Builder MenuBuilder() {
    Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
      Text('text1')
        .fontSize(20)
        .width(100)
        .height(50)
        .textAlign(TextAlign.Center)

      Divider().height(10)

      Text('text2')
        .fontSize(20)
        .width(100)
        .height(50)
        .textAlign(TextAlign.Center)

      Divider().height(10)

      Button('Next')
        .fontSize(20)
        .width(100)
        .height(50)
        .onClick(() => {
          router.push({ uri: 'pages/details' })
        })

    }.width(100)
  }

  build() {
    Column() {
      Text('click for menu')
    }
    .width('100%')
    .margin({ top: 5 })
    .bindMenu(this.MenuBuilder)
  }
}
```

E
esterzhou 已提交
115 116 117
![en_image_0000001186807708](figures/en_image_0000001186807708.gif)

#### Context Menu (Displayed Upon Right-Clicking)
Z
zengyawen 已提交
118

E
esterzhou 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
```
@Entry
@Component
struct ContextMenuExample {
  @Builder MenuBuilder() {
    Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
      Text('Test menu item 1')
        .fontSize(20)
        .width(100)
        .height(50)
        .textAlign(TextAlign.Center)
      Divider().height(10)
      Text('Test menu item 2')
        .fontSize(20)
        .width(100)
        .height(50)
        .textAlign(TextAlign.Center)
    }.width(100)
  }
  
  build() {
    Column() {
      Text('rightclick for menu')
    }
    .width('100%')
    .margin({ top: 5 })
    .bindContextMenu(this.MenuBuilder, ResponseType.RightClick)
  }
}
```