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 16// class without @Component convert tool will not process. 17class Month { 18 year: number = 2010; // why add assignment here, TS grammar required, if not it will omit by TSC. 19 month: number = 2; 20 days: number[] = [1, 2]; 21 22 constructor(year:number, month:number, days:number[]){ 23 this.year = year; 24 this.month = month; 25 this.days = days; 26 } 27} 28 29@Component 30@Entry 31struct CalendarDemo { 32 33// simulate with 6 months 34@State calendar : Month[] = [ 35 new Month(2020, 1, [...Array(31).keys()]), 36 new Month(2020, 2, [...Array(28).keys()]), 37 new Month(2020, 3, [...Array(31).keys()]) 38] 39 40 build() { 41 Column() { 42 Button() { 43 Text('next month') 44 }.onClick(() => { 45 this.calendar.shift() 46 this.calendar.push(new Month(2020, 4, [...Array(30).keys()])) 47 }) 48 49 ForEach(this.calendar, 50 (item: Month) => { 51 Row() { 52 ForEach(item.days, 53 (day : number) => { 54 Text(`${day}`) 55 }, 56 (day : number) => day.toString()) // inner ForEach 57 } 58 }, 59 (item: Month) => (item.year * 12 + item.month).toString()) // outer ForEach 60 } 61 } 62} 63