• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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
17class Model {
18  value: string
19
20  constructor(value: string) {
21    this.value = value
22  }
23}
24
25@Entry
26@Component
27struct EntryComponent05 {
28  build() {
29    Column() {
30      MyComponent05({ count: 1, increaseBy: 2 })
31      MyComponent05({ title: { value: 'Hello World 2' }, count: 7 })
32    }
33  }
34}
35
36@Component
37struct MyComponent05 {
38  @State title: Model = { value: 'Hello World' }
39  @State count: number = 0
40  private toggle: string = 'Hello World'
41  private increaseBy: number = 1
42
43  build() {
44    Column() {
45      Button('Click to change title')
46        .margin(20)
47        .onClick(() => {
48          this.title.value = (this.toggle == this.title.value) ? 'Hello World' : 'Hello ArkUI'
49        })
50    }
51  }
52}
53
54
55@Entry
56@Component
57struct ParentComponent {
58  @State countDownStartValue: number = 10
59
60  build() {
61    Column() {
62      Button('+1 - Nuggets in New Game')
63        .margin(15)
64        .onClick(() => {
65          this.countDownStartValue += 1
66        })
67
68      Button('-1  - Nuggets in New Game')
69        .margin(15)
70        .onClick(() => {
71          this.countDownStartValue -= 1
72        })
73      CountDownComponent({ count: this.countDownStartValue, costOfOneAttempt: 2 })
74    }
75  }
76}
77
78@Component
79struct CountDownComponent {
80  @Prop count: number
81  private costOfOneAttempt: number
82
83  build() {
84    Column() {
85      Button('count - costOfOneAttempt')
86        .margin(15)
87        .onClick(() => {
88          this.count -= this.costOfOneAttempt
89        })
90    }
91  }
92}
93
94
95@Entry
96@Component
97struct Player {
98  @State isPlaying: boolean = false
99
100  build() {
101    Column() {
102      Text(`Player is playing`).fontSize(18)
103      Button('Parent:' + this.isPlaying)
104        .margin(15)
105        .onClick(() => {
106          this.isPlaying = !this.isPlaying
107        })
108    }
109  }
110}
111
112@Component
113struct PlayButton {
114  @Link buttonPlaying: boolean
115
116  build() {
117    Column() {
118      Button(this.buttonPlaying ? 'pause' : 'play')
119        .margin(20)
120        .onClick(() => {
121          this.buttonPlaying = !this.buttonPlaying
122        })
123    }
124  }
125}
126
127
128@Entry
129@Component
130struct Parent {
131  @State arr: number[] = [1, 2, 3]
132
133  build() {
134    Column() {
135      Button('Parent Button: splice')
136        .margin(10)
137        .onClick(() => {
138          this.arr.splice(0, 1, 60)
139        })
140      ForEach(this.arr, item => {
141        Text(item.toString()).fontSize(18).margin(10)
142      }, item => item.toString())
143    }
144  }
145}
146
147
148@Component
149struct Child {
150  @Link items: number[]
151
152  build() {
153    Column() {
154      Button('Child Button1: push')
155        .margin(15)
156        .onClick(() => {
157          this.items.push(100)
158        })
159      Button('Child Button2: replace whole item')
160        .margin(15)
161        .onClick(() => {
162          this.items = [100, 200, 300]
163        })
164    }
165  }
166}
167
168
169@Entry
170@Component
171struct ParentView {
172  @State counter: number = 0
173
174  build() {
175    Column() {
176      ChildA({ counterVal: this.counter })
177    }
178  }
179}
180
181@Component
182struct ChildA {
183  @Prop counterVal: number
184
185  build() {
186  }
187}
188
189@Component
190struct ChildB {
191  @Link counterRef: number
192
193  build() {
194  }
195}
196
197
198var nextID: number = 0
199
200@Observed
201class ClassA05 {
202  public name: string
203  public c: number
204  public id: number
205
206  constructor(c: number, name: string = 'OK') {
207    this.name = name
208    this.c = c
209    this.id = nextID++
210  }
211}
212
213@Component
214struct ViewA {
215  label: string = 'ViewA1'
216  @ObjectLink a: ClassA05
217
218  build() {
219    Row() {
220    }.margin({ top: 10 })
221  }
222}
223
224@Entry
225@Component
226struct ViewB {
227  @State arrA: ClassA05[] = [new ClassA05(0), new ClassA05(0)]
228
229  build() {
230    Column() {
231      ForEach(this.arrA, (item) => {
232      }, (item) => item.id.toString())
233      ViewA({ label: `this.arrA[first]`, a: this.arrA[0] })
234      ViewA({ label: `this.arrA[last]`, a: this.arrA[this.arrA.length - 1] })
235
236      Button(`ViewB: reset array`)
237        .margin({ top: 10 })
238        .onClick(() => {
239          this.arrA = [new ClassA05(0), new ClassA05(0)]
240        })
241      Button(`ViewB: push`)
242        .margin({ top: 10 })
243        .onClick(() => {
244          this.arrA.push(new ClassA05(0))
245        })
246      Button(`ViewB: shift`)
247        .margin({ top: 10 })
248        .onClick(() => {
249          this.arrA.shift()
250        })
251    }.width('100%')
252  }
253}
254
255
256@Entry
257@Component
258struct CompA {
259  @Provide("reviewVote") reviewVotes: number = 0;
260
261  build() {
262    Column() {
263      CompB()
264    }
265  }
266}
267
268@Component
269struct CompB {
270  build() {
271    Column() {
272      CompC()
273    }
274  }
275}
276
277@Component
278struct CompC {
279  @Consume("reviewVote") reviewVotes: number
280
281  build() {
282    Column() {
283    }.width('100%')
284  }
285}
286
287@Entry
288@Component
289struct CompA02 {
290  @State @Watch('onBasketUpdated') shopBasket: Array<number> = [7, 12, 47, 3]
291  @State totalPurchase: number = 0
292  @State addPurchase: number = 0
293
294  aboutToAppear() {
295    this.updateTotal()
296  }
297
298  updateTotal(): number {
299    let sum = 0;
300    this.shopBasket.forEach((i) => {
301      sum += i
302    })
303    this.totalPurchase = (sum < 100) ? sum : 0.9 * sum
304    return this.totalPurchase
305  }
306
307  onBasketUpdated(propName: string): void {
308    this.updateTotal()
309  }
310
311  build() {
312    Column() {
313      Button('add to basket ' + this.addPurchase)
314        .margin(15)
315        .onClick(() => {
316          this.addPurchase = Math.round(100 * Math.random())
317          this.shopBasket.push(this.addPurchase)
318        })
319    }
320  }
321}