From 74e5b487e1960cde374835af6812b07eb1f477e6 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 | 18 +++------- Sources/Orbit/Components/Select.swift | 14 ++------ Sources/Orbit/Components/Textarea.swift | 18 +++------- .../Orbit/Support/Forms/FieldWrapper.swift | 33 +++++++------------ 4 files changed, 22 insertions(+), 61 deletions(-) diff --git a/Sources/Orbit/Components/InputField.swift b/Sources/Orbit/Components/InputField.swift index 891bb0a6090..ff7fc3b869d 100644 --- a/Sources/Orbit/Components/InputField.swift +++ b/Sources/Orbit/Components/InputField.swift @@ -72,7 +72,6 @@ public struct InputField: 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 @@ -83,7 +82,7 @@ public struct InputField: var shouldDeleteBackwardAction: (String) -> Bool = { _ in true } public var body: some View { - FieldWrapper(message: message, messageHeight: $messageHeight) { + FieldWrapper(message: message) { InputContent(state: state, message: message, isFocused: isFocused) { textField } label: { @@ -202,7 +201,6 @@ public struct InputField: /// /// - Parameters: /// - message: Optional message below the text field. - /// - messageHeight: Binding to the current height of the optional message. public init( value: Binding, state: InputState = .default, @@ -210,7 +208,6 @@ public struct InputField: isSecure: Bool = false, passwordStrength: PasswordStrengthIndicator.PasswordStrength? = nil, message: Message? = nil, - messageHeight: Binding = .constant(0), @ViewBuilder label: () -> Label, @ViewBuilder prompt: () -> Prompt = { EmptyView() }, @ViewBuilder prefix: () -> Prefix = { EmptyView() }, @@ -222,7 +219,6 @@ public struct InputField: self.isSecure = isSecure self.passwordStrength = passwordStrength self.message = message - self._messageHeight = messageHeight self.label = label() self.prompt = prompt() self.prefix = prefix() @@ -237,7 +233,6 @@ public extension InputField where Label == Text, Prompt == Text, Prefix == Icon, /// /// - Parameters: /// - message: Optional message below the text field. - /// - messageHeight: Binding to the current height of the optional message. @_disfavoredOverload init( _ label: some StringProtocol = String(""), @@ -249,8 +244,7 @@ public extension InputField where Label == Text, Prompt == Text, Prefix == Icon, labelStyle: InputLabelStyle = .default, isSecure: Bool = false, passwordStrength: PasswordStrengthIndicator.PasswordStrength? = nil, - message: Message? = nil, - messageHeight: Binding = .constant(0) + message: Message? = nil ) { self.init( value: value, @@ -258,8 +252,7 @@ public extension InputField where Label == Text, Prompt == Text, Prefix == Icon, labelStyle: labelStyle, isSecure: isSecure, passwordStrength: passwordStrength, - message: message, - messageHeight: messageHeight + message: message ) { Text(label) } prompt: { @@ -275,7 +268,6 @@ public extension InputField where Label == Text, Prompt == Text, Prefix == Icon, /// /// - Parameters: /// - message: Optional message below the text field. - /// - messageHeight: Binding to the current height of the optional message. @_semantics("swiftui.init_with_localization") init( _ label: LocalizedStringKey = "", @@ -288,7 +280,6 @@ public extension InputField where Label == Text, Prompt == Text, Prefix == Icon, isSecure: Bool = false, passwordStrength: PasswordStrengthIndicator.PasswordStrength? = nil, message: Message? = nil, - messageHeight: Binding = .constant(0), tableName: String? = nil, bundle: Bundle? = nil, labelComment: StaticString? = nil @@ -299,8 +290,7 @@ public extension InputField where Label == Text, Prompt == Text, Prefix == Icon, labelStyle: labelStyle, isSecure: isSecure, passwordStrength: passwordStrength, - message: message, - messageHeight: messageHeight + message: message ) { Text(label, tableName: tableName, bundle: bundle) } prompt: { diff --git a/Sources/Orbit/Components/Select.swift b/Sources/Orbit/Components/Select.swift index 63d85e5cf30..43025866853 100644 --- a/Sources/Orbit/Components/Select.swift +++ b/Sources/Orbit/Components/Select.swift @@ -40,8 +40,6 @@ public struct Select = .constant(0), action: @escaping () -> Void, @ViewBuilder label: () -> Label = { EmptyView() }, @ViewBuilder value: () -> Value = { EmptyView() }, @@ -145,7 +142,6 @@ public struct Select = .constant(0), action: @escaping () -> Void ) { self.init( state: state, labelStyle: labelStyle, - message: message, - messageHeight: messageHeight + message: message ) { action() } label: { @@ -204,7 +198,6 @@ public extension Select where Prefix == Icon, Suffix == Icon, Label == Text, Val state: InputState = .default, labelStyle: InputLabelStyle = .default, message: Message? = nil, - messageHeight: Binding = .constant(0), tableName: String? = nil, bundle: Bundle? = nil, labelComment: StaticString? = nil, @@ -213,8 +206,7 @@ public extension Select where Prefix == Icon, Suffix == Icon, Label == Text, Val self.init( state: state, labelStyle: labelStyle, - message: message, - messageHeight: messageHeight + message: message ) { action() } label: { diff --git a/Sources/Orbit/Components/Textarea.swift b/Sources/Orbit/Components/Textarea.swift index febedfd7bf9..79b21074219 100644 --- a/Sources/Orbit/Components/Textarea.swift +++ b/Sources/Orbit/Components/Textarea.swift @@ -35,7 +35,6 @@ public struct Textarea: View, TextFieldBuildable { private let state: InputState private let message: Message? - @Binding private var messageHeight: CGFloat @ViewBuilder private let label: Label @ViewBuilder private let prompt: Prompt @@ -48,7 +47,7 @@ public struct Textarea: View, TextFieldBuildable { var shouldDeleteBackwardAction: (String) -> Bool = { _ in true } public var body: some View { - FieldWrapper(message: message, messageHeight: $messageHeight) { + FieldWrapper(message: message) { InputContent(state: state, message: message, isFocused: isFocused) { textView .alignmentGuide(.firstTextBaseline) { dimension in @@ -102,19 +101,16 @@ public struct Textarea: View, TextFieldBuildable { /// /// - Parameters: /// - message: Optional message below the text field. - /// - messageHeight: Binding to the current height of the optional message. public init( value: Binding, state: InputState = .default, message: Message? = nil, - messageHeight: Binding = .constant(0), @ViewBuilder label: () -> Label, @ViewBuilder prompt: () -> Prompt = { EmptyView() } ) { self._value = value self.state = state self.message = message - self._messageHeight = messageHeight self.label = label() self.prompt = prompt() } @@ -127,21 +123,18 @@ public extension Textarea where Label == Text, Prompt == Text { /// /// - Parameters: /// - message: Optional message below the text field. - /// - messageHeight: Binding to the current height of the optional message. @_disfavoredOverload init( _ label: some StringProtocol = String(""), value: Binding, prompt: some StringProtocol = String(""), state: InputState = .default, - message: Message? = nil, - messageHeight: Binding = .constant(0) + message: Message? = nil ) { self.init( value: value, state: state, - message: message, - messageHeight: messageHeight + message: message ) { Text(label) } prompt: { @@ -153,7 +146,6 @@ public extension Textarea where Label == Text, Prompt == Text { /// /// - Parameters: /// - message: Optional message below the text field. - /// - messageHeight: Binding to the current height of the optional message. @_semantics("swiftui.init_with_localization") init( _ label: LocalizedStringKey = "", @@ -161,7 +153,6 @@ public extension Textarea where Label == Text, Prompt == Text { prompt: LocalizedStringKey = "", state: InputState = .default, message: Message? = nil, - messageHeight: Binding = .constant(0), tableName: String? = nil, bundle: Bundle? = nil, labelComment: StaticString? = nil @@ -169,8 +160,7 @@ public extension Textarea where Label == Text, Prompt == Text { self.init( value: value, state: state, - message: message, - messageHeight: messageHeight + message: message ) { Text(label, tableName: tableName, bundle: bundle) } prompt: { diff --git a/Sources/Orbit/Support/Forms/FieldWrapper.swift b/Sources/Orbit/Support/Forms/FieldWrapper.swift index a7c311e466f..b433d4946d9 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) { @@ -21,13 +19,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) } } } @@ -37,13 +33,11 @@ public struct FieldWrapper: View { /// ``FieldLabel`` is a default component for constructing custom label. public 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() @@ -57,13 +51,11 @@ public extension FieldWrapper where Label == Text { 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: { Text(label) @@ -98,32 +90,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") }