@@ -22,13 +22,20 @@ public struct FloatingLabelTextField: View {
2222 @Binding private var textFieldValue : String
2323 @State fileprivate var isSelected : Bool = false
2424 @Binding private var validtionChecker : Bool
25+
2526 private var currentError : TextFieldValidator {
27+ if notifier. isRequiredField && isShowError && textFieldValue. isEmpty {
28+ return TextFieldValidator ( condition: false , errorMessage: notifier. requiredFieldMessage)
29+ }
30+
2631 if let firstError = notifier. arrValidator. filter ( { !$0. condition} ) . first {
2732 return firstError
2833 }
2934 return TextFieldValidator ( condition: true , errorMessage: " " )
3035 }
3136
37+ @State fileprivate var isShowError : Bool = false
38+
3239 //MARK: Observed Object
3340 @ObservedObject private var notifier = FloatingLabelTextFieldNotifier ( )
3441
@@ -61,6 +68,7 @@ public struct FloatingLabelTextField: View {
6168 SecureField ( " " , text: $textFieldValue. animation ( ) ) {
6269 }
6370 . onTapGesture {
71+ self . isShowError = self . notifier. isRequiredField
6472 self . validtionChecker = self . currentError. condition
6573 self . editingChanged ( self . isSelected)
6674 if !self . isSelected {
@@ -71,6 +79,7 @@ public struct FloatingLabelTextField: View {
7179 self . isSelected = self . notifier. isSecureTextEntry
7280 currentTextField. addAction ( for: . editingDidEnd) {
7381 self . isSelected = false
82+ self . isShowError = self . notifier. isRequiredField
7483 self . commit ( )
7584 }
7685 }
@@ -85,8 +94,10 @@ public struct FloatingLabelTextField: View {
8594 self . isSelected = isChanged
8695 self . validtionChecker = self . currentError. condition
8796 self . editingChanged ( isChanged)
97+ self . isShowError = self . notifier. isRequiredField
8898
8999 } , onCommit: {
100+ self . isShowError = self . notifier. isRequiredField
90101 self . validtionChecker = self . currentError. condition
91102 self . commit ( )
92103 } )
@@ -97,6 +108,15 @@ public struct FloatingLabelTextField: View {
97108 }
98109 }
99110
111+ // MARK: Top error and title lable view
112+ var topTitleLable : some View {
113+ Text ( ( self . currentError. condition || !notifier. isShowError) ? placeholderText : self . currentError. errorMessage)
114+ . frame ( minWidth: 0 , maxWidth: . infinity, minHeight: 0 , maxHeight: . infinity, alignment: notifier. textAlignment. getAlignment ( ) )
115+ . animation ( . default)
116+ . foregroundColor ( ( self . currentError. condition || !notifier. isShowError) ? ( self . isSelected ? notifier. selectedTitleColor : notifier. titleColor) : notifier. errorColor)
117+ . font ( notifier. titleFont)
118+ }
119+
100120 // MARK: Bottom Line View
101121 var bottomLine : some View {
102122 Divider ( )
@@ -108,13 +128,13 @@ public struct FloatingLabelTextField: View {
108128 VStack ( ) {
109129 ZStack ( alignment: . bottomLeading) {
110130
111- Text ( ( self . currentError . condition || !notifier . isShowError ) ? placeholderText : self . currentError . errorMessage )
112- . frame ( minWidth : 0 , maxWidth : . infinity , minHeight : 0 , maxHeight : . infinity , alignment : notifier . textAlignment . getAlignment ( ) )
113- . animation ( . default )
114- . foregroundColor ( ( self . currentError . condition || !notifier . isShowError ) ? ( self . isSelected ? notifier . selectedTitleColor : notifier . titleColor ) : notifier . errorColor )
115- . padding ( . bottom , CGFloat ( !textFieldValue . isEmpty ? notifier . spaceBetweenTitleText : 0 ) )
116- . opacity ( textFieldValue. isEmpty ? 0 : 1 )
117- . font ( notifier . titleFont )
131+ //Top error and title lable view
132+ if notifier . isShowError && self . isShowError && textFieldValue . isEmpty {
133+ self . topTitleLable . padding ( . bottom , CGFloat ( notifier . spaceBetweenTitleText ) ) . opacity ( 1 )
134+
135+ } else {
136+ self . topTitleLable . padding ( . bottom , CGFloat ( !textFieldValue . isEmpty ? notifier . spaceBetweenTitleText : 0 ) ) . opacity ( ( textFieldValue. isEmpty) ? 0 : 1 )
137+ }
118138
119139 HStack {
120140 // Left View
@@ -303,6 +323,13 @@ extension FloatingLabelTextField {
303323 notifier. errorColor = color
304324 return self
305325 }
326+
327+ /// Sets the field is required or not with message.
328+ public func isRequiredField( _ required: Bool , with message: String ) -> Self {
329+ notifier. isRequiredField = required
330+ notifier. requiredFieldMessage = message
331+ return self
332+ }
306333}
307334
308335//MARK: Preview
0 commit comments