• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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
16import prompt from '@ohos.promptAction';
17import { Information } from '../../model/Information';
18import { logger } from '@ohos/common/src/main/ets/util/Logger';
19
20const TAG: string = 'AddDialog';
21
22@CustomDialog
23export struct AddDialog {
24  @State writeInformation: Information = new Information('', Number.NaN, '')
25  private saveData: (information: Information) => void = () => {
26  };
27  private controller?: CustomDialogController;
28
29  build() {
30    Column() {
31      Text($r('app.string.contact_information'))
32        .fontSize(24)
33        .fontColor(Color.Black)
34        .fontWeight(FontWeight.Bold)
35        .margin({ top: 12 })
36      Row() {
37        Text($r('app.string.contact_name'))
38          .width(65)
39          .fontSize(20)
40          .fontColor(Color.Black)
41          .fontWeight(FontWeight.Medium)
42          .margin({ left: 10 })
43
44        TextInput({ placeholder: $r('app.string.input_name'), text: this.writeInformation.name })
45          .key('inputPhone')
46          .layoutWeight(1)
47          .type(InputType.Normal)
48          .placeholderColor(Color.Gray)
49          .fontSize(19)
50          .maxLength(11)
51          .margin({ right: 10 })
52          .onChange((value: string) => {
53            this.writeInformation.name = value;
54            logger.info(TAG, ` this.writeInformation.name = ${value}`);
55          })
56      }
57      .margin({ top: 12 })
58
59      Row() {
60        Text($r('app.string.contact_age'))
61          .width(65)
62          .fontSize(20)
63          .fontColor(Color.Black)
64          .fontWeight(FontWeight.Medium)
65          .margin({ left: 10 })
66
67        TextInput({ placeholder: $r('app.string.input_age') })
68          .key('inputPhone')
69          .layoutWeight(1)
70          .type(InputType.Number)
71          .placeholderColor(Color.Gray)
72          .fontSize(19)
73          .maxLength(11)
74          .margin({ right: 10 })
75          .onChange((value: string) => {
76            this.writeInformation.age = Number(value);
77          })
78      }
79      .margin({ top: 12 })
80
81      Row() {
82        Text($r('app.string.contact_phone'))
83          .width(65)
84          .fontSize(20)
85          .fontColor(Color.Black)
86          .fontWeight(FontWeight.Medium)
87          .margin({ left: 10 })
88
89        TextInput({ placeholder: $r('app.string.input_phone'), text: this.writeInformation.phone })
90          .key('inputPhone')
91          .layoutWeight(1)
92          .type(InputType.Number)
93          .placeholderColor(Color.Gray)
94          .fontSize(19)
95          .maxLength(11)
96          .margin({ right: 10 })
97          .onChange((value: string) => {
98            this.writeInformation.phone = value;
99          })
100      }
101      .margin({ top: 12 })
102
103      Row() {
104        Button() {
105          Text($r("app.string.button_confirm"))
106            .key('confirm')
107            .fontColor(Color.Blue)
108            .fontSize(17)
109        }
110        .layoutWeight(7)
111        .backgroundColor(Color.White)
112        .margin(5)
113        .onClick(() => {
114          this.addInformation();
115        })
116
117        Divider()
118          .height(30)
119          .vertical(true)
120          .strokeWidth(2)
121          .color('#999999')
122
123        Button() {
124          Text($r('app.string.button_cancel'))
125            .fontColor(Color.Red)
126            .fontSize(17)
127        }
128        .margin(5)
129        .layoutWeight(7)
130        .backgroundColor(Color.White)
131        .onClick(() => {
132          this.controller?.close();
133        })
134      }
135      .margin({ top: 12 })
136    }
137    .padding(12)
138  }
139
140  addInformation() {
141    if (this.writeInformation.name === '') {
142      prompt.showToast({ message: $r('app.string.name_null') });
143      return;
144    }
145    if (this.writeInformation.age < 1 || Number.isNaN(this.writeInformation.age)) {
146      prompt.showToast({ message: $r('app.string.age_null') });
147      return;
148    }
149    if (this.writeInformation.phone === '') {
150      prompt.showToast({ message: $r('app.string.phone_null') });
151      return;
152    }
153    if (this.writeInformation.name !== '' && this.writeInformation.phone !== '' && this.writeInformation.age > 0) {
154      this.saveData(this.writeInformation);
155    }
156    this.controller?.close();
157  }
158}