-
Notifications
You must be signed in to change notification settings - Fork 3
/
12-IfCaseLet.swift
55 lines (49 loc) · 1.28 KB
/
12-IfCaseLet.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
import CasePaths
import SwiftUI
import SwiftUINavigation
private let readMe = """
This demonstrates how to destructure a binding of an enum into a binding of one of its cases.
Tap the "Edit" button to put the form into edit mode. Then you can make changes to the message \
and either commit the changes by tapping "Save", or discard the changes by tapping "Discard".
"""
struct IfCaseLetCaseStudy: View {
@State var string: String = "Hello"
@State var editableString: EditableString = .inactive
@CasePathable
enum EditableString {
case active(String)
case inactive
}
var body: some View {
Form {
Section {
Text(readMe)
}
$editableString.active.map { $string in
VStack {
TextField("Edit string", text: $string)
HStack {
Button("Discard", role: .cancel) {
editableString = .inactive
}
Spacer()
Button("Save") {
string = string
editableString = .inactive
}
}
}
}
if !editableString.is(\.active) {
Text("\(string)")
Button("Edit") {
editableString = .active(string)
}
}
}
.buttonStyle(.borderless)
}
}
#Preview {
IfCaseLetCaseStudy()
}