Skip to content

Commit

Permalink
Remove messageHeight from field components
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelHolec committed Feb 15, 2024
1 parent 213db5c commit 20070fe
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 46 deletions.
13 changes: 3 additions & 10 deletions Sources/Orbit/Components/InputField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public struct InputField<Prefix: View, Suffix: View>: View, TextFieldBuildable {
private let isSecure: Bool
private let passwordStrength: PasswordStrengthIndicator.PasswordStrength?
private let message: Message?
@Binding private var messageHeight: CGFloat

// Builder properties (keyboard related)
var autocapitalizationType: UITextAutocapitalizationType = .none
Expand All @@ -79,7 +78,7 @@ public struct InputField<Prefix: View, Suffix: View>: View, TextFieldBuildable {
var shouldDeleteBackwardAction: (String) -> Bool = { _ in true }

public var body: some View {
FieldWrapper(defaultLabel, message: message, messageHeight: $messageHeight) {
FieldWrapper(defaultLabel, message: message) {
InputContent(state: state, label: compactLabel, message: message, isFocused: isFocused) {
textField
} prefix: {
Expand Down Expand Up @@ -170,7 +169,6 @@ public extension InputField {
///
/// - Parameters:
/// - message: Optional message below the text field.
/// - messageHeight: Binding to the current height of the optional message.
init(
_ label: String = "",
value: Binding<String>,
Expand All @@ -181,8 +179,7 @@ public extension InputField {
labelStyle: InputLabelStyle = .default,
isSecure: Bool = false,
passwordStrength: PasswordStrengthIndicator.PasswordStrength? = nil,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0)
message: Message? = nil
) where Prefix == Icon, Suffix == Icon {
self.init(
label,
Expand All @@ -192,8 +189,7 @@ public extension InputField {
labelStyle: labelStyle,
isSecure: isSecure,
passwordStrength: passwordStrength,
message: message,
messageHeight: messageHeight
message: message
) {
Icon(prefix)
} suffix: {
Expand All @@ -205,7 +201,6 @@ public extension InputField {
///
/// - Parameters:
/// - message: Optional message below the text field.
/// - messageHeight: Binding to the current height of the optional message.
init(
_ label: String = "",
value: Binding<String>,
Expand All @@ -215,7 +210,6 @@ public extension InputField {
isSecure: Bool = false,
passwordStrength: PasswordStrengthIndicator.PasswordStrength? = nil,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0),
@ViewBuilder prefix: () -> Prefix,
@ViewBuilder suffix: () -> Suffix = { EmptyView() }
) {
Expand All @@ -227,7 +221,6 @@ public extension InputField {
self.isSecure = isSecure
self.passwordStrength = passwordStrength
self.message = message
self._messageHeight = messageHeight
self.prefix = prefix()
self.suffix = suffix()
}
Expand Down
10 changes: 2 additions & 8 deletions Sources/Orbit/Components/Select.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public struct Select<Prefix: View, Suffix: View>: View {

@Environment(\.isEnabled) private var isEnabled
@Environment(\.isHapticsEnabled) private var isHapticsEnabled

@Binding private var messageHeight: CGFloat

private let label: String
private let value: String?
Expand All @@ -54,7 +52,7 @@ public struct Select<Prefix: View, Suffix: View>: View {
@ViewBuilder private let suffix: Suffix

public var body: some View {
FieldWrapper(defaultLabel, message: message, messageHeight: $messageHeight) {
FieldWrapper(defaultLabel, message: message) {
SwiftUI.Button {
if isHapticsEnabled {
HapticsProvider.sendHapticFeedback(.light(0.5))
Expand Down Expand Up @@ -131,7 +129,6 @@ public extension Select {
state: InputState = .default,
labelStyle: InputLabelStyle = .default,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0),
action: @escaping () -> Void
) where Prefix == Icon, Suffix == Icon {
self.init(
Expand All @@ -140,8 +137,7 @@ public extension Select {
prompt: prompt,
state: state,
labelStyle: labelStyle,
message: message,
messageHeight: messageHeight
message: message
) {
action()
} prefix: {
Expand All @@ -161,7 +157,6 @@ public extension Select {
state: InputState = .default,
labelStyle: InputLabelStyle = .default,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0),
action: @escaping () -> Void,
@ViewBuilder prefix: () -> Prefix,
@ViewBuilder suffix: () -> Suffix = { Icon(.chevronDown) }
Expand All @@ -172,7 +167,6 @@ public extension Select {
self.state = state
self.labelStyle = labelStyle
self.message = message
self._messageHeight = messageHeight
self.action = action
self.prefix = prefix()
self.suffix = suffix()
Expand Down
8 changes: 2 additions & 6 deletions Sources/Orbit/Components/Textarea.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public struct Textarea: View, TextFieldBuildable {
private let state: InputState

private let message: Message?
@Binding private var messageHeight: CGFloat

// Builder properties (keyboard related)
var autocapitalizationType: UITextAutocapitalizationType = .none
Expand All @@ -48,7 +47,7 @@ public struct Textarea: View, TextFieldBuildable {
var shouldDeleteBackwardAction: (String) -> Bool = { _ in true }

public var body: some View {
FieldWrapper(label, message: message, messageHeight: $messageHeight) {
FieldWrapper(label, message: message) {
InputContent(state: state, message: message, isFocused: isFocused) {
textView
.alignmentGuide(.firstTextBaseline) { dimension in
Expand Down Expand Up @@ -95,21 +94,18 @@ public extension Textarea {
///
/// - Parameters:
/// - message: Optional message below the text field.
/// - messageHeight: Binding to the current height of the optional message.
init(
_ label: String = "",
value: Binding<String>,
prompt: String = "",
state: InputState = .default,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0)
message: Message? = nil
) {
self.label = label
self._value = value
self.prompt = prompt
self.state = state
self.message = message
self._messageHeight = messageHeight
}
}

Expand Down
33 changes: 11 additions & 22 deletions Sources/Orbit/Support/Forms/FieldWrapper.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import SwiftUI

/// Orbit support component that orovides label and message around input field.
/// Orbit support component that provides label and message around the form field.
public struct FieldWrapper<Label: View, Content: View, Footer: View>: View {

@Binding private var messageHeight: CGFloat

private let message: Message?
@ViewBuilder private let content: Content
@ViewBuilder private let label: Label
@ViewBuilder private let footer: Footer
@ViewBuilder private let content: Content

public var body: some View {
VStack(alignment: .leading, spacing: 0) {
Expand All @@ -20,13 +18,11 @@ public struct FieldWrapper<Label: View, Content: View, Footer: View>: View {

content

ContentHeightReader(height: $messageHeight) {
VStack(alignment: .leading, spacing: 0) {
footer
VStack(alignment: .leading, spacing: 0) {
footer

FieldMessage(message)
.padding(.top, .xxSmall)
}
FieldMessage(message)
.padding(.top, .xxSmall)
}
}
}
Expand All @@ -40,13 +36,11 @@ public extension FieldWrapper {
/// ``FieldLabel`` is a default component for constructing custom label.
init(
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0),
@ViewBuilder content: () -> Content,
@ViewBuilder label: () -> Label,
@ViewBuilder footer: () -> Footer = { EmptyView() }
) {
self.message = message
self._messageHeight = messageHeight
self.content = content()
self.label = label()
self.footer = footer()
Expand All @@ -59,13 +53,11 @@ public extension FieldWrapper where Label == FieldLabel {
init(
_ label: String,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0),
@ViewBuilder content: () -> Content,
@ViewBuilder footer: () -> Footer = { EmptyView() }
) {
self.init(
message: message,
messageHeight: messageHeight,
content: content,
label: {
FieldLabel(label)
Expand Down Expand Up @@ -100,32 +92,29 @@ struct FieldWrapperPreviews: PreviewProvider {
contentPlaceholder
}

StateWrapper((true, true, CGFloat(0), false)) { state in
StateWrapper((true, true, false)) { state in
VStack(alignment: .leading, spacing: .large) {
FieldWrapper(
state.0.wrappedValue ? "Form Field Label" : "",
message: state.1.wrappedValue ? .error("Error message") : .none,
messageHeight: state.2
message: state.1.wrappedValue ? .error("Error message") : .none
) {
contentPlaceholder
}

Text("Message height: \(state.2.wrappedValue)")

HStack(spacing: .medium) {
Button("Toggle label") {
state.0.wrappedValue.toggle()
state.3.wrappedValue.toggle()
state.2.wrappedValue.toggle()
}
Button("Toggle message") {
state.1.wrappedValue.toggle()
state.3.wrappedValue.toggle()
state.2.wrappedValue.toggle()
}
}

Spacer()
}
.animation(.easeOut(duration: 1), value: state.3.wrappedValue)
.animation(.easeOut(duration: 1), value: state.2.wrappedValue)
}
.previewDisplayName("Live preview")
}
Expand Down

0 comments on commit 20070fe

Please sign in to comment.