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 Jul 19, 2024
1 parent 40ef301 commit c3d9b38
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 61 deletions.
18 changes: 4 additions & 14 deletions Sources/Orbit/Components/InputField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
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 @@ -83,7 +82,7 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
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: {
Expand Down Expand Up @@ -202,15 +201,13 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
///
/// - Parameters:
/// - message: Optional message below the text field.
/// - messageHeight: Binding to the current height of the optional message.
public init(
value: Binding<String>,
state: InputState = .default,
labelStyle: InputLabelStyle = .default,
isSecure: Bool = false,
passwordStrength: PasswordStrengthIndicator.PasswordStrength? = nil,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0),
@ViewBuilder label: () -> Label,
@ViewBuilder prompt: () -> Prompt = { EmptyView() },
@ViewBuilder prefix: () -> Prefix = { EmptyView() },
Expand All @@ -222,7 +219,6 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
self.isSecure = isSecure
self.passwordStrength = passwordStrength
self.message = message
self._messageHeight = messageHeight
self.label = label()
self.prompt = prompt()
self.prefix = prefix()
Expand All @@ -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(""),
Expand All @@ -249,17 +244,15 @@ 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<CGFloat> = .constant(0)
message: Message? = nil
) {
self.init(
value: value,
state: state,
labelStyle: labelStyle,
isSecure: isSecure,
passwordStrength: passwordStrength,
message: message,
messageHeight: messageHeight
message: message
) {
Text(label)
} prompt: {
Expand All @@ -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 = "",
Expand All @@ -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<CGFloat> = .constant(0),
tableName: String? = nil,
bundle: Bundle? = nil,
labelComment: StaticString? = nil
Expand All @@ -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: {
Expand Down
14 changes: 3 additions & 11 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<Label: View, Value: View, Prompt: View, Prefix: View, Suffi

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

@Binding private var messageHeight: CGFloat

private let state: InputState
private let labelStyle: InputLabelStyle
Expand All @@ -54,7 +52,7 @@ public struct Select<Label: View, Value: View, Prompt: View, Prefix: View, Suffi
@ViewBuilder private let suffix: Suffix

public var body: some View {
FieldWrapper(message: message, messageHeight: $messageHeight) {
FieldWrapper(message: message) {
SwiftUI.Button {
if isHapticsEnabled {
HapticsProvider.sendHapticFeedback(.light(0.5))
Expand Down Expand Up @@ -134,7 +132,6 @@ public struct Select<Label: View, Value: View, Prompt: View, Prefix: View, Suffi
state: InputState = .default,
labelStyle: InputLabelStyle = .default,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0),
action: @escaping () -> Void,
@ViewBuilder label: () -> Label = { EmptyView() },
@ViewBuilder value: () -> Value = { EmptyView() },
Expand All @@ -145,7 +142,6 @@ public struct Select<Label: View, Value: View, Prompt: View, Prefix: View, Suffi
self.state = state
self.labelStyle = labelStyle
self.message = message
self._messageHeight = messageHeight
self.action = action
self.label = label()
self.value = value()
Expand All @@ -169,14 +165,12 @@ public extension Select where Prefix == Icon, Suffix == Icon, Label == Text, Val
state: InputState = .default,
labelStyle: InputLabelStyle = .default,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0),
action: @escaping () -> Void
) {
self.init(
state: state,
labelStyle: labelStyle,
message: message,
messageHeight: messageHeight
message: message
) {
action()
} label: {
Expand Down Expand Up @@ -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<CGFloat> = .constant(0),
tableName: String? = nil,
bundle: Bundle? = nil,
labelComment: StaticString? = nil,
Expand All @@ -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: {
Expand Down
18 changes: 4 additions & 14 deletions Sources/Orbit/Components/Textarea.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public struct Textarea<Label: View, Prompt: View>: 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

Expand All @@ -48,7 +47,7 @@ public struct Textarea<Label: View, Prompt: View>: 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
Expand Down Expand Up @@ -102,19 +101,16 @@ public struct Textarea<Label: View, Prompt: View>: View, TextFieldBuildable {
///
/// - Parameters:
/// - message: Optional message below the text field.
/// - messageHeight: Binding to the current height of the optional message.
public init(
value: Binding<String>,
state: InputState = .default,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .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()
}
Expand All @@ -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<String>,
prompt: some StringProtocol = String(""),
state: InputState = .default,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0)
message: Message? = nil
) {
self.init(
value: value,
state: state,
message: message,
messageHeight: messageHeight
message: message
) {
Text(label)
} prompt: {
Expand All @@ -153,24 +146,21 @@ 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 = "",
value: Binding<String>,
prompt: LocalizedStringKey = "",
state: InputState = .default,
message: Message? = nil,
messageHeight: Binding<CGFloat> = .constant(0),
tableName: String? = nil,
bundle: Bundle? = nil,
labelComment: StaticString? = nil
) {
self.init(
value: value,
state: state,
message: message,
messageHeight: messageHeight
message: message
) {
Text(label, tableName: tableName, bundle: bundle)
} prompt: {
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 @@ -21,13 +19,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 @@ -37,13 +33,11 @@ public struct FieldWrapper<Label: View, Content: View, Footer: View>: View {
/// ``FieldLabel`` is a default component for constructing custom label.
public 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 @@ -57,13 +51,11 @@ public extension FieldWrapper where Label == Text {
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: {
Text(label)
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit c3d9b38

Please sign in to comment.