• 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
16exports.source = `
17@Entry
18@Component
19struct MyComponent2 {
20    @State arr: number[] = [10, 20, 30]
21
22    build() {
23        Column() {
24            Button() {
25                Text('Reverse Array')
26            }.onClick(() => {
27                this.arr.reverse()
28            })
29
30            ForEach(this.arr,                         // Parameter 1: array to be iterated
31                    (item: number) => {               // Parameter 2: item generator
32                        Column() {
33                            Divider()
34                        }
35                    },
36                    (item: number) => item.toString() // Parameter 3: unique key generator, which is optional but recommended.
37            )
38        }
39    }
40}`
41
42exports.expectResult =
43`"use strict";
44class MyComponent2 extends View {
45    constructor(compilerAssignedUniqueChildId, parent, params) {
46        super(compilerAssignedUniqueChildId, parent);
47        this.__arr = new ObservedPropertyObject([10, 20, 30], this, "arr");
48        this.updateWithValueParams(params);
49    }
50    updateWithValueParams(params) {
51        if (params.arr !== undefined) {
52            this.arr = params.arr;
53        }
54    }
55    aboutToBeDeleted() {
56        this.__arr.aboutToBeDeleted();
57        SubscriberManager.Get().delete(this.id());
58    }
59    get arr() {
60        return this.__arr.get();
61    }
62    set arr(newValue) {
63        this.__arr.set(newValue);
64    }
65    render() {
66        Column.create();
67        Button.createWithChild();
68        Button.onClick(() => {
69            this.arr.reverse();
70        });
71        Text.create('Reverse Array');
72        Text.pop();
73        Button.pop();
74        ForEach.create("2", this, ObservedObject.GetRawObject(this.arr), // Parameter 1: array to be iterated
75        (item) => {
76            Column.create();
77            Divider.create();
78            Column.pop();
79        }, (item) => item.toString() // Parameter 3: unique key generator, which is optional but recommended.
80        );
81        ForEach.pop();
82        Column.pop();
83    }
84}
85loadDocument(new MyComponent2("1", undefined, {}));
86`
87