Skip to content

Commit 0a1554b

Browse files
add bordered style for input field
1 parent 4317b07 commit 0a1554b

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/InputFieldPreview.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ struct InputFieldPreview: View {
5252
Toggle("Required", isOn: self.$model.isRequired)
5353
Toggle("Secure Input", isOn: self.$model.isSecureInput)
5454
SizePicker(selection: self.$model.size)
55+
Picker("Style", selection: self.$model.style) {
56+
Text("Light").tag(InputFieldVM.Style.light)
57+
Text("Bordered").tag(InputFieldVM.Style.bordered)
58+
}
5559
SubmitTypePicker(selection: self.$model.submitType)
5660
UniversalColorPicker(
5761
title: "Tint Color",
@@ -65,6 +69,10 @@ struct InputFieldPreview: View {
6569
self.model.title = newValue ? "Title" : nil
6670
}
6771
))
72+
Picker("Title Position", selection: self.$model.titlePosition) {
73+
Text("Inside").tag(InputFieldVM.TitlePosition.inside)
74+
Text("Outside").tag(InputFieldVM.TitlePosition.outside)
75+
}
6876
}
6977
}
7078
.toolbar {
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 Style: Hashable {
5+
case light
6+
case bordered
7+
}
8+
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public struct InputFieldVM: ComponentVM {
5454
/// Defaults to `.medium`.
5555
public var size: ComponentSize = .medium
5656

57+
public var style: Style = .light
58+
5759
/// The type of the submit button on the keyboard.
5860
///
5961
/// Defaults to `.return`.
@@ -118,7 +120,12 @@ extension InputFieldVM {
118120
}
119121
}
120122
var backgroundColor: UniversalColor {
121-
return self.color?.background ?? .content1
123+
switch self.style {
124+
case .light:
125+
return self.color?.background ?? .content1
126+
case .bordered:
127+
return .background
128+
}
122129
}
123130
var foregroundColor: UniversalColor {
124131
let color = self.color?.main ?? .foreground
@@ -131,6 +138,24 @@ extension InputFieldVM {
131138
return .secondaryForeground.enabled(self.isEnabled)
132139
}
133140
}
141+
var borderWidth: CGFloat {
142+
switch self.style {
143+
case .light:
144+
return 0
145+
case .bordered:
146+
switch self.size {
147+
case .small:
148+
return BorderWidth.small.value
149+
case .medium:
150+
return BorderWidth.medium.value
151+
case .large:
152+
return BorderWidth.large.value
153+
}
154+
}
155+
}
156+
var borderColor: UniversalColor {
157+
return (self.color?.main ?? .content3).enabled(self.isEnabled)
158+
}
134159
}
135160

136161
// MARK: - UIKit Helpers

Sources/ComponentsKit/Components/InputField/SUInputField.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,16 @@ public struct SUInputField<FocusValue: Hashable>: View {
9292
}
9393
.clipShape(
9494
RoundedRectangle(
95-
cornerRadius: self.model.cornerRadius.value()
95+
cornerRadius: self.model.cornerRadius.value(),
96+
)
97+
)
98+
.overlay(
99+
RoundedRectangle(
100+
cornerRadius: self.model.cornerRadius.value(),
101+
)
102+
.stroke(
103+
self.model.borderColor.color,
104+
lineWidth: self.model.borderWidth
96105
)
97106
)
98107
}

Sources/ComponentsKit/Components/InputField/UKInputField.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ open class UKInputField: UIView, UKComponent {
8383

8484
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleTap)))
8585
self.textField.addTarget(self, action: #selector(self.handleTextChange), for: .editingChanged)
86+
87+
if #available(iOS 17.0, *) {
88+
self.registerForTraitChanges([UITraitUserInterfaceStyle.self]) { (view: Self, _: UITraitCollection) in
89+
view.handleTraitChanges()
90+
}
91+
}
8692
}
8793

8894
@objc private func handleTap() {
@@ -168,8 +174,19 @@ open class UKInputField: UIView, UKComponent {
168174
height: min(size.height, self.model.height)
169175
)
170176
}
171-
177+
178+
open override func traitCollectionDidChange(
179+
_ previousTraitCollection: UITraitCollection?
180+
) {
181+
super.traitCollectionDidChange(previousTraitCollection)
182+
self.handleTraitChanges()
183+
}
184+
172185
// MARK: Helpers
186+
187+
@objc private func handleTraitChanges() {
188+
Self.Style.mainView(self, model: self.model)
189+
}
173190

174191
private func updateCornerRadius() {
175192
self.layer.cornerRadius = self.model.cornerRadius.value(for: self.bounds.height)
@@ -186,6 +203,8 @@ extension UKInputField {
186203
) {
187204
view.backgroundColor = model.backgroundColor.uiColor
188205
view.layer.cornerRadius = model.cornerRadius.value(for: view.bounds.height)
206+
view.layer.borderWidth = model.borderWidth
207+
view.layer.borderColor = model.borderColor.cgColor
189208
}
190209
static func titleLabel(
191210
_ label: UILabel,

0 commit comments

Comments
 (0)