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// class without @Component convert tool will not process. 18class Month { 19 year: number = 2010; // why add assignment here, TS grammar required, if not it will omit by TSC. 20 month: number = 2; 21 days: number[] = [1, 2]; 22 23 constructor(year:number, month:number, days:number[]){ 24 this.year = year; 25 this.month = month; 26 this.days = days; 27 } 28} 29 30@Component 31@Entry 32struct Calendar { 33 34// simulate with 6 months 35@State calendar : Month[] = [ 36 new Month(2020, 1, [...Array(31).keys()]), 37 new Month(2020, 2, [...Array(28).keys()]), 38 new Month(2020, 3, [...Array(31).keys()]), 39 new Month(2020, 4, [...Array(30).keys()]), 40 new Month(2020, 5, [...Array(31).keys()]), 41 new Month(2020, 6, [...Array(30).keys()]) 42] 43 44 build() { 45 Column() { 46 Button() { 47 Text('next month') 48 }.onClick(() => { 49 this.calendar.shift() 50 this.calendar.push(new Month(2020, 7, [...Array(31).keys()])) 51 }) 52 53 ForEach(this.calendar, 54 (item: Month) => { 55 ForEach(item.days, 56 (day : number) => { 57 Text('day') 58 }, 59 (day : number) => day.toString() 60 ) // inner ForEach 61 }, 62 (item: Month) => (item.year * 12 + item.month).toString() // field is used together with year and month as the unique ID of the month. 63 ) // outer ForEach 64 } 65 } 66}` 67 68exports.expectResult = 69`"use strict"; 70// class without @Component convert tool will not process. 71class Month { 72 constructor(year, month, days) { 73 this.year = 2010; // why add assignment here, TS grammar required, if not it will omit by TSC. 74 this.month = 2; 75 this.days = [1, 2]; 76 this.year = year; 77 this.month = month; 78 this.days = days; 79 } 80} 81class Calendar extends View { 82 constructor(compilerAssignedUniqueChildId, parent, params) { 83 super(compilerAssignedUniqueChildId, parent); 84 this.__calendar = new ObservedPropertyObject([ 85 new Month(2020, 1, [...Array(31).keys()]), 86 new Month(2020, 2, [...Array(28).keys()]), 87 new Month(2020, 3, [...Array(31).keys()]), 88 new Month(2020, 4, [...Array(30).keys()]), 89 new Month(2020, 5, [...Array(31).keys()]), 90 new Month(2020, 6, [...Array(30).keys()]) 91 ], this, "calendar"); 92 this.updateWithValueParams(params); 93 } 94 updateWithValueParams(params) { 95 if (params.calendar !== undefined) { 96 this.calendar = params.calendar; 97 } 98 } 99 aboutToBeDeleted() { 100 this.__calendar.aboutToBeDeleted(); 101 SubscriberManager.Get().delete(this.id()); 102 } 103 get calendar() { 104 return this.__calendar.get(); 105 } 106 set calendar(newValue) { 107 this.__calendar.set(newValue); 108 } 109 render() { 110 Column.create(); 111 Button.createWithChild(); 112 Button.onClick(() => { 113 this.calendar.shift(); 114 this.calendar.push(new Month(2020, 7, [...Array(31).keys()])); 115 }); 116 Text.create('next month'); 117 Text.pop(); 118 Button.pop(); 119 ForEach.create("3", this, ObservedObject.GetRawObject(this.calendar), (item) => { 120 ForEach.create("2", this, ObservedObject.GetRawObject(item.days), (day) => { 121 Text.create('day'); 122 Text.pop(); 123 }, (day) => day.toString()); // inner ForEach 124 ForEach.pop(); 125 }, (item) => (item.year * 12 + item.month).toString() // field is used together with year and month as the unique ID of the month. 126 ); // outer ForEach 127 ForEach.pop(); 128 Column.pop(); 129 } 130} 131loadDocument(new Calendar("1", undefined, {})); 132` 133