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

E
esterzhou 已提交
3
A menu – a vertical list of items – can be bound to a component and displayed by long-pressing, clicking, or right-clicking the component.
4

E
esterzhou 已提交
5 6 7
> **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 已提交
8

E
esterzhou 已提交
9 10

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

None

E
esterzhou 已提交
14 15 16 17

## Attributes


18 19
| Name                          | Type                                    | Default Value  | Description                                |
| ---------------------------- | ---------------------------------------- | ---- | ---------------------------------- |
E
esterzhou 已提交
20 21
| 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](ts-appendix-enums.md#responsetype8) | -    | 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 已提交
22 23 24


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


## Example

#### Menu with Textual Menu Items
Z
zengyawen 已提交
34

35 36
```ts
// xxx.ets
Z
zengyawen 已提交
37 38
@Entry
@Component
Z
zengyawen 已提交
39
struct MenuExample {
Z
zengyawen 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  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 已提交
64 65 66
![en_image_0000001174582862](figures/en_image_0000001174582862.gif)

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

68 69
```ts
// xxx.ets
Z
zengyawen 已提交
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

119 120
```ts
// xxx.ets
E
esterzhou 已提交
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 149
@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)
  }
}
```