Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature / Error presentation condition and animation improvement #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 52 additions & 22 deletions Sources/CleevioUI/Views/Inputs/CleevioInputField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public struct CleevioInputField<
var contentPadding: EdgeInsets
var font: Font?

/// Condition defining when the error label should be presented.
let errorPresentationCondition: (_ isFocused: Bool, _ error: String?) -> Bool

public init(
@ViewBuilder title: @escaping (InputFieldState) -> Title,
@ViewBuilder foreground: @escaping (InputFieldState) -> Color,
Expand All @@ -79,12 +82,37 @@ public struct CleevioInputField<
self.isExternallyFocused = isFocused
self.contentPadding = contentPadding
self.font = font

self.errorPresentationCondition = { _, error in error != nil }
tomasbabulak marked this conversation as resolved.
Show resolved Hide resolved
}

public init(
@ViewBuilder title: @escaping (InputFieldState) -> Title,
@ViewBuilder foreground: @escaping (InputFieldState) -> Color,
@ViewBuilder background: @escaping (InputFieldState) -> Background,
@ViewBuilder overlay: @escaping (InputFieldState) -> Overlay,
@ViewBuilder errorLabel: @escaping (String) -> ErrorLabel,
isFocused: Bool,
contentPadding: EdgeInsets,
font: Font?,
errorPresentationCondition: @escaping (_ isFocused: Bool, _ error: String?) -> Bool
) {
self.title = title
self.foreground = foreground
self.background = background
self.overlay = overlay
self.errorLabel = errorLabel
self.isExternallyFocused = isFocused
self.contentPadding = contentPadding
self.font = font

self.errorPresentationCondition = errorPresentationCondition
}
}

@usableFromInline
@ViewBuilder var content: (InputFieldState) -> Content

public var configuration: Configuration

@FocusState private var isFocused: Bool
Expand All @@ -101,41 +129,43 @@ public struct CleevioInputField<
}

var state: InputFieldState {
.init(
isFocused: isFocused || configuration.isExternallyFocused,
let isFocused = isFocused || configuration.isExternallyFocused
return .init(
isFocused: isFocused,
isEnabled: isEnabled,
isError: error != nil
isError: configuration.errorPresentationCondition(isFocused, error)
)
}

var isErrorPresented: Bool {
state.isError
}

public var body: some View {
VStack(alignment: .leading, spacing: 6) {
tomasbabulak marked this conversation as resolved.
Show resolved Hide resolved
configuration.title(state)

content(state)
.focused($isFocused)
.padding(configuration.contentPadding)
.background(configuration.background(state))
.overlay { configuration.overlay(state) }
.contentShape(Rectangle())
.onTapGesture {
isFocused = true
}
VStack(alignment: .leading, spacing: .zero) {
content(state)
.focused($isFocused)
.padding(configuration.contentPadding)
.background(configuration.background(state))
.overlay { configuration.overlay(state) }
.contentShape(Rectangle())
.onTapGesture {
isFocused = true
}

if let error {
configuration.errorLabel(error)
.transition(
.asymmetric(
insertion: .move(edge: .leading),
removal: .move(edge: .leading)
)
)
configuration.errorLabel(error ?? "")
tomasbabulak marked this conversation as resolved.
Show resolved Hide resolved
.opacity(isErrorPresented ? 1 : 0)
tomasbabulak marked this conversation as resolved.
Show resolved Hide resolved
.frame(height: isErrorPresented ? nil : 0)
.padding(.top, isErrorPresented ? 6 : 0)
}
}
.foregroundStyle(configuration.foreground(state))
.tint(configuration.foreground(state))
.font(configuration.font)
.animation(.default, value: error)
.animation(.default, value: isErrorPresented)
.animation(.easeInOut, value: state.isFocused)
.animation(.easeInOut, value: isEnabled)
}
Expand Down