-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextFields.swift
61 lines (52 loc) · 1.65 KB
/
TextFields.swift
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
import SwiftUI
struct TextFields: View {
@State var text: String = ""
@State var dataArray: [String] = []
var body: some View {
NavigationStack {
VStack(spacing: 20) {
TextField("Search for a book", text : $text)
.padding(10)
.background(.gray.opacity(0.1))
.cornerRadius(10)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
Button(
action: {
if textIsAppropriate() {
saveText()
}
},
label: {
Text("Save".uppercased())
.padding()
.frame(maxWidth: .infinity)
.background(textIsAppropriate() ? .blue : .gray)
.cornerRadius(10)
.foregroundStyle(.white)
.font(.headline)
}
).disabled(!textIsAppropriate())
ForEach(dataArray, id: \.self) { data in
Text(data)
}
Spacer()
}
.padding()
.navigationTitle("Find a book")
}
}
func textIsAppropriate() -> Bool {
if text.count >= 3 {
return true
}
return false
}
func saveText() {
dataArray.append(text)
text = ""
}
}
#Preview {
TextFields()
}