diff --git a/arkoala-arkts/user/src/ets/tests/test01.ets b/arkoala-arkts/user/src/ets/tests/test01.ets new file mode 100644 index 0000000000000000000000000000000000000000..46f16dbd9509dcec39703e7aa9e994fcc44e0946 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test01.ets @@ -0,0 +1,30 @@ +@Component +struct EntryComponent1 { + @Prop prop : number = 2; + build() { + Column() { + Button(`${this.prop}`).onClick(()=>{ + this.prop += 4; // only received once, change prop, will not synchronized to its Child. + }) + MyComponent1({state : this.prop}) + } + } +} + +@Component +struct MyComponent1 { + @State count: Resource = $r('sys.color.ohos_id_color_emphasize') + @State state : number = 1; + build() { + Column() { + Text(`Hello ${this.state}`) + .fontColor(this.count) + Button('change') + .onClick(() => { + this.count = $r('sys.media.ohos_user_auth_icon_face'); + }) + } + } +} + + diff --git a/arkoala-arkts/user/src/ets/tests/test02.ets b/arkoala-arkts/user/src/ets/tests/test02.ets new file mode 100644 index 0000000000000000000000000000000000000000..335171a3539683429a637c92aca1e4e430a133f4 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test02.ets @@ -0,0 +1,66 @@ +class Person2 { + public value: string; + + constructor(value: string) { + this.value = value; + } +} + +class Model2 { + public value: string; + public name: Person2; + constructor(value: string, person: Person2) { + this.value = value; + this.name = person; + } +} + +@Entry +@Component +struct Page2 { + // class类型 + @State @Watch("onTitleChange") title: Model2 = new Model2('Hello', new Person2('World')); + onTitleChange() { + console.log(`observe the object and object property change`) + } + + build() { + Column() { + Text(`assign a object: ${JSON.stringify(this.title)}`) + .id('Page2HelloWorld') + .fontSize(40) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }).backgroundColor(Color.Green) + .onClick(()=>{ + this.title = new Model2('Hi', new Person2('ArkUI')); // assign the object to @State variable + }) + Text(`assign a object property: ${this.title.value}`) + .id('Page2HelloWorld') + .fontSize(40) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }).backgroundColor(Color.Red) + .onClick(()=>{ + this.title.value = 'Hi'; // assign the object property + }) + Text(`assign a object second layer property: ${this.title.name.value}`) + .id('Page2HelloWorld') + .fontSize(40) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }).backgroundColor(Color.Blue) + .onClick(()=>{ + this.title.name.value = 'ArkUI'; // can not observe properties of second-level objects + }) + } + .height('100%') + .width('100%') + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test03.ets b/arkoala-arkts/user/src/ets/tests/test03.ets new file mode 100644 index 0000000000000000000000000000000000000000..c7a494fe4ea95ee3b5f6da3eaa7974b9dd2b4cfa --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test03.ets @@ -0,0 +1,73 @@ +class Person3 { + public value: string; + + constructor(value: string) { + this.value = value; + } +} + +class Model3 { + public value: string; + public name: Person3; + constructor(value: string, person: Person3) { + this.value = value; + this.name = person; + } +} + +@Entry +@Component +struct Page3 { + // 数组类型 + @State @Watch("onTitleChange") title: Model3[] = [new Model3("11", new Person3("11")), new Model3("22", new Person3("22"))]; + onTitleChange() { + console.log(`observe the object and object property change`) + } + + build() { + Column() { + Text(`assign an array: ${JSON.stringify(this.title)}`) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }).backgroundColor(Color.Green) + .onClick(()=>{ + this.title = [new Model3("33", new Person3("33"))]; // Array assignment + }) + Text(`assign item: ${this.title[0].value}`) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }).backgroundColor(Color.Red) + .onClick(()=>{ + this.title[0] = new Model3("44", new Person3("44")); // Array item assignment + }) + Text(`delete array items: ${JSON.stringify(this.title)}`) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }).backgroundColor(Color.Gray) + .onClick(()=>{ + this.title.pop(); // + }) + Text(`Added array items: ${JSON.stringify(this.title)}`) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }).backgroundColor(Color.Yellow) + .onClick(()=>{ + this.title.push(new Model3("12", new Person3("12"))); + }) + } + .height('100%') + .width('100%') + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test04.ets b/arkoala-arkts/user/src/ets/tests/test04.ets new file mode 100644 index 0000000000000000000000000000000000000000..d3ea0530949d167037ba191db4aa0682605a02bb --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test04.ets @@ -0,0 +1,35 @@ +@Entry +@Component +struct DatePickerExample { + @State selectedDate: Date = new Date('2021-08-08'); + + build() { + Column() { + Button('set selectedDate to 2023-07-08') + .margin(10) + .onClick(() => { + this.selectedDate = new Date('2023-07-08'); + }) + Button('increase the year by 1') + .margin(10) + .onClick(() => { + this.selectedDate.setFullYear(this.selectedDate.getFullYear() + 1); + }) + Button('increase the month by 1') + .margin(10) + .onClick(() => { + this.selectedDate.setMonth(this.selectedDate.getMonth() + 1); + }) + Button('increase the day by 1') + .margin(10) + .onClick(() => { + this.selectedDate.setDate(this.selectedDate.getDate() + 1); + }) + DatePicker({ + start: new Date('1970-1-1'), + end: new Date('2100-1-1'), + selected: this.selectedDate + }) + }.width('100%') + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test05.ets b/arkoala-arkts/user/src/ets/tests/test05.ets new file mode 100644 index 0000000000000000000000000000000000000000..3ea4531ae4f8b5eb9e905ca39d68964f7beb3e27 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test05.ets @@ -0,0 +1,34 @@ +@Entry +@Component +struct MapSample { + @State message: Map = new Map([[0, "a"], [1, "b"], [3, "c"]]); + + build() { + Row() { + Column() { + ForEach(Array.from(this.message.entries()), (item: [number, string]) => { + Text(`${item[0]}`).fontSize(30) + Text(`${item[1]}`).fontSize(30) + Divider() + }) + Button('init map').onClick(() => { + this.message = new Map([[0, "e"], [1, "f"], [3, "g"]]); + }) + Button('set new one').onClick(() => { + this.message.set(4, "d"); + }) + Button('clear').onClick(() => { + this.message.clear(); + }) + Button('replace the first one').onClick(() => { + this.message.set(0, "aa"); + }) + Button('delete the first one').onClick(() => { + this.message.delete(0); + }) + } + .width('100%') + } + .height('100%') + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test06.ets b/arkoala-arkts/user/src/ets/tests/test06.ets new file mode 100644 index 0000000000000000000000000000000000000000..80209e198d505c2db28ab3a711698d9190a128b4 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test06.ets @@ -0,0 +1,32 @@ +@Entry +@Component +struct SetSample { + @State message: Set = new Set([0, 1, 2, 3, 4]); + + build() { + Row() { + Column() { + ForEach(Array.from(this.message.entries()), (item: [number, number]) => { + Text(`${item[0]}`).fontSize(30) + Divider() + }) + Button('init set').onClick(() => { + this.message = new Set([10, 1, 2, 3, 8]); + }) + Button('set new one').onClick(() => { + this.message.add(5); + }) + Button('clear').onClick(() => { + this.message.clear(); + }) + Button('delete the first one').onClick(() => { + this.message.delete(0); + }) + } + .width('100%') + } + .height('100%') + } +} + + diff --git a/arkoala-arkts/user/src/ets/tests/test07.ets b/arkoala-arkts/user/src/ets/tests/test07.ets new file mode 100644 index 0000000000000000000000000000000000000000..a4d5406c20f1f5ed17a544482871de5dca52028f --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test07.ets @@ -0,0 +1,24 @@ +@Entry +@Component +struct EntryComponent7 { + build() { + Column() { + MyComponent7() + } + } +} + +@Component +struct MyComponent7 { + @State count: number | undefined = 0; + + build() { + Column() { + Text(`count(${this.count})`) + Button('change') + .onClick(() => { + this.count = undefined; // UI will be triggered + }) + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test08.ets b/arkoala-arkts/user/src/ets/tests/test08.ets new file mode 100644 index 0000000000000000000000000000000000000000..5e91059e0d9d83a0f016db9554cac0ec327782dc --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test08.ets @@ -0,0 +1,27 @@ +// import { resourceManager } from '@kit.LocalizationKit'; + +@Entry +@Component +struct EntryComponent8 { + build() { + Column() { + MyComponent8() + } + } +} + +@Component +struct MyComponent8 { + @State count: Resource = $r('sys.color.ohos_id_color_emphasize') + + build() { + Column() { + Text('Hello') + .fontColor(this.count) + Button('change') + .onClick(() => { + this.count = $r('sys.media.ohos_user_auth_icon_face'); + }) + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test09.ets b/arkoala-arkts/user/src/ets/tests/test09.ets new file mode 100644 index 0000000000000000000000000000000000000000..e6bf04df39407dacd31975d0b079f0e9089b893a --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test09.ets @@ -0,0 +1,29 @@ +@Entry +@Component +struct EntryComponent { + @State state : number = 2; + build() { + Column() { + Button(`${this.state}`).onClick(()=>{ + this.state += 4; // change state, will synchronize to its Child. + }) + MyComponent({prop : this.state}) + } + } +} + +@Component +struct MyComponent { + @State count: Resource = $r('sys.color.ohos_id_color_emphasize') + @Prop prop : number = 1; + build() { + Column() { + Text(`Hello ${this.prop}`) + .fontColor(this.count) + Button('change') + .onClick(() => { + this.prop += 5 // change prop, will not sync back to its parent + }) + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test10.ets b/arkoala-arkts/user/src/ets/tests/test10.ets new file mode 100644 index 0000000000000000000000000000000000000000..1b453137c69818dcb08c023cc2c50a7a2340eb26 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test10.ets @@ -0,0 +1,30 @@ +//import {AppStorageV2} from '@kit.ArkUI' +@Entry +@Component +struct EntryComponent10 { + @State state : number = 2; + build() { + Column() { + Button(`${this.state}`).onClick(()=>{ + this.state += 4; // change state, will synchronize to its Child. + }) + MyComponent10({link : this.state}) + } + } +} + +@Component +struct MyComponent10 { + @State count: Resource = $r('sys.color.ohos_id_color_emphasize') + @Link link : number; + build() { + Column() { + Text(`Hello ${this.link}`) + .fontColor(this.count) + Button('change') + .onClick(() => { + this.link += 5 // change link, will sync back to its parent + }) + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test11.ets b/arkoala-arkts/user/src/ets/tests/test11.ets new file mode 100644 index 0000000000000000000000000000000000000000..c7ca7a31b9422d36290183f879e65d9e4fc2b679 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test11.ets @@ -0,0 +1,46 @@ +class Model11 { + public value: string; + constructor(value: string) { + this.value = value; + } +} + +@Entry +@Component +struct EntryComponent11 { + build() { + Column() { + // 此处指定的任何命名参数都将在初始渲染时 + // 覆盖本地定义的默认值 + MyComponent11({ count: 1, increaseBy: 2 }) + MyComponent11({ title: new Model11('Hello, World 2'), count: 7 }) + } + } +} + +@Component +struct MyComponent11 { + + @State title: Model11 = new Model11('local child value'); + @State count: number = 0; + private increaseBy : number = 1; + + build() { + Column() { + Text(`count : ${this.count} titleValue : ${this.title.value}`) + Button() { + Text(`Click to change title`).fontSize(10) + }.onClick(() => { + // @State变量的更新将触发上面的Text组件内容更新 + this.title.value = this.title.value == 'Hello Ace' ? 'Hello World' : 'Hello Ace'; + }) + + Button() { + Text(`Click to increase count=${this.count}`).fontSize(10) + }.onClick(() => { + // @State变量的更新将触发上面的Text组件内容更新 + this.count += this.increaseBy; + }) + } // Column + } // build +} diff --git a/arkoala-arkts/user/src/ets/tests/test12.ets b/arkoala-arkts/user/src/ets/tests/test12.ets new file mode 100644 index 0000000000000000000000000000000000000000..dce9e8526514a6d8637dc6ca60c5af5a9fb4e392 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test12.ets @@ -0,0 +1,97 @@ +@Observed +class Son12 { + public title: string; + + constructor(title: string) { + this.title = title; + } +} + +@Observed +class Father12 { + public name: string; + public son: Son12; + + constructor(name: string, son: Son12) { + this.name = name; + this.son = son; + } +} + + +@Entry +@Component +struct Person12 { + @State person: Father12 = new Father12('Hello', new Son12('world')); + + build() { + Column() { + Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { + Button('change Father12 name') + .width(312) + .height(40) + .margin(12) + .fontColor('#FFFFFF,90%') + .onClick(() => { + this.person.name = "Hi"; + }) + Button('change Son12 title') + .width(312) + .height(40) + .margin(12) + .fontColor('#FFFFFF,90%') + .onClick(() => { + this.person.son.title = "ArkUI"; // Son12 must be decorated with @Observed + }) + Text(this.person.name) + .fontSize(16) + .margin(12) + .width(312) + .height(40) + .backgroundColor('#ededed') + .borderRadius(20) + .textAlign(TextAlign.Center) + .fontColor('#e6000000') + .onClick(() => { + this.person.name = 'Bye'; + }) + Text(this.person.son.title) + .fontSize(16) + .margin(12) + .width(312) + .height(40) + .backgroundColor('#ededed') + .borderRadius(20) + .textAlign(TextAlign.Center) + .onClick(() => { + this.person.son.title = "openHarmony"; + }) + Child({ child: this.person.son }) + } + + } + + } +} + + +@Component +struct Child { + @Prop child: Son12 = new Son12(''); + + build() { + Column() { + Text(this.child.title) + .fontSize(16) + .margin(12) + .width(312) + .height(40) + .backgroundColor('#ededed') + .borderRadius(20) + .textAlign(TextAlign.Center) + .onClick(() => { + this.child.title = 'Bye Bye'; + }) + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test13.ets b/arkoala-arkts/user/src/ets/tests/test13.ets new file mode 100644 index 0000000000000000000000000000000000000000..c6bf46028cc372f373f1081e1465c22a963f6fe9 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test13.ets @@ -0,0 +1,23 @@ +class Info13 { + info: string = 'Hello'; +} + +@Component +struct Child13 { + @Link test: Info13; + build() { + Text(this.test.info) + } +} + +@Entry +@Component +struct LinkExample { + @State info: Info13 = new Info13(); + build() { + Column() { + Child13({ test: this.info }) // also support this spec + Child13({ test: $info }) // support $ + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test14.ets b/arkoala-arkts/user/src/ets/tests/test14.ets new file mode 100644 index 0000000000000000000000000000000000000000..40b9629a8a6636349b898ac8a385ebe252817693 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test14.ets @@ -0,0 +1,50 @@ +@Component +struct Child14 { + @Consume selectedDate: Date; + + build() { + Column() { + Button(`child increase the day by 1`) + .onClick(() => { + this.selectedDate.setDate(this.selectedDate.getDate() + 1) + }) + Button('child update the new date') + .margin(10) + .onClick(() => { + this.selectedDate = new Date('2023-09-09') + }) + DatePicker({ + start: new Date('1970-1-1'), + end: new Date('2100-1-1'), + selected: this.selectedDate + }) + } + } +} + +@Entry +@Component +struct Parent14 { + @Provide selectedDate: Date = new Date('2021-08-08') + + build() { + Column() { + Button('parent increase the day by 1') + .margin(10) + .onClick(() => { + this.selectedDate.setDate(this.selectedDate.getDate() + 1) + }) + Button('parent update the new date') + .margin(10) + .onClick(() => { + this.selectedDate = new Date('2023-07-07') + }) + DatePicker({ + start: new Date('1970-1-1'), + end: new Date('2100-1-1'), + selected: this.selectedDate + }) + Child14() + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test15.ets b/arkoala-arkts/user/src/ets/tests/test15.ets new file mode 100644 index 0000000000000000000000000000000000000000..15ad7966927c0968a27f45c5ca4b4ed001336ccf --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test15.ets @@ -0,0 +1,50 @@ +@Component +struct GrandSon15 { + // @Consume装饰的变量通过相同的属性名绑定其祖先内的@Provide装饰的变量 + @Consume("reviewVotes") reviewVotes: number; + + build() { + Column() { + Text(`reviewVotes(${this.reviewVotes})`) // Text显示10 + Button(`reviewVotes(${this.reviewVotes}), give +1`) + .onClick(() => this.reviewVotes += 1) + } + .width('50%') + } +} + +@Component +struct Child15 { + // @Provide({ allowOverride: "reviewVotes" }) reviewVotes: number = 10; + @Provide("reviewVotes") reviewVotes: number = 10; + + build() { + Row({ space: 5 }) { + GrandSon15() + } + } +} + +@Component +struct Parent15 { + // @Provide({ allowOverride: "reviewVotes" }) reviewVotes: number = 20; + @Provide("reviewVotes") reviewVotes: number = 20; + + build() { + Child15() + } +} + +@Entry +@Component +struct GrandParent15 { + @Provide("reviewVotes") reviewVotes: number = 40; + + build() { + Column() { + Button(`reviewVotes(${this.reviewVotes}), give +1`) + .onClick(() => this.reviewVotes += 1) + Parent15() + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test16.ets b/arkoala-arkts/user/src/ets/tests/test16.ets new file mode 100644 index 0000000000000000000000000000000000000000..ea59a0288ea55c0322b9625f6709defc7bd4523b --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test16.ets @@ -0,0 +1,41 @@ +@Observed +class Info16 { + count: number; + + constructor(count: number) { + this.count = count; + } +} + +@Component +struct Child16 { + @ObjectLink num: Info16; + + build() { + Column() { + Text(`num的值: ${this.num.count}`) + .onClick(() => { + // 正确写法,可以更改@ObjectLink装饰变量的成员属性 + this.num.count += 20; // modify count can sync back its parent + }) + } + } +} + +@Entry +@Component +struct Parent16 { + @State num: Info16 = new Info16(10); + + build() { + Column() { + Text(`count的值: ${this.num.count}`) + Button('click') + .onClick(() => { + // 可以在父组件做整体替换 + this.num = new Info16(30); // modify ObjectLink object, can sync to its child + }) + Child16({num: this.num}) + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test17.ets b/arkoala-arkts/user/src/ets/tests/test17.ets new file mode 100644 index 0000000000000000000000000000000000000000..1b47318b052a0a94881696998dc4793fe1a1ab66 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test17.ets @@ -0,0 +1,123 @@ +let NextID: number = 1; + +@Observed +class Bag { + public id: number; + public size: number; + + constructor(size: number) { + this.id = NextID++; + this.size = size; + } +} + +@Observed +class User { + public bag: Bag; + + constructor(bag: Bag) { + this.bag = bag; + } +} + +@Observed +class Book { + public bookName: BookName; + + constructor(bookName: BookName) { + this.bookName = bookName; + } +} + +@Observed +class BookName extends Bag { + public nameSize: number; + + constructor(nameSize: number) { + // 调用父类方法对nameSize进行处理 + super(nameSize); + this.nameSize = nameSize; + } +} + +@Component +struct Son17 { + label: string = 'Son17'; + @ObjectLink bag: Bag; + + build() { + Column() { + Text(`Son17 [${this.label}] this.bag.size = ${this.bag.size}`) + .fontColor('#ffffffff') + .backgroundColor('#ff3d9dba') + .width(320) + .height(50) + .borderRadius(25) + .margin(10) + .textAlign(TextAlign.Center) + Button(`Son17: this.bag.size add 1`) + .width(320) + .backgroundColor('#ff17a98d') + .margin(10) + .onClick(() => { + this.bag.size += 1; + }) + } + } +} + +@Component +struct Father17 { + label: string = 'Father17'; + @ObjectLink bookName: BookName; + + build() { + Row() { + Column() { + Text(`Father17 [${this.label}] this.bookName.size = ${this.bookName.size}`) + .fontColor('#ffffffff') + .backgroundColor('#ff3d9dba') + .width(320) + .height(50) + .borderRadius(25) + .margin(10) + .textAlign(TextAlign.Center) + Button(`Father17: this.bookName.size add 1`) + .width(320) + .backgroundColor('#ff17a98d') + .margin(10) + .onClick(() => { + this.bookName.size += 1; + console.log('this.bookName.size:' + this.bookName.size); + }) + } + .width(320) + } + } +} + +@Entry +@Component +struct GrandFather17 { + @State user: User = new User(new Bag(0)); + @State child: Book = new Book(new BookName(0)); + + build() { + Column() { + Son17({ label: 'Son17 #1', bag: this.user.bag }) + .width(320) + Father17({ label: 'Father17 #3', bookName: this.child.bookName }) + .width(320) + Button(`GrandFather17: this.child.bookName.size add 10`) + .width(320) + .backgroundColor('#ff17a98d') + .margin(10) + .onClick(() => { + this.child.bookName.size += 10; + console.log('this.child.bookName.size:' + this.child.bookName.size); + }) + } + } +} + + diff --git a/arkoala-arkts/user/src/ets/tests/test18.ets b/arkoala-arkts/user/src/ets/tests/test18.ets new file mode 100644 index 0000000000000000000000000000000000000000..64d7f45afa72440a0e8cf5aaf34ab2a0cd23e76e --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test18.ets @@ -0,0 +1,61 @@ +@Observed +class ObservedArray extends Array { + constructor(args: T[]) { + super(...args); + } +} + +@Component +struct Item { + @ObjectLink itemArr: ObservedArray; + + build() { + Row() { + ForEach(this.itemArr, (item: string, index: number) => { + Text(`${index}: ${item}`) + .width(100) + .height(100) + }, (item: string) => item) + } + } +} + +@Entry +@Component +struct IndexPage { + @State arr: Array> = [new ObservedArray(['apple']), new ObservedArray(['banana']), new ObservedArray(['orange'])]; + + build() { + Column() { + ForEach(this.arr, (itemArr: ObservedArray) => { + Item({ itemArr: itemArr }) + }) + + Divider() + + Button('push two-dimensional array item') + .margin(10) + .onClick(() => { + this.arr[0].push('strawberry'); + }) + + Button('push array item') + .margin(10) + .onClick(() => { + this.arr.push(new ObservedArray(['pear'])); + }) + + Button('change two-dimensional array first item') + .margin(10) + .onClick(() => { + this.arr[0][0] = 'APPLE'; + }) + + Button('change array first item') + .margin(10) + .onClick(() => { + this.arr[0] = new ObservedArray(['watermelon']); + }) + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test19.ets b/arkoala-arkts/user/src/ets/tests/test19.ets new file mode 100644 index 0000000000000000000000000000000000000000..653f21e03aa70f0f1264a6c46b4951bffa558235 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test19.ets @@ -0,0 +1,75 @@ +@Observed +class Info { + public info: MySet; + + constructor(info: MySet) { + this.info = info; + } +} + + +@Observed +export class MySet extends Set { + public name: string; + + constructor(name?: string, args?: T[]) { + super(args); + this.name = name ? name : "My Set"; + } + + getName() { + return this.name; + } +} + +@Entry +@Component +struct SetSampleNested { + @State message: Info = new Info(new MySet("Set", [0, 1, 2, 3, 4])); + + build() { + Row() { + Column() { + SetSampleNestedChild({ mySet: this.message.info }) + } + .width('100%') + } + .height('100%') + } +} + +@Component +struct SetSampleNestedChild { + @ObjectLink mySet: MySet; + + build() { + Row() { + Column() { + ForEach(Array.from(this.mySet.entries()), (item: [number, number]) => { + Text(`${item}`).fontSize(30) + Divider() + }) + Button('set new one') + .width(200) + .margin(10) + .onClick(() => { + this.mySet.add(5); + }) + Button('clear') + .width(200) + .margin(10) + .onClick(() => { + this.mySet.clear(); + }) + Button('delete the first one') + .width(200) + .margin(10) + .onClick(() => { + this.mySet.delete(0); + }) + } + .width('100%') + } + .height('100%') + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test20.ets b/arkoala-arkts/user/src/ets/tests/test20.ets new file mode 100644 index 0000000000000000000000000000000000000000..653f21e03aa70f0f1264a6c46b4951bffa558235 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test20.ets @@ -0,0 +1,75 @@ +@Observed +class Info { + public info: MySet; + + constructor(info: MySet) { + this.info = info; + } +} + + +@Observed +export class MySet extends Set { + public name: string; + + constructor(name?: string, args?: T[]) { + super(args); + this.name = name ? name : "My Set"; + } + + getName() { + return this.name; + } +} + +@Entry +@Component +struct SetSampleNested { + @State message: Info = new Info(new MySet("Set", [0, 1, 2, 3, 4])); + + build() { + Row() { + Column() { + SetSampleNestedChild({ mySet: this.message.info }) + } + .width('100%') + } + .height('100%') + } +} + +@Component +struct SetSampleNestedChild { + @ObjectLink mySet: MySet; + + build() { + Row() { + Column() { + ForEach(Array.from(this.mySet.entries()), (item: [number, number]) => { + Text(`${item}`).fontSize(30) + Divider() + }) + Button('set new one') + .width(200) + .margin(10) + .onClick(() => { + this.mySet.add(5); + }) + Button('clear') + .width(200) + .margin(10) + .onClick(() => { + this.mySet.clear(); + }) + Button('delete the first one') + .width(200) + .margin(10) + .onClick(() => { + this.mySet.delete(0); + }) + } + .width('100%') + } + .height('100%') + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test21.ets b/arkoala-arkts/user/src/ets/tests/test21.ets new file mode 100644 index 0000000000000000000000000000000000000000..999f324e260cd9b68624107200fe7fe9a88804d0 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test21.ets @@ -0,0 +1,80 @@ +@Observed +class Source { + public source: number; + + constructor(source: number) { + this.source = source; + } +} + +@Observed +class Data { + public data: number; + + constructor(data: number) { + this.data = data; + } +} + +@Entry +@Component +struct Parent21 { + @State count: Source | Data | undefined = new Source(10); + + build() { + Column() { + Child21({ count: this.count }) + + Button('change count property') + .margin(10) + .onClick(() => { + // 判断count的类型,做属性的更新 + if (this.count instanceof Source) { + this.count.source += 1; + } else if (this.count instanceof Data) { + this.count.data += 1; + } else { + console.info('count is undefined, cannot change property'); + } + }) + + Button('change count to Source') + .margin(10) + .onClick(() => { + // 赋值为Source的实例 + this.count = new Source(100); + }) + + Button('change count to Data') + .margin(10) + .onClick(() => { + // 赋值为Data的实例 + this.count = new Data(100); + }) + + Button('change count to undefined') + .margin(10) + .onClick(() => { + // 赋值为undefined + this.count = undefined; + }) + }.width('100%') + } +} + +@Component +struct Child21 { + @ObjectLink count: Source | Data | undefined; + + build() { + Column() { + Text(`count is instanceof ${this.count instanceof Source ? 'Source' : + this.count instanceof Data ? 'Data' : 'undefined'}`) + .fontSize(30) + .margin(10) + + Text(`count's property is ${this.count instanceof Source ? this.count.source : this.count?.data}`).fontSize(15) + + }.width('100%') + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test22.ets b/arkoala-arkts/user/src/ets/tests/test22.ets new file mode 100644 index 0000000000000000000000000000000000000000..f97b70a7ef5af052c9f7918971eed7718dac9c28 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test22.ets @@ -0,0 +1,60 @@ +class LogTrack { + @Track str1: string; + @Track str2: string; + + constructor(str1: string) { + this.str1 = str1; + this.str2 = 'World'; + } +} + +class LogNotTrack { + str1: string; + str2: string; + + constructor(str1: string) { + this.str1 = str1; + this.str2 = '世界'; + } +} + +@Entry +@Component +struct AddLog { + @State logTrack: LogTrack = new LogTrack('Hello'); + @State logNotTrack: LogNotTrack = new LogNotTrack('你好'); + + isRender(index: number) { + console.log(`Text ${index} is rendered`); + return 50; + } + + build() { + Row() { + Column() { + Text(this.logTrack.str1) // Text1 + .fontSize(this.isRender(1)) + .fontWeight(FontWeight.Bold) + Text(this.logTrack.str2) // Text2 + .fontSize(this.isRender(2)) + .fontWeight(FontWeight.Bold) + Button('change logTrack.str1') + .onClick(() => { + this.logTrack.str1 = 'Bye'; + }) + Text(this.logNotTrack.str1) // Text3 + .fontSize(this.isRender(3)) + .fontWeight(FontWeight.Bold) + Text(this.logNotTrack.str2) // Text4 + .fontSize(this.isRender(4)) + .fontWeight(FontWeight.Bold) + Button('change logNotTrack.str1') + .onClick(() => { + this.logNotTrack.str1 = '再见'; + }) + } + .width('100%') + } + .height('100%') + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test23.ets b/arkoala-arkts/user/src/ets/tests/test23.ets new file mode 100644 index 0000000000000000000000000000000000000000..75b382ab6e8118c0b7682128d2f721b36da43784 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test23.ets @@ -0,0 +1,29 @@ +@Entry +@Component +struct UsePropertyName { + @State @Watch('countUpdated') apple: number = 0; + @State @Watch('countUpdated') cabbage: number = 0; + @State fruit: number = 0; + // @Watch 回调 + countUpdated(propName: string): void { + if (propName == 'apple') { + this.fruit = this.apple; + } + } + + build() { + Column() { + Text(`Number of apples: ${this.apple.toString()}`).fontSize(30) + Text(`Number of cabbages: ${this.cabbage.toString()}`).fontSize(30) + Text(`Total number of fruits: ${this.fruit.toString()}`).fontSize(30) + Button('Add apples') + .onClick(() => { + this.apple++; + }) + Button('Add cabbages') + .onClick(() => { + this.cabbage++; + }) + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test24.ets b/arkoala-arkts/user/src/ets/tests/test24.ets new file mode 100644 index 0000000000000000000000000000000000000000..37d6917cc35918dbe3b064267ff0c83024c52155 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test24.ets @@ -0,0 +1,57 @@ +@Observed +class Task { + isFinished: boolean = false; + + constructor(isFinished : boolean) { + this.isFinished = isFinished; + } +} + +@Entry +@Component +struct ParentComponent { + @State @Watch('onTaskAChanged') taskA: Task = new Task(false); + @State @Watch('onTaskBChanged') taskB: Task = new Task(false); + + onTaskAChanged(changedPropertyName: string): void { + console.log(`观测到父组件任务属性变化: ${changedPropertyName}`); + } + + onTaskBChanged(changedPropertyName: string): void { + console.log(`观测到父组件任务属性变化: ${changedPropertyName}`); + } + + build() { + Column() { + Text(`父组件任务A状态: ${this.taskA.isFinished ? '已完成' : '未完成'}`) + Text(`父组件任务B状态: ${this.taskB.isFinished ? '已完成' : '未完成'}`) + ChildComponent({ taskA: this.taskA, taskB: this.taskB }) + Button('切换任务状态') + .onClick(() => { + this.taskB = new Task(!this.taskB.isFinished); + this.taskA = new Task(!this.taskA.isFinished); + }) + } + } +} + +@Component +struct ChildComponent { + @ObjectLink @Watch('onObjectLinkTaskChanged') taskB: Task; + @Link @Watch('onLinkTaskChanged') taskA: Task; + + onObjectLinkTaskChanged(changedPropertyName: string): void { + console.log(`观测到子组件@ObjectLink关联的任务属性变化: ${changedPropertyName}`); + } + + onLinkTaskChanged(changedPropertyName: string): void { + console.log(`观测到子组件@Link关联的任务属性变化: ${changedPropertyName}`); + } + + build() { + Column() { + Text(`子组件任务A状态: ${this.taskA.isFinished ? '已完成' : '未完成'}`) + Text(`子组件任务B状态: ${this.taskB.isFinished ? '已完成' : '未完成'}`) + } + } +} diff --git a/arkoala-arkts/user/src/ets/tests/test25.ets b/arkoala-arkts/user/src/ets/tests/test25.ets new file mode 100644 index 0000000000000000000000000000000000000000..3e1605b5bb9f088777f766ef4b0435e0f873ce61 --- /dev/null +++ b/arkoala-arkts/user/src/ets/tests/test25.ets @@ -0,0 +1,35 @@ +@Component +struct TotalView { + @Prop @Watch('onCountUpdated') count: number = 0; + @State total: number = 0; + // @Watch 回调 + onCountUpdated(propName: string): void { + this.total += this.count; + } + + build() { + Text(`Total: ${this.total}`) + } +} + +@Entry +@Component +struct CountModifier { + @State @Watch('onCountChange') count: number = 0; + + onCountChange() { + console.log(`change count = ${this.count}`) + } + build() { + Column() { + Button('add to basket') + .onClick(() => { + for (let i = 0; i < 1000; i++) { + this.count++ + } + + }) + TotalView({ count: this.count }) + } + } +}