Skip to content

Commit ff01653

Browse files
authored
Fixed "Modifying state during view update, this will cause undefined behavior." (#28)
1 parent b9aab8f commit ff01653

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

Sources/FloatingLabelTextFieldSwiftUI/FloatingLabelTextField.swift

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,35 @@ public protocol FloatingLabelTextFieldStyle {
1717
//MARK: FloatingLabelTextField View
1818
@available(iOS 13.0, *)
1919
public struct FloatingLabelTextField: View {
20-
20+
2121
//MARK: Binding Property
2222
@Binding private var textFieldValue: String
23-
@State fileprivate var isSelected: Bool = false
23+
@State var isSelected: Bool = false
2424
@Binding private var validtionChecker: Bool
25-
25+
2626
private var currentError: TextFieldValidator {
2727
if notifier.isRequiredField && isShowError && textFieldValue.isEmpty {
2828
return TextFieldValidator(condition: false, errorMessage: notifier.requiredFieldMessage)
2929
}
30-
30+
3131
if let firstError = notifier.arrValidator.filter({!$0.condition}).first {
3232
return firstError
3333
}
3434
return TextFieldValidator(condition: true, errorMessage: "")
3535
}
36-
37-
@State fileprivate var isShowError: Bool = false
38-
36+
37+
@State var isShowError: Bool = false
38+
3939
@State fileprivate var isFocused: Bool = false
40-
40+
4141
//MARK: Observed Object
4242
@ObservedObject private var notifier = FloatingLabelTextFieldNotifier()
43-
43+
4444
//MARK: Properties
4545
private var placeholderText: String = ""
4646
private var editingChanged: (Bool) -> () = { _ in }
4747
private var commit: () -> () = { }
48-
48+
4949
//MARK: Init
5050
public init(_ text: Binding<String>, validtionChecker: Binding<Bool>? = nil, placeholder: String = "", editingChanged: @escaping (Bool)->() = { _ in }, commit: @escaping ()->() = { }) {
5151
self._textFieldValue = text
@@ -54,18 +54,18 @@ public struct FloatingLabelTextField: View {
5454
self.commit = commit
5555
self._validtionChecker = validtionChecker ?? Binding.constant(false)
5656
}
57-
57+
5858
// MARK: Center View
5959
var centerTextFieldView: some View {
6060
ZStack(alignment: notifier.textAlignment.getAlignment()) {
61-
61+
6262
if (notifier.isAnimateOnFocus ? (!isSelected && textFieldValue.isEmpty) : textFieldValue.isEmpty) {
6363
Text(placeholderText)
6464
.font(notifier.placeholderFont)
6565
.multilineTextAlignment(notifier.textAlignment)
6666
.foregroundColor(notifier.placeholderColor)
6767
}
68-
68+
6969
if notifier.isSecureTextEntry {
7070
SecureField("", text: $textFieldValue.animation()) {
7171
}
@@ -97,13 +97,13 @@ public struct FloatingLabelTextField: View {
9797
.font(notifier.font)
9898
.multilineTextAlignment(notifier.textAlignment)
9999
.foregroundColor((self.currentError.condition || !notifier.isShowError) ? (isSelected ? notifier.selectedTextColor : notifier.textColor) : notifier.errorColor)
100-
100+
101101
} else {
102102
TextField("", text: $textFieldValue.animation(), onEditingChanged: { (isChanged) in
103103
withAnimation {
104104
self.isSelected = isChanged
105105
}
106-
106+
107107
self.validtionChecker = self.currentError.condition
108108
self.editingChanged(isChanged)
109109
self.isShowError = self.notifier.isRequiredField
@@ -122,7 +122,7 @@ public struct FloatingLabelTextField: View {
122122
}
123123
}
124124
}
125-
125+
126126
// MARK: Top error and title lable view
127127
var topTitleLable: some View {
128128
Text((self.currentError.condition || !notifier.isShowError) ? placeholderText : self.currentError.errorMessage)
@@ -131,48 +131,48 @@ public struct FloatingLabelTextField: View {
131131
.foregroundColor((self.currentError.condition || !notifier.isShowError) ? (self.isSelected ? notifier.selectedTitleColor : notifier.titleColor) : notifier.errorColor)
132132
.font(notifier.titleFont)
133133
}
134-
134+
135135
// MARK: Bottom Line View
136136
var bottomLine: some View {
137137
Divider()
138138
.frame(height: self.isSelected ? notifier.selectedLineHeight : notifier.lineHeight, alignment: .leading)
139139
}
140-
140+
141141
//MARK: Body View
142142
public var body: some View {
143143
VStack () {
144144
ZStack(alignment: .bottomLeading) {
145-
145+
146146
//Top error and title lable view
147147
if notifier.isShowError && self.isShowError && textFieldValue.isEmpty || (notifier.isAnimateOnFocus && isSelected){
148148
self.topTitleLable.padding(.bottom, CGFloat(notifier.spaceBetweenTitleText)).opacity(1)
149-
149+
150150
} else {
151151
self.topTitleLable.padding(.bottom, CGFloat(!textFieldValue.isEmpty ? notifier.spaceBetweenTitleText : 0)).opacity((textFieldValue.isEmpty) ? 0 : 1)
152152
}
153-
153+
154154
HStack {
155155
// Left View
156156
notifier.leftView
157-
157+
158158
// Center View
159159
centerTextFieldView
160-
160+
161161
//Right View
162162
notifier.rightView
163163
}
164164
}
165-
165+
166166
//MARK: Line View
167167
if textFieldValue.isEmpty || !notifier.isShowError {
168168
bottomLine
169169
.background((self.isSelected ? notifier.selectedLineColor : notifier.lineColor))
170-
170+
171171
} else {
172172
bottomLine
173173
.background((self.currentError.condition) ? (self.isSelected ? notifier.selectedLineColor : notifier.lineColor) : notifier.errorColor)
174174
}
175-
175+
176176
}
177177
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .bottomLeading)
178178
}
@@ -194,7 +194,7 @@ extension FloatingLabelTextField {
194194
notifier.leftView = AnyView(view())
195195
return self
196196
}
197-
197+
198198
/// Sets the right view.
199199
public func rightView<LRView: View>(@ViewBuilder _ view: @escaping () -> LRView) -> Self {
200200
notifier.rightView = AnyView(view())
@@ -210,19 +210,19 @@ extension FloatingLabelTextField {
210210
notifier.textAlignment = alignment
211211
return self
212212
}
213-
213+
214214
/// Sets the secure text entry for TextField.
215215
public func isSecureTextEntry(_ isSecure: Bool) -> Self {
216216
notifier.isSecureTextEntry = isSecure
217217
return self
218218
}
219-
219+
220220
/// Whether users can interact with this.
221221
public func disabled(_ isDisabled: Bool) -> Self {
222222
notifier.disabled = isDisabled
223223
return self
224224
}
225-
225+
226226
/// Whether this view participates in hit test operations.
227227
public func allowsHitTesting(_ isAllowsHitTesting: Bool) -> Self {
228228
notifier.allowsHitTesting = isAllowsHitTesting
@@ -238,19 +238,19 @@ extension FloatingLabelTextField {
238238
notifier.lineHeight = height
239239
return self
240240
}
241-
241+
242242
/// Sets the selected line height.
243243
public func selectedLineHeight(_ height: CGFloat) -> Self {
244244
notifier.selectedLineHeight = height
245245
return self
246246
}
247-
247+
248248
/// Sets the line color.
249249
public func lineColor(_ color: Color) -> Self {
250250
notifier.lineColor = color
251251
return self
252252
}
253-
253+
254254
/// Sets the selected line color.
255255
public func selectedLineColor(_ color: Color) -> Self {
256256
notifier.selectedLineColor = color
@@ -266,19 +266,19 @@ extension FloatingLabelTextField {
266266
notifier.titleColor = color
267267
return self
268268
}
269-
269+
270270
/// Sets the selected title color.
271271
public func selectedTitleColor(_ color: Color) -> Self {
272272
notifier.selectedTitleColor = color
273273
return self
274274
}
275-
275+
276276
/// Sets the title font.
277277
public func titleFont(_ font: Font) -> Self {
278278
notifier.titleFont = font
279279
return self
280280
}
281-
281+
282282
/// Sets the space between title and text.
283283
public func spaceBetweenTitleText(_ space: Double) -> Self {
284284
notifier.spaceBetweenTitleText = space
@@ -294,13 +294,13 @@ extension FloatingLabelTextField {
294294
notifier.textColor = color
295295
return self
296296
}
297-
297+
298298
/// Sets the selected text color.
299299
public func selectedTextColor(_ color: Color) -> Self {
300300
notifier.selectedTextColor = color
301301
return self
302302
}
303-
303+
304304
/// Sets the text font.
305305
public func textFont(_ font: Font) -> Self {
306306
notifier.font = font
@@ -316,7 +316,7 @@ extension FloatingLabelTextField {
316316
notifier.placeholderColor = color
317317
return self
318318
}
319-
319+
320320
/// Sets the placeholder font.
321321
public func placeholderFont(_ font: Font) -> Self {
322322
notifier.placeholderFont = font
@@ -332,25 +332,25 @@ extension FloatingLabelTextField {
332332
notifier.isShowError = show
333333
return self
334334
}
335-
335+
336336
/// Sets the validation conditions.
337337
public func addValidations(_ conditions: [TextFieldValidator]) -> Self {
338338
notifier.arrValidator.append(contentsOf: conditions)
339339
return self
340340
}
341-
341+
342342
/// Sets the validation condition.
343343
public func addValidation(_ condition: TextFieldValidator) -> Self {
344344
notifier.arrValidator.append(condition)
345345
return self
346346
}
347-
347+
348348
/// Sets the error color.
349349
public func errorColor(_ color: Color) -> Self {
350350
notifier.errorColor = color
351351
return self
352352
}
353-
353+
354354
/// Sets the field is required or not with message.
355355
public func isRequiredField(_ required: Bool, with message: String) -> Self {
356356
notifier.isRequiredField = required

0 commit comments

Comments
 (0)