@@ -16,13 +16,17 @@ public struct ResponsiveTextField {
1616 /// A binding to the text state that will hold the typed text
1717 let text : Binding < String >
1818
19- /// A binding to the editing state of the text field.
19+ /// A binding to control the first responder state of the text field.
2020 ///
21- /// This will synchronise with the textfield's first responder state - it will get updated
22- /// if the user taps on the textfield (making it first responder) or if the text field resigns
23- /// first responder status. It can also allow the containing view to manually control
24- /// the first responder state by setting it to true or false.
25- let isEditing : Binding < Bool >
21+ /// If the text field becomes or resigns first responder as a result of a user interaction,
22+ /// this will be updated to `.current` or `.resigned` when the text field indicates
23+ /// that it has started or finished editing.
24+ ///
25+ /// You can programatically set this to a value of `.become` to become first responder
26+ /// or `.resign` to resign first responder. Programatically setting it to any other value
27+ /// will not have any effect on the first responder state (it can only become `.current`
28+ /// or `.resigned` when the system indicates that its responder state has changed).
29+ let firstResponderState : Binding < FirstResponderState >
2630
2731 /// Enables secure text entry.
2832 ///
@@ -80,7 +84,8 @@ public struct ResponsiveTextField {
8084 public init (
8185 placeholder: String ,
8286 text: Binding < String > ,
83- isEditing: Binding < Bool > ,
87+ //isEditing: Binding<Bool>,
88+ firstResponderState: Binding < FirstResponderState > ,
8489 isSecure: Bool = false ,
8590 configuration: Configuration = . empty,
8691 handleReturn: ( ( ) -> Void ) ? = nil ,
@@ -89,7 +94,7 @@ public struct ResponsiveTextField {
8994 ) {
9095 self . placeholder = placeholder
9196 self . text = text
92- self . isEditing = isEditing
97+ self . firstResponderState = firstResponderState
9398 self . isSecure = isSecure
9499 self . configuration = configuration
95100 self . handleReturn = handleReturn
@@ -102,6 +107,13 @@ public struct ResponsiveTextField {
102107 callback ( )
103108 shouldUpdateView = true
104109 }
110+
111+ public enum FirstResponderState : Equatable {
112+ case resigned
113+ case become
114+ case current
115+ case resign
116+ }
105117}
106118
107119// MARK: - UIViewRepresentable
@@ -141,10 +153,10 @@ extension ResponsiveTextField: UIViewRepresentable {
141153 uiView. returnKeyType = returnKeyType
142154 uiView. font = font
143155
144- switch ( uiView. isFirstResponder, isEditing . wrappedValue) {
145- case ( true , false ) :
156+ switch ( uiView. isFirstResponder, firstResponderState . wrappedValue) {
157+ case ( true , . resign ) :
146158 uiView. resignFirstResponder ( )
147- case ( false , true ) :
159+ case ( false , . become ) :
148160 uiView. becomeFirstResponder ( )
149161 default :
150162 break
@@ -158,20 +170,20 @@ extension ResponsiveTextField: UIViewRepresentable {
158170 var text : String
159171
160172 @Binding
161- var isEditing : Bool
173+ var firstResponderState : FirstResponderState
162174
163175 init ( textField: ResponsiveTextField ) {
164176 self . parent = textField
165177 self . _text = textField. text
166- self . _isEditing = textField. isEditing
178+ self . _firstResponderState = textField. firstResponderState
167179 }
168180
169181 public func textFieldDidBeginEditing( _ textField: UITextField ) {
170- parent. skippingViewUpdates { self . isEditing = true }
182+ parent. skippingViewUpdates { self . firstResponderState = . current }
171183 }
172184
173185 public func textFieldDidEndEditing( _ textField: UITextField ) {
174- parent. skippingViewUpdates { self . isEditing = false }
186+ parent. skippingViewUpdates { self . firstResponderState = . resigned }
175187 }
176188
177189 public func textFieldShouldReturn( _ textField: UITextField ) -> Bool {
@@ -309,13 +321,13 @@ struct ResponsiveTextField_Previews: PreviewProvider {
309321 var text : String = " "
310322
311323 @State
312- var isEditing : Bool = false
324+ var firstResponderState : ResponsiveTextField . FirstResponderState = . resigned
313325
314326 var body : some View {
315327 ResponsiveTextField (
316328 placeholder: " Placeholder " ,
317329 text: $text,
318- isEditing : $isEditing ,
330+ firstResponderState : $firstResponderState ,
319331 configuration: configuration,
320332 shouldChange: { $1. count <= 10 }
321333 )
0 commit comments