Skip to content

Commit

Permalink
Update code and added new properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kishanraja committed Apr 18, 2021
1 parent 2b52de9 commit 6ae2dbb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
17 changes: 12 additions & 5 deletions Example/FloatingLabelTextFieldSwiftUI/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import FloatingLabelTextFieldSwiftUI
struct ContentView: View {

@State private var firstName: String = ""
@State private var isValidFirstName: Bool = false

@State private var lastName: String = ""
@State private var isValidLastName: Bool = false

@State private var mobileNumber: String = ""
@State private var email: String = ""
@State private var isValidEmail: Bool = false
Expand Down Expand Up @@ -41,7 +45,7 @@ struct ContentView: View {
VStack {

HStack(spacing: 20) {
FloatingLabelTextField($firstName, placeholder: "First Name", editingChanged: { (isChanged) in
FloatingLabelTextField($firstName, validtionChecker: $isValidFirstName, placeholder: "First Name", editingChanged: { (isChanged) in

}) {

Expand All @@ -56,7 +60,7 @@ struct ContentView: View {
.modifier(ThemeTextField())


FloatingLabelTextField($lastName, placeholder: "Last Name", editingChanged: { (isChanged) in
FloatingLabelTextField($lastName, validtionChecker: $isValidLastName, placeholder: "Last Name", editingChanged: { (isChanged) in

}) {

Expand Down Expand Up @@ -121,12 +125,15 @@ struct ContentView: View {
})
.isSecureTextEntry(!self.isPasswordShow)
.modifier(ThemeTextField())
// SecureField("", text: $password)
// Text(password)

Button(action: {
self.endEditing(true)

if self.isValidEmail {
print("First Name--->", isValidFirstName)
print("Last Name--->", isValidLastName)
print("Email--->", isValidEmail)

if self.isValidEmail && isValidFirstName && isValidLastName {
print("Valid field")

} else {
Expand Down
45 changes: 41 additions & 4 deletions Sources/FloatingLabelTextFieldSwiftUI/FloatingLabelTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public struct FloatingLabelTextField: View {

@State fileprivate var isShowError: Bool = false

@State fileprivate var isFocused: Bool = false

//MARK: Observed Object
@ObservedObject private var notifier = FloatingLabelTextFieldNotifier()

Expand All @@ -57,7 +59,7 @@ public struct FloatingLabelTextField: View {
var centerTextFieldView: some View {
ZStack(alignment: notifier.textAlignment.getAlignment()) {

if textFieldValue.isEmpty {
if (notifier.isAnimateOnFocus ? !isSelected : textFieldValue.isEmpty) {
Text(placeholderText)
.font(notifier.placeholderFont)
.multilineTextAlignment(notifier.textAlignment)
Expand All @@ -77,22 +79,31 @@ public struct FloatingLabelTextField: View {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
if let currentResponder = UIResponder.currentFirstResponder, let currentTextField = currentResponder.globalView as? UITextField{
arrTextFieldEditActions = self.notifier.arrTextFieldEditActions
self.isSelected = self.notifier.isSecureTextEntry
withAnimation {
self.isSelected = self.notifier.isSecureTextEntry
}
currentTextField.addAction(for: .editingDidEnd) {
self.isSelected = false
self.isShowError = self.notifier.isRequiredField
self.validtionChecker = self.currentError.condition
self.commit()
arrTextFieldEditActions = []
}
}
}
}
.disabled(self.notifier.disabled)
.allowsHitTesting(self.notifier.allowsHitTesting)
.font(notifier.font)
.multilineTextAlignment(notifier.textAlignment)
.foregroundColor((self.currentError.condition || !notifier.isShowError) ? (isSelected ? notifier.selectedTextColor : notifier.textColor) : notifier.errorColor)

} else {
TextField("", text: $textFieldValue.animation(), onEditingChanged: { (isChanged) in
self.isSelected = isChanged
withAnimation {
self.isSelected = isChanged
}

self.validtionChecker = self.currentError.condition
self.editingChanged(isChanged)
self.isShowError = self.notifier.isRequiredField
Expand All @@ -101,7 +112,10 @@ public struct FloatingLabelTextField: View {
self.isShowError = self.notifier.isRequiredField
self.validtionChecker = self.currentError.condition
self.commit()
arrTextFieldEditActions = []
})
.disabled(self.notifier.disabled)
.allowsHitTesting(self.notifier.allowsHitTesting)
.multilineTextAlignment(notifier.textAlignment)
.font(notifier.font)
.foregroundColor((self.currentError.condition || !notifier.isShowError) ? (isSelected ? notifier.selectedTextColor : notifier.textColor) : notifier.errorColor)
Expand Down Expand Up @@ -130,7 +144,7 @@ public struct FloatingLabelTextField: View {
ZStack(alignment: .bottomLeading) {

//Top error and title lable view
if notifier.isShowError && self.isShowError && textFieldValue.isEmpty {
if notifier.isShowError && self.isShowError && textFieldValue.isEmpty || (notifier.isAnimateOnFocus && isSelected){
self.topTitleLable.padding(.bottom, CGFloat(notifier.spaceBetweenTitleText)).opacity(1)

} else {
Expand Down Expand Up @@ -202,6 +216,18 @@ extension FloatingLabelTextField {
notifier.isSecureTextEntry = isSecure
return self
}

/// Whether users can interact with this.
public func disabled(_ isDisabled: Bool) -> Self {
notifier.disabled = isDisabled
return self
}

/// Whether this view participates in hit test operations.
public func allowsHitTesting(_ isAllowsHitTesting: Bool) -> Self {
notifier.allowsHitTesting = isAllowsHitTesting
return self
}
}

//MARK: Line Property Funcation
Expand Down Expand Up @@ -342,3 +368,14 @@ extension FloatingLabelTextField {
return self
}
}

//MARK: Animation Style Funcation
@available(iOS 13.0, *)
extension FloatingLabelTextField {
/// Enable the placeholder label when the textfield is focused.
public func enablePlaceholderOnFocus(_ isEanble: Bool) -> Self {
notifier.isAnimateOnFocus = isEanble
return self
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class FloatingLabelTextFieldNotifier: ObservableObject {
//MARK: Other Properties
@Published var spaceBetweenTitleText: Double = 15
@Published var isSecureTextEntry: Bool = false
@Published var disabled: Bool = false
@Published var allowsHitTesting: Bool = true

//MARK: Error Properties
@Published var isShowError: Bool = false
Expand All @@ -51,4 +53,7 @@ class FloatingLabelTextFieldNotifier: ObservableObject {

//MARK: Action Editing Properties
@Published var arrTextFieldEditActions: [TextFieldEditActions] = []

//MARK: Animation Style Properties
@Published var isAnimateOnFocus: Bool = false
}

0 comments on commit 6ae2dbb

Please sign in to comment.