From 6171c600cf81f7de5927a69778c0585f2714077d Mon Sep 17 00:00:00 2001 From: Pavel Holec Date: Thu, 15 Feb 2024 13:23:18 +0100 Subject: [PATCH] Remove messageHeight from field components --- Sources/Orbit/Components/InputField.swift | 13 ++------ Sources/Orbit/Components/Select.swift | 10 ++---- Sources/Orbit/Components/Textarea.swift | 8 ++--- .../Orbit/Support/Forms/FieldWrapper.swift | 33 +++++++------------ 4 files changed, 18 insertions(+), 46 deletions(-) diff --git a/Sources/Orbit/Components/InputField.swift b/Sources/Orbit/Components/InputField.swift index 8ff24a457f5..0f1a5acd4bc 100644 --- a/Sources/Orbit/Components/InputField.swift +++ b/Sources/Orbit/Components/InputField.swift @@ -68,7 +68,6 @@ public struct InputField: 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 @@ -79,7 +78,7 @@ public struct InputField: 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: { @@ -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, @@ -181,8 +179,7 @@ public extension InputField { labelStyle: InputLabelStyle = .default, isSecure: Bool = false, passwordStrength: PasswordStrengthIndicator.PasswordStrength? = nil, - message: Message? = nil, - messageHeight: Binding = .constant(0) + message: Message? = nil ) where Prefix == Icon, Suffix == Icon { self.init( label, @@ -192,8 +189,7 @@ public extension InputField { labelStyle: labelStyle, isSecure: isSecure, passwordStrength: passwordStrength, - message: message, - messageHeight: messageHeight + message: message ) { Icon(prefix) } suffix: { @@ -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, @@ -215,7 +210,6 @@ public extension InputField { isSecure: Bool = false, passwordStrength: PasswordStrengthIndicator.PasswordStrength? = nil, message: Message? = nil, - messageHeight: Binding = .constant(0), @ViewBuilder prefix: () -> Prefix, @ViewBuilder suffix: () -> Suffix = { EmptyView() } ) { @@ -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() } diff --git a/Sources/Orbit/Components/Select.swift b/Sources/Orbit/Components/Select.swift index c86049c42b8..3ce6a595d9c 100644 --- a/Sources/Orbit/Components/Select.swift +++ b/Sources/Orbit/Components/Select.swift @@ -40,8 +40,6 @@ public struct Select: View { @Environment(\.isEnabled) private var isEnabled @Environment(\.isHapticsEnabled) private var isHapticsEnabled - - @Binding private var messageHeight: CGFloat private let label: String private let value: String? @@ -54,7 +52,7 @@ public struct Select: 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)) @@ -131,7 +129,6 @@ public extension Select { state: InputState = .default, labelStyle: InputLabelStyle = .default, message: Message? = nil, - messageHeight: Binding = .constant(0), action: @escaping () -> Void ) where Prefix == Icon, Suffix == Icon { self.init( @@ -140,8 +137,7 @@ public extension Select { prompt: prompt, state: state, labelStyle: labelStyle, - message: message, - messageHeight: messageHeight + message: message ) { action() } prefix: { @@ -161,7 +157,6 @@ public extension Select { state: InputState = .default, labelStyle: InputLabelStyle = .default, message: Message? = nil, - messageHeight: Binding = .constant(0), action: @escaping () -> Void, @ViewBuilder prefix: () -> Prefix, @ViewBuilder suffix: () -> Suffix = { Icon(.chevronDown) } @@ -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() diff --git a/Sources/Orbit/Components/Textarea.swift b/Sources/Orbit/Components/Textarea.swift index ef227c77230..b402239419d 100644 --- a/Sources/Orbit/Components/Textarea.swift +++ b/Sources/Orbit/Components/Textarea.swift @@ -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 @@ -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 @@ -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, prompt: String = "", state: InputState = .default, - message: Message? = nil, - messageHeight: Binding = .constant(0) + message: Message? = nil ) { self.label = label self._value = value self.prompt = prompt self.state = state self.message = message - self._messageHeight = messageHeight } } diff --git a/Sources/Orbit/Support/Forms/FieldWrapper.swift b/Sources/Orbit/Support/Forms/FieldWrapper.swift index e9bfedfd603..f8faf474bd6 100644 --- a/Sources/Orbit/Support/Forms/FieldWrapper.swift +++ b/Sources/Orbit/Support/Forms/FieldWrapper.swift @@ -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: 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) { @@ -20,13 +18,11 @@ public struct FieldWrapper: 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) } } } @@ -40,13 +36,11 @@ public extension FieldWrapper { /// ``FieldLabel`` is a default component for constructing custom label. init( message: Message? = nil, - messageHeight: Binding = .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() @@ -59,13 +53,11 @@ public extension FieldWrapper where Label == FieldLabel { init( _ label: String, message: Message? = nil, - messageHeight: Binding = .constant(0), @ViewBuilder content: () -> Content, @ViewBuilder footer: () -> Footer = { EmptyView() } ) { self.init( message: message, - messageHeight: messageHeight, content: content, label: { FieldLabel(label) @@ -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") }