1/* 2 * Copyright (c) 2022 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 CompA { 20 @State @Watch("onBasketUpdated") shopBasket: Array<number> = [ 7, 12, 47, 3 ]; 21 @State totalPurchase: number = 0; 22 @State @Watch('onPutItem') defArray: Array<string> = ['c', 'g', 't', 'z']; 23 @State resultTip: string = ''; 24 25 updateTotal() : number { 26 let sum = 0; 27 this.shopBasket.forEach((i) => { sum += i; }); 28 this.totalPurchase = (sum < 100) ? sum : 0.9 * sum; 29 return this.totalPurchase; 30 } 31 32 onBasketUpdated(propName: string) : void { 33 animateTo({duration: 1000}, () => { 34 this.updateTotal(); 35 }) 36 } 37 38 updateTip() : string { 39 let tempArray = this.defArray.slice(0, -1); 40 let addItem = this.defArray[this.defArray.length -1]; 41 this.resultTip = tempArray.includes(addItem) ? 42 'add item invalid' : 43 'congratulations! add item success'; 44 return this.resultTip; 45 } 46 47 onPutItem(propName: string) : void { 48 this.updateTip(); 49 } 50 51 build() { 52 Column(){ 53 Button("add to basket").onClick(() => { 54 this.shopBasket.push(Math.round(100 * Math.random())) 55 }) 56 Text('totalPurchase: ' + this.totalPurchase).fontSize(20) 57 Button("put item").onClick(() => { 58 let alList = 'abcdefghijklmnopqrstuvwxyz'; 59 let ranItem = alList[Math.floor(Math.random() * 26)]; 60 this.defArray.push(ranItem) 61 }) 62 Text('tips: ' + this.resultTip).fontSize(20) 63 } 64 } 65} 66` 67exports.expectResult = 68`"use strict"; 69let __generate__Id = 0; 70function generateId() { 71 return "@watch_" + ++__generate__Id; 72} 73class CompA extends View { 74 constructor(compilerAssignedUniqueChildId, parent, params, localStorage) { 75 super(compilerAssignedUniqueChildId, parent, localStorage); 76 this.__shopBasket = new ObservedPropertyObject([7, 12, 47, 3], this, "shopBasket"); 77 this.__totalPurchase = new ObservedPropertySimple(0, this, "totalPurchase"); 78 this.__defArray = new ObservedPropertyObject(['c', 'g', 't', 'z'], this, "defArray"); 79 this.__resultTip = new ObservedPropertySimple('', this, "resultTip"); 80 this.updateWithValueParams(params); 81 this.declareWatch("shopBasket", this.onBasketUpdated); 82 this.declareWatch("defArray", this.onPutItem); 83 } 84 updateWithValueParams(params) { 85 if (params.shopBasket !== undefined) { 86 this.shopBasket = params.shopBasket; 87 } 88 if (params.totalPurchase !== undefined) { 89 this.totalPurchase = params.totalPurchase; 90 } 91 if (params.defArray !== undefined) { 92 this.defArray = params.defArray; 93 } 94 if (params.resultTip !== undefined) { 95 this.resultTip = params.resultTip; 96 } 97 } 98 aboutToBeDeleted() { 99 this.__shopBasket.aboutToBeDeleted(); 100 this.__totalPurchase.aboutToBeDeleted(); 101 this.__defArray.aboutToBeDeleted(); 102 this.__resultTip.aboutToBeDeleted(); 103 SubscriberManager.Get().delete(this.id()); 104 } 105 get shopBasket() { 106 return this.__shopBasket.get(); 107 } 108 set shopBasket(newValue) { 109 this.__shopBasket.set(newValue); 110 } 111 get totalPurchase() { 112 return this.__totalPurchase.get(); 113 } 114 set totalPurchase(newValue) { 115 this.__totalPurchase.set(newValue); 116 } 117 get defArray() { 118 return this.__defArray.get(); 119 } 120 set defArray(newValue) { 121 this.__defArray.set(newValue); 122 } 123 get resultTip() { 124 return this.__resultTip.get(); 125 } 126 set resultTip(newValue) { 127 this.__resultTip.set(newValue); 128 } 129 updateTotal() { 130 let sum = 0; 131 this.shopBasket.forEach((i) => { sum += i; }); 132 this.totalPurchase = (sum < 100) ? sum : 0.9 * sum; 133 return this.totalPurchase; 134 } 135 onBasketUpdated(propName) { 136 Context.animateTo({ duration: 1000 }, () => { 137 this.updateTotal(); 138 }); 139 } 140 updateTip() { 141 let tempArray = this.defArray.slice(0, -1); 142 let addItem = this.defArray[this.defArray.length - 1]; 143 this.resultTip = tempArray.includes(addItem) ? 144 'add item invalid' : 145 'congratulations! add item success'; 146 return this.resultTip; 147 } 148 onPutItem(propName) { 149 this.updateTip(); 150 } 151 render() { 152 Column.create(); 153 Button.createWithLabel("add to basket"); 154 Button.onClick(() => { 155 this.shopBasket.push(Math.round(100 * Math.random())); 156 }); 157 Button.pop(); 158 Text.create('totalPurchase: ' + this.totalPurchase); 159 Text.fontSize(20); 160 Text.pop(); 161 Button.createWithLabel("put item"); 162 Button.onClick(() => { 163 let alList = 'abcdefghijklmnopqrstuvwxyz'; 164 let ranItem = alList[Math.floor(Math.random() * 26)]; 165 this.defArray.push(ranItem); 166 }); 167 Button.pop(); 168 Text.create('tips: ' + this.resultTip); 169 Text.fontSize(20); 170 Text.pop(); 171 Column.pop(); 172 } 173} 174loadDocument(new CompA("1", undefined, {})); 175` 176