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