idlize

Форк
0
298 строк · 4.5 Кб
1
/*
2
 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15

16

17
// Bare empty struct
18

19
@Entry
20
@Component
21
export struct EntryExample {
22
    build(){ }
23
}
24

25
@Component
26
struct ComponentExample {
27
    build(){ }
28
}
29

30

31
// A builder call in the build() function
32

33
@Component
34
struct BuildExample {
35
    build(){
36
        Text("message")
37
            .fontColor(Color.Red)
38
            .width(100)
39
    }
40
}
41

42
// Simple state management annotations
43

44
@Component
45
struct StateExample {
46
    @State x: string = "hello"
47
    build(){
48
        Text(this.x)
49
    }
50
}
51

52
@Component
53
struct LinkExample {
54
    @Link x: string
55
    build(){
56
        Text(this.x)
57
    }
58
}
59

60
@Component
61
struct PropExample {
62
    @Prop x: string
63
    build(){
64
        Text(this.x)
65
    }
66
}
67

68
@Component
69
struct PropInitializedExample {
70
    @Prop x: string = "init"
71
    build(){
72
        Text(this.x)
73
    }
74
}
75

76
@Component
77
struct ProvideExample {
78
    @Provide("name") x: string = "text"
79
    build(){
80
        Text(this.x)
81
    }
82
}
83

84
@Component
85
struct ConsumeExample {
86
    @Consume("name") x: string
87
    build(){
88
        Text(this.x)
89
    }
90
}
91

92
// Builder functions
93

94
@Component
95
struct BuilderExample {
96
    @Builder foo() {
97
        Text("hello")
98
    }
99
    build() {
100
        this.foo()
101
    }
102
}
103

104
@Builder
105
function bar() {
106
    Text("hello")
107
}
108

109
@Component
110
struct GlobalBuilderExample {
111
    build() {
112
        bar()
113
    }
114
}
115

116
@Component
117
struct BuilderParamExample {
118
    @BuilderParam foo: () => {}
119
    build() {
120
        this.foo()
121
    }
122
}
123

124
// @Styles @Extend @AnimatableExtend
125

126
@Styles
127
function looks() {
128
    .height(500)
129
    .width(400)
130
    .backgroundColor(Color.Gray)
131
}
132

133
@Component
134
struct StylesExample {
135
    build() {
136
        Text()
137
            .width(17)
138
            .looks()
139
    }
140
}
141

142
@Component
143
struct StylesMethodExample {
144
    @Styles
145
    nice() {
146
        .height(500)
147
        .width(400)
148
        .backgroundColor(Color.Gray)
149
    }
150

151
    build() {
152
        Text()
153
            .width(17)
154
            .nice()
155
    }
156
}
157

158

159
@Extend(Column)
160
function clown() {
161
    .height(500)
162
    .width(400)
163
    .backgroundColor(Color.Gray)
164
}
165

166
@Component
167
struct ExtendExample {
168
    build() {
169
        Column()
170
            .width(17)
171
            .clown()
172
    }
173
}
174

175
@AnimatableExtend(Text)
176
function attributeExtend(n: number, unused: any) {
177
  .fontSize(n)
178
}
179

180
@Component
181
struct AnimatableExtendExample {
182
    build() {
183
        Text()
184
            .width(17)
185
            .attributeExtend(50, "unused")
186
    }
187
}
188

189

190
// Advanced state management
191

192
@Component
193
struct WatchExample {
194
    @State @Watch("watchFunction") x: string = "hello"
195
    watchFunction() {
196
        console.log("Watch function")
197
    }
198
    build() {
199
    }
200
}
201

202
@Component
203
struct StorageLinkExample {
204
    @StorageLink("storage") link: string = "Start"
205
    build() {
206
    }
207
}
208

209
@Component
210
struct StoragePropExample {
211
    @StorageProp("storage") prop: string = "Start"
212
    build() {
213
    }
214
}
215

216
// CustomDialog
217

218
@CustomDialog
219
struct CustomDialogExample {
220
  controller: CustomDialogController
221
  build() {
222
  }
223
}
224

225
@Component
226
export struct CustomDialogControllerExample {
227
  dialogController: CustomDialogController = new CustomDialogController({
228
    builder: CustomDialogExample(),
229
    autoCancel: true,
230
    alignment: DialogAlignment.Default,
231
    offset: { dx: 0, dy: 0 },
232
    gridCount: 4,
233
    customStyle: false
234
  })
235

236
  aboutToAppear() {
237
    this.dialogController.open()
238
  }
239

240
  aboutToDisappear() {
241
      this.dialogController.close()
242
  }
243

244
  build() {
245
  }
246
}
247

248
// ObjectLink and Observed
249

250
@Observed
251
class ObservedExample {
252
  public c: number
253
  constructor(c: number) {
254
    this.c = c
255
  }
256
}
257

258
@Component
259
struct ObjectLinkExample {
260
  @ObjectLink a: ObservedExample
261

262
  build() {
263
    Button()
264
      .onClick(() => {
265
          this.a.c += 1
266
    })
267
  }
268
}
269

270
@Component
271
struct ObjectLinkExampleParent {
272
  @State a: ObservedExample[] = [new ObservedExample(0), new ObservedExample(0)]
273

274
  build() {
275
    ObjectLinkExample()
276
  }
277
}
278

279
@Component
280
struct PlainPropertyExample {
281
    field = 17
282
}
283

284
@Component
285
struct CallExample {
286
    @State
287
    state: number = 17
288
    build() {
289
        Child({counter: this.$state})
290
    }
291
}
292

293
@Component
294
struct Child {
295
    @Link
296
    counter: number
297
    build() {}
298
}
299

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.