• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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
16class Item{
17    id:number = 0;
18    label:string = '';
19
20    constructor(id:number, label:string){
21        this.id = id;
22        this.label = label;
23    }
24}
25
26@Component
27struct CustomRow {
28    item:Item;
29
30    build(){
31        Row(){
32            Text("label: ")
33            Text(this.item.label)
34            Text(", cap: ")
35            Text(this.item.label.toUpperCase())
36            Text(", rev: ")
37            Text(this.item.label.split("").reverse().join(""))
38        }
39    }
40}
41
42@Component
43struct CustomText {
44    item:Item;
45
46    build() {
47        Column() {
48            Text(this.item.label)
49        }
50    }
51}
52
53@Entry
54@Component
55struct ShufflingArrayContainer {
56    @State anArray:Item[] = [
57      new Item(1, "Text 1"),
58      new Item(2, "Text 2"),
59      new Item(3, "Text 3"),
60      new Item(4, "Text 4"),
61      new Item(5, "Text 5")
62    ];
63
64    build() {
65        Column(){
66            ForEach(this.anArray,
67                (item:Item) => {CustomRow({ item:item })},
68                (item:Item) => item.id.toString()
69            ) //ForEach
70            Button("Reverse anArray")
71            .width(500.0)
72            .height(150.0)
73            .onClick(() => {
74                // replace entire array
75                this.anArray = (this.anArray[0].id == 1) ?
76                    [
77                        new Item(10, "Text A"),
78                        new Item(20, "Text B"),
79                        new Item(30, "Text C"),
80                        new Item(40, "Text D"),
81                        new Item(50, "Text E")
82                    ] : [
83                        new Item(1, "Text 1"),
84                        new Item(2, "Text 2"),
85                        new Item(3, "Text 3"),
86                        new Item(4, "Text 4"),
87                        new Item(5, "Text 5")
88                    ];
89                console.log("click handler on ShufflingArrayContainer, this.anArray: " + JSON.stringify(this.anArray));
90            }) // click
91        } //Column
92    } //build
93}  //struct
94