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
0 commit comments