Skip to content

Commit 16cbf2c

Browse files
authored
Merge pull request #3 from aaronLab/feature/support-binding
Support @State text Property
2 parents 816ce9c + 7a46e53 commit 16cbf2c

5 files changed

Lines changed: 194 additions & 131 deletions

File tree

Example/Example/ContentView.swift

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,31 @@ struct ContentView: View {
2525
Group {
2626
Text("Default")
2727
.font(.system(size: 14, weight: .bold, design: .default))
28-
RespondableTextField(tag: 0, placeholder: "First TextField") { value in
29-
self.text1 = value
28+
RespondableTextField(text: $text1, tag: 0, isFirstResponder: true, placeholder: "1st") { value in
29+
print("onEditing: \(value)")
3030
} onCommitted: {
31-
print(self.text1)
31+
print("onCommitted")
32+
} didBeginEditing: {
33+
print("didBeginEditing")
34+
} didEndEditing: {
35+
print("didEndEditing")
3236
}
37+
3338
Text(text1)
3439
}
3540

3641
// SecureType + RectangleLine Border
3742
Group {
3843
Text("SecureType + RectangleLine Border")
3944
.font(.system(size: 14, weight: .bold, design: .default))
40-
RespondableTextField(tag: 1, placeholder: "Second TextField") { value in
41-
self.text2 = value
45+
RespondableTextField(text: $text2, tag: 1, placeholder: "2nd") { value in
46+
print("onEditing: \(value)")
47+
} onCommitted: {
48+
print("onCommitted")
49+
} didBeginEditing: {
50+
print("didBeginEditing")
51+
} didEndEditing: {
52+
print("didEndEditing")
4253
}
4354
.respondableSecureType()
4455
.respondableLineStyle()
@@ -50,10 +61,14 @@ struct ContentView: View {
5061
Group {
5162
Text("NumberPad + OneTimeCode + Rounded Border")
5263
.font(.system(size: 14, weight: .bold, design: .default))
53-
RespondableTextField(tag: 2, placeholder: "Third TextField") { value in
54-
self.text3 = value
64+
RespondableTextField(text: $text3, tag: 2, placeholder: "3rd") { value in
65+
print("onEditing: \(value)")
5566
} onCommitted: {
56-
print(self.text3)
67+
print("onCommitted")
68+
} didBeginEditing: {
69+
print("didBeginEditing")
70+
} didEndEditing: {
71+
print("didEndEditing")
5772
}
5873
.respondableKeyboardType(.numberPad)
5974
.respondableContentType(.oneTimeCode)
@@ -62,19 +77,23 @@ struct ContentView: View {
6277
Text(text3)
6378
}
6479

65-
// didBeginEditing + didEndEditing + Bazel Border + Font
80+
// didEndEditing + Bazel Border + Font
6681
Group {
6782
Text("didBeginEditing + didEndEditing + Bazel Border")
6883
.font(.system(size: 14, weight: .bold, design: .default))
69-
RespondableTextField(tag: 3, placeholder: "Fourth TextField") { value in
70-
self.text4 = value
84+
RespondableTextField(text: $text4, tag: 3, placeholder: "4th") { value in
85+
print("onEditing: \(value)")
86+
} onCommitted: {
87+
print("onCommitted")
7188
} didBeginEditing: {
72-
print("Begin")
73-
} didEndEditing: { _ in
74-
print("Done")
89+
print("didBeginEditing")
90+
} didEndEditing: {
91+
print("didEndEditing")
7592
}
7693
.respondableBezelStyle()
7794
.respondableFont(.systemFont(ofSize: 20, weight: .bold))
95+
96+
Text(text4)
7897
}
7998

8099
} //: V

README.md

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@
2929

3030
```Swift
3131
RespondableTextField(
32-
tag: Int,
33-
placeholder: String?,
34-
onEditing: ((String) -> Void)?,
35-
onCommitted: (() -> Void)?,
36-
didBeginEditing: (() -> Void)?,
37-
didEndEditing: ((UITextField.DidEndEditingReason) -> Void)?
32+
text: Binding<String>,
33+
tag: Int,
34+
isFirstResponder: Bool = false,
35+
placeholder: String? = nil,
36+
onEditing: ((String) -> Void)? = nil,
37+
onCommitted: (() -> Void)? = nil,
38+
didBeginEditing: (() -> Void)? = nil,
39+
didEndEditing: (() -> Void)? = nil
3840
)
3941
```
4042

41-
- Also, you don't have to fill all of the optional parameters.
43+
- Also, you don't have to fill all of the optional parameters or `isFirstResponder`.
44+
- You can make the textField the first responder with `isFirstResponder = true`.
4245

4346
## Example
4447

@@ -72,20 +75,31 @@ struct ContentView: View {
7275
Group {
7376
Text("Default")
7477
.font(.system(size: 14, weight: .bold, design: .default))
75-
RespondableTextField(tag: 0, placeholder: "First TextField") { value in
76-
self.text1 = value
78+
RespondableTextField(text: $text1, tag: 0, isFirstResponder: true, placeholder: "1st") { value in
79+
print("onEditing: \(value)")
7780
} onCommitted: {
78-
print(self.text1)
81+
print("onCommitted")
82+
} didBeginEditing: {
83+
print("didBeginEditing")
84+
} didEndEditing: {
85+
print("didEndEditing")
7986
}
87+
8088
Text(text1)
8189
}
8290

8391
// SecureType + RectangleLine Border
8492
Group {
8593
Text("SecureType + RectangleLine Border")
8694
.font(.system(size: 14, weight: .bold, design: .default))
87-
RespondableTextField(tag: 1, placeholder: "Second TextField") { value in
88-
self.text2 = value
95+
RespondableTextField(text: $text2, tag: 1, placeholder: "2nd") { value in
96+
print("onEditing: \(value)")
97+
} onCommitted: {
98+
print("onCommitted")
99+
} didBeginEditing: {
100+
print("didBeginEditing")
101+
} didEndEditing: {
102+
print("didEndEditing")
89103
}
90104
.respondableSecureType()
91105
.respondableLineStyle()
@@ -97,10 +111,14 @@ struct ContentView: View {
97111
Group {
98112
Text("NumberPad + OneTimeCode + Rounded Border")
99113
.font(.system(size: 14, weight: .bold, design: .default))
100-
RespondableTextField(tag: 2, placeholder: "Third TextField") { value in
101-
self.text3 = value
114+
RespondableTextField(text: $text3, tag: 2, placeholder: "3rd") { value in
115+
print("onEditing: \(value)")
102116
} onCommitted: {
103-
print(self.text3)
117+
print("onCommitted")
118+
} didBeginEditing: {
119+
print("didBeginEditing")
120+
} didEndEditing: {
121+
print("didEndEditing")
104122
}
105123
.respondableKeyboardType(.numberPad)
106124
.respondableContentType(.oneTimeCode)
@@ -109,19 +127,23 @@ struct ContentView: View {
109127
Text(text3)
110128
}
111129

112-
// didBeginEditing + didEndEditing + Bazel Border + Font
130+
// didEndEditing + Bazel Border + Font
113131
Group {
114132
Text("didBeginEditing + didEndEditing + Bazel Border")
115133
.font(.system(size: 14, weight: .bold, design: .default))
116-
RespondableTextField(tag: 3, placeholder: "Fourth TextField") { value in
117-
self.text4 = value
134+
RespondableTextField(text: $text4, tag: 3, placeholder: "4th") { value in
135+
print("onEditing: \(value)")
136+
} onCommitted: {
137+
print("onCommitted")
118138
} didBeginEditing: {
119-
print("Begin")
120-
} didEndEditing: { _ in
121-
print("Done")
139+
print("didBeginEditing")
140+
} didEndEditing: {
141+
print("didEndEditing")
122142
}
123143
.respondableBezelStyle()
124144
.respondableFont(.systemFont(ofSize: 20, weight: .bold))
145+
146+
Text(text4)
125147
}
126148

127149
} //: V
@@ -130,12 +152,6 @@ struct ContentView: View {
130152
}
131153
}
132154

133-
struct ContentView_Previews: PreviewProvider {
134-
static var previews: some View {
135-
ContentView()
136-
}
137-
}
138-
139155
```
140156

141157
## License

RespondableTextField.xcodeproj/project.pbxproj

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
/* End PBXAggregateTarget section */
2222

2323
/* Begin PBXBuildFile section */
24-
A4C784E5258B9545009BB00C /* WrappableTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4C784E4258B9545009BB00C /* WrappableTextField.swift */; };
2524
OBJ_24 /* RespondableTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* RespondableTextField.swift */; };
2625
OBJ_31 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; };
2726
OBJ_42 /* RespondableTextFieldTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* RespondableTextFieldTests.swift */; };
@@ -47,7 +46,6 @@
4746
/* End PBXContainerItemProxy section */
4847

4948
/* Begin PBXFileReference section */
50-
A4C784E4258B9545009BB00C /* WrappableTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WrappableTextField.swift; sourceTree = "<group>"; };
5149
OBJ_12 /* RespondableTextFieldTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RespondableTextFieldTests.swift; sourceTree = "<group>"; };
5250
OBJ_13 /* XCTestManifests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCTestManifests.swift; sourceTree = "<group>"; };
5351
OBJ_17 /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
@@ -104,7 +102,7 @@
104102
name = Products;
105103
sourceTree = BUILT_PRODUCTS_DIR;
106104
};
107-
OBJ_5 /* */ = {
105+
OBJ_5 = {
108106
isa = PBXGroup;
109107
children = (
110108
OBJ_6 /* Package.swift */,
@@ -114,7 +112,6 @@
114112
OBJ_17 /* LICENSE */,
115113
OBJ_18 /* README.md */,
116114
);
117-
name = "";
118115
sourceTree = "<group>";
119116
};
120117
OBJ_7 /* Sources */ = {
@@ -129,7 +126,6 @@
129126
isa = PBXGroup;
130127
children = (
131128
OBJ_9 /* RespondableTextField.swift */,
132-
A4C784E4258B9545009BB00C /* WrappableTextField.swift */,
133129
);
134130
name = RespondableTextField;
135131
path = Sources/RespondableTextField;
@@ -201,7 +197,7 @@
201197
knownRegions = (
202198
en,
203199
);
204-
mainGroup = OBJ_5 /* */;
200+
mainGroup = OBJ_5;
205201
productRefGroup = OBJ_14 /* Products */;
206202
projectDirPath = "";
207203
projectRoot = "";
@@ -219,7 +215,6 @@
219215
isa = PBXSourcesBuildPhase;
220216
buildActionMask = 0;
221217
files = (
222-
A4C784E5258B9545009BB00C /* WrappableTextField.swift in Sources */,
223218
OBJ_24 /* RespondableTextField.swift in Sources */,
224219
);
225220
runOnlyForDeploymentPostprocessing = 0;

0 commit comments

Comments
 (0)