Skip to content

Commit 4317b07

Browse files
add TitlePosition param to InputFieldVM and implement in SUInputField
1 parent ca0ee89 commit 4317b07

File tree

3 files changed

+61
-35
lines changed

3 files changed

+61
-35
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Foundation
2+
3+
extension InputFieldVM {
4+
public enum TitlePosition {
5+
case inside
6+
case outside
7+
}
8+
}

Sources/ComponentsKit/Components/InputField/Models/InputFieldVM.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public struct InputFieldVM: ComponentVM {
6767
/// The title displayed on the input field.
6868
public var title: String?
6969

70+
public var titlePosition: TitlePosition = .inside
71+
7072
/// Initializes a new instance of `InputFieldVM` with default values.
7173
public init() {}
7274
}
@@ -104,7 +106,16 @@ extension InputFieldVM {
104106
}
105107
}
106108
var spacing: CGFloat {
107-
return self.title.isNotNilAndEmpty ? 12 : 0
109+
guard self.title.isNotNilAndEmpty else {
110+
return 0
111+
}
112+
113+
switch self.titlePosition {
114+
case .inside:
115+
return 12
116+
case .outside:
117+
return 8
118+
}
108119
}
109120
var backgroundColor: UniversalColor {
110121
return self.color?.background ?? .content1

Sources/ComponentsKit/Components/InputField/SUInputField.swift

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,46 +49,53 @@ public struct SUInputField<FocusValue: Hashable>: View {
4949
// MARK: Body
5050

5151
public var body: some View {
52-
HStack(spacing: self.model.spacing) {
53-
if let title = self.model.attributedTitle {
52+
VStack(alignment: .leading, spacing: self.model.spacing) {
53+
if let title = self.model.attributedTitle,
54+
self.model.titlePosition == .outside {
5455
Text(title)
55-
.font(self.model.preferredFont.font)
5656
}
5757

58-
Group {
59-
if self.model.isSecureInput {
60-
SecureField(text: self.$text, label: {
61-
Text(self.model.placeholder ?? "")
62-
.foregroundStyle(self.model.placeholderColor.color)
63-
})
64-
} else {
65-
TextField(text: self.$text, label: {
66-
Text(self.model.placeholder ?? "")
67-
.foregroundStyle(self.model.placeholderColor.color)
68-
})
58+
HStack(spacing: self.model.spacing) {
59+
if let title = self.model.attributedTitle,
60+
self.model.titlePosition == .inside {
61+
Text(title)
6962
}
63+
64+
Group {
65+
if self.model.isSecureInput {
66+
SecureField(text: self.$text, label: {
67+
Text(self.model.placeholder ?? "")
68+
.foregroundStyle(self.model.placeholderColor.color)
69+
})
70+
} else {
71+
TextField(text: self.$text, label: {
72+
Text(self.model.placeholder ?? "")
73+
.foregroundStyle(self.model.placeholderColor.color)
74+
})
75+
}
76+
}
77+
.tint(self.model.tintColor.color)
78+
.font(self.model.preferredFont.font)
79+
.foregroundStyle(self.model.foregroundColor.color)
80+
.applyFocus(globalFocus: self.globalFocus, localFocus: self.localFocus)
81+
.disabled(!self.model.isEnabled)
82+
.keyboardType(self.model.keyboardType)
83+
.submitLabel(self.model.submitType.submitLabel)
84+
.autocorrectionDisabled(!self.model.isAutocorrectionEnabled)
85+
.textInputAutocapitalization(self.model.autocapitalization.textInputAutocapitalization)
7086
}
71-
.tint(self.model.tintColor.color)
72-
.font(self.model.preferredFont.font)
73-
.foregroundStyle(self.model.foregroundColor.color)
74-
.applyFocus(globalFocus: self.globalFocus, localFocus: self.localFocus)
75-
.disabled(!self.model.isEnabled)
76-
.keyboardType(self.model.keyboardType)
77-
.submitLabel(self.model.submitType.submitLabel)
78-
.autocorrectionDisabled(!self.model.isAutocorrectionEnabled)
79-
.textInputAutocapitalization(self.model.autocapitalization.textInputAutocapitalization)
80-
}
81-
.padding(.horizontal, self.model.horizontalPadding)
82-
.frame(height: self.model.height)
83-
.background(self.model.backgroundColor.color)
84-
.onTapGesture {
85-
self.globalFocus?.wrappedValue = self.localFocus
86-
}
87-
.clipShape(
88-
RoundedRectangle(
89-
cornerRadius: self.model.cornerRadius.value()
87+
.padding(.horizontal, self.model.horizontalPadding)
88+
.frame(height: self.model.height)
89+
.background(self.model.backgroundColor.color)
90+
.onTapGesture {
91+
self.globalFocus?.wrappedValue = self.localFocus
92+
}
93+
.clipShape(
94+
RoundedRectangle(
95+
cornerRadius: self.model.cornerRadius.value()
96+
)
9097
)
91-
)
98+
}
9299
}
93100
}
94101

0 commit comments

Comments
 (0)