summaryrefslogtreecommitdiff
path: root/TalerWallet1/Views/HelperViews/TextFieldAlert.swift
blob: c29bba38545d8f98d45db98ef912cbd24acdf25f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import SwiftUI

struct TextFieldAlert: ViewModifier {
    @Binding var isPresented: Bool
    let title: String
    let doneText: String
    @Binding var text: String
    let placeholder: String
    let action: (String) -> Void
    func body(content: Content) -> some View {
        ZStack(alignment: .center) {
            content
                .disabled(isPresented)
                .accessibilityElement(children: isPresented ? .ignore : .contain)
            if isPresented {
                VStack {
                    Text(title)
                        .accessibilityFont(.headline)
                        .accessibilityAddTraits(.isHeader)
                        .accessibilityRemoveTraits(.isStaticText)
                        .padding()
                    TextField(placeholder, text: $text).textFieldStyle(.roundedBorder).padding()
                    Divider()
                    HStack {
                        Spacer()
                        Button(role: .cancel) {
                            withAnimation { isPresented.toggle() }
                        } label: {
                            Text("Cancel")
                        }
                        Spacer()
                        Divider()
                        Spacer()
                        Button(doneText) {
                            action(text)
                            withAnimation { isPresented.toggle() }
                        }
//                        .accessibilityFont(.talerBody)     TODO: check
                        Spacer()
                    }
                }
                    .accessibility(addTraits: .isModal)
                    .background(.background)
                    .frame(width: 300, height: 200)
                    .cornerRadius(20)
                    .overlay {
                        RoundedRectangle(cornerRadius: 20)
                            .stroke(.quaternary, lineWidth: 1)
                    }
            }
        }
    }
}

extension View {
    public func textFieldAlert(isPresented: Binding<Bool>,
                                     title: String,
                                  doneText: String,
                                      text: Binding<String>,
                               placeholder: String = "",
                                    action: @escaping (String) -> Void
    ) -> some View {
        self.modifier(TextFieldAlert(isPresented: isPresented, title: title, doneText: doneText, text: text, placeholder: placeholder, action: action))
    }
}