Skip to content

Commit f435fc6

Browse files
change appearance of SUInputField
1 parent 954662c commit f435fc6

File tree

3 files changed

+31
-38
lines changed

3 files changed

+31
-38
lines changed

Sources/SwiftComponents/InputField/Models/InputFieldVM.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public struct InputFieldVM: ComponentVM {
1313

1414
/// The corner radius of the input field.
1515
///
16-
/// /// Defaults to `.medium`.
16+
/// Defaults to `.medium`.
1717
public var cornerRadius: ComponentRadius = .medium
1818

1919
/// The font used for the input field's text.
@@ -65,7 +65,7 @@ public struct InputFieldVM: ComponentVM {
6565
public var tintColor: UniversalColor = .accent
6666

6767
/// The title displayed on the input field.
68-
public var title: String = ""
68+
public var title: String?
6969

7070
/// Initializes a new instance of `InputFieldVM` with default values.
7171
public init() {}
@@ -96,6 +96,9 @@ extension InputFieldVM {
9696
return 16
9797
}
9898
}
99+
var spacing: CGFloat {
100+
return 12
101+
}
99102
var backgroundColor: UniversalColor {
100103
if let color {
101104
return color.main.withOpacity(0.25)
@@ -166,7 +169,11 @@ extension InputFieldVM {
166169
}
167170
}
168171
var height: CGFloat {
169-
return self.inputFieldHeight + self.inputFieldTopPadding + self.verticalPadding
172+
return switch self.size {
173+
case .small: 40
174+
case .medium: 60
175+
case .large: 80
176+
}
170177
}
171178
}
172179

@@ -183,9 +190,13 @@ extension InputFieldVM {
183190
])
184191
}
185192
func nsAttributedTitle(for position: InputFieldTitlePosition) -> NSAttributedString {
193+
guard let title else {
194+
return NSAttributedString()
195+
}
196+
186197
let attributedString = NSMutableAttributedString()
187198
attributedString.append(NSAttributedString(
188-
string: self.title,
199+
string: title,
189200
attributes: [
190201
.font: self.titleFont(for: position).uiFont,
191202
.foregroundColor: self.titleColor(for: position).uiColor
@@ -223,9 +234,13 @@ extension InputFieldVM {
223234
func attributedTitle(
224235
for position: InputFieldTitlePosition
225236
) -> AttributedString {
237+
guard let title else {
238+
return AttributedString()
239+
}
240+
226241
var attributedString = AttributedString()
227242

228-
var attributedTitle = AttributedString(self.title)
243+
var attributedTitle = AttributedString(title)
229244
attributedTitle.font = self.titleFont(for: position).font
230245
attributedTitle.foregroundColor = self.titleColor(for: position).uiColor
231246
attributedString.append(attributedTitle)

Sources/SwiftComponents/InputField/SUInputField.swift

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ public struct SUInputField<FocusValue: Hashable>: View {
2727

2828
@Environment(\.colorScheme) private var colorScheme
2929

30-
private var titlePosition: InputFieldTitlePosition {
31-
if self.model.placeholder.isNilOrEmpty,
32-
self.text.isEmpty,
33-
self.globalFocus != self.localFocus {
34-
return .center
35-
} else {
36-
return .top
37-
}
38-
}
39-
4030
// MARK: Initialization
4131

4232
/// Initializer.
@@ -60,19 +50,14 @@ public struct SUInputField<FocusValue: Hashable>: View {
6050
// MARK: Body
6151

6252
public var body: some View {
63-
ZStack(alignment: Alignment(
64-
horizontal: .leading,
65-
vertical: self.titlePosition == .top ? .top : .center
66-
)) {
67-
Text(self.model.attributedTitle(for: self.titlePosition))
68-
.font(self.model.titleFont(for: self.titlePosition).font)
69-
.foregroundStyle(
70-
self.model
71-
.titleColor(for: self.titlePosition)
72-
.color(for: self.colorScheme)
73-
)
74-
.padding(.top, self.titlePosition == .top ? self.model.verticalPadding : 0)
75-
.animation(.linear(duration: 0.1), value: self.titlePosition)
53+
HStack(spacing: self.model.spacing) {
54+
if self.model.title.isNotNilAndEmpty {
55+
Text(self.model.attributedTitle(for: .center))
56+
.font(self.model.preferredFont.font)
57+
.foregroundStyle(
58+
self.model.foregroundColor.color(for: self.colorScheme)
59+
)
60+
}
7661

7762
Group {
7863
if self.model.isSecureInput {
@@ -96,11 +81,9 @@ public struct SUInputField<FocusValue: Hashable>: View {
9681
.submitLabel(self.model.submitType.submitLabel)
9782
.autocorrectionDisabled(!self.model.isAutocorrectionEnabled)
9883
.textInputAutocapitalization(self.model.autocapitalization.textInputAutocapitalization)
99-
.frame(height: self.model.inputFieldHeight)
100-
.padding(.bottom, self.model.verticalPadding)
101-
.padding(.top, self.model.inputFieldTopPadding)
10284
}
10385
.padding(.horizontal, self.model.horizontalPadding)
86+
.frame(height: self.model.height)
10487
.background(self.model.backgroundColor.color(for: self.colorScheme))
10588
.onTapGesture {
10689
self.globalFocus = self.localFocus
@@ -110,11 +93,6 @@ public struct SUInputField<FocusValue: Hashable>: View {
11093
cornerRadius: self.model.cornerRadius.value()
11194
)
11295
)
113-
.onChange(of: self.globalFocus) { _ in
114-
// NOTE: Workaround to force `globalFocus` value update properly
115-
// Without this workaround the title position changes to `center`
116-
// when the text is cleared
117-
}
11896
}
11997
}
12098

Sources/SwiftComponents/Shared/Initializable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/// A type that can be initialized with an empty initializer or with a transformation closure that modifies default parameters.
22
public protocol Initializable {
3-
3+
44
/// Initializes a new instance with default values.
55
init()
6-
6+
77
/// Initializes a new instance by applying a transformation closure to the default values.
88
///
99
/// - Parameter transform: A closure that defines the transformation.

0 commit comments

Comments
 (0)