Skip to content

Commit 3d82608

Browse files
add styles to text input
1 parent 8dbba1e commit 3d82608

File tree

6 files changed

+77
-10
lines changed

6 files changed

+77
-10
lines changed

Examples/DemosApp/DemosApp/ComponentsPreview/Helpers/PreviewPickers.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct AutocapitalizationPicker: View {
2323
@Binding var selection: TextAutocapitalization
2424

2525
var body: some View {
26-
Picker("Autocapitalization", selection: $selection) {
26+
Picker("Autocapitalization", selection: self.$selection) {
2727
Text("Never").tag(TextAutocapitalization.never)
2828
Text("Characters").tag(TextAutocapitalization.characters)
2929
Text("Words").tag(TextAutocapitalization.words)
@@ -203,13 +203,25 @@ struct CaptionFontPicker: View {
203203
}
204204
}
205205

206+
struct InputStylePicker: View {
207+
@Binding var selection: InputStyle
208+
209+
var body: some View {
210+
Picker("Style", selection: self.$selection) {
211+
Text("Light").tag(InputStyle.light)
212+
Text("Bordered").tag(InputStyle.bordered)
213+
Text("Faded").tag(InputStyle.faded)
214+
}
215+
}
216+
}
217+
206218
// MARK: - KeyboardTypePicker
207219

208220
struct KeyboardTypePicker: View {
209221
@Binding var selection: UIKeyboardType
210222

211223
var body: some View {
212-
Picker("Keyboard Type", selection: $selection) {
224+
Picker("Keyboard Type", selection: self.$selection) {
213225
Text("Default").tag(UIKeyboardType.default)
214226
Text("asciiCapable").tag(UIKeyboardType.asciiCapable)
215227
Text("numbersAndPunctuation").tag(UIKeyboardType.numbersAndPunctuation)
@@ -260,7 +272,7 @@ struct SubmitTypePicker: View {
260272
@Binding var selection: SubmitType
261273

262274
var body: some View {
263-
Picker("Submit Type", selection: $selection) {
275+
Picker("Submit Type", selection: self.$selection) {
264276
Text("done").tag(SubmitType.done)
265277
Text("go").tag(SubmitType.go)
266278
Text("join").tag(SubmitType.join)

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ struct InputFieldPreview: View {
6161
Toggle("Required", isOn: self.$model.isRequired)
6262
Toggle("Secure Input", isOn: self.$model.isSecureInput)
6363
SizePicker(selection: self.$model.size)
64-
Picker("Style", selection: self.$model.style) {
65-
Text("Light").tag(InputStyle.light)
66-
Text("Bordered").tag(InputStyle.bordered)
67-
Text("Faded").tag(InputStyle.faded)
68-
}
64+
InputStylePicker(selection: self.$model.style)
6965
SubmitTypePicker(selection: self.$model.submitType)
7066
UniversalColorPicker(
7167
title: "Tint Color",

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/TextInputPreview.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ struct TextInputPreviewPreview: View {
6060
}
6161
))
6262
SizePicker(selection: self.$model.size)
63+
InputStylePicker(selection: self.$model.style)
6364
SubmitTypePicker(selection: self.$model.submitType)
6465
UniversalColorPicker(
6566
title: "Tint Color",

Sources/ComponentsKit/Components/TextInput/Models/TextInputVM.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public struct TextInputVM: ComponentVM {
5252
/// Defaults to `.medium`.
5353
public var size: ComponentSize = .medium
5454

55+
/// The visual style of the text input.
56+
///
57+
/// Defaults to `.light`.
58+
public var style: InputStyle = .light
59+
5560
/// The type of the submit button on the keyboard.
5661
///
5762
/// Defaults to `.return`.
@@ -89,7 +94,12 @@ extension TextInputVM {
8994
}
9095

9196
var backgroundColor: UniversalColor {
92-
return self.color?.background ?? .content1
97+
switch self.style {
98+
case .light, .faded:
99+
return self.color?.background ?? .content1
100+
case .bordered:
101+
return .background
102+
}
93103
}
94104

95105
var foregroundColor: UniversalColor {
@@ -105,6 +115,26 @@ extension TextInputVM {
105115
}
106116
}
107117

118+
var borderWidth: CGFloat {
119+
switch self.style {
120+
case .light:
121+
return 0
122+
case .bordered, .faded:
123+
switch self.size {
124+
case .small:
125+
return BorderWidth.small.value
126+
case .medium:
127+
return BorderWidth.medium.value
128+
case .large:
129+
return BorderWidth.large.value
130+
}
131+
}
132+
}
133+
134+
var borderColor: UniversalColor {
135+
return (self.color?.main ?? .content3).enabled(self.isEnabled)
136+
}
137+
108138
var minTextInputHeight: CGFloat {
109139
let numberOfRows: Int
110140
if let maxRows {

Sources/ComponentsKit/Components/TextInput/SUTextInput.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ public struct SUTextInput<FocusValue: Hashable>: View {
124124
cornerRadius: self.model.adaptedCornerRadius()
125125
)
126126
)
127+
.overlay(
128+
RoundedRectangle(
129+
cornerRadius: self.model.cornerRadius.value()
130+
)
131+
.stroke(
132+
self.model.borderColor.color,
133+
lineWidth: self.model.borderWidth
134+
)
135+
)
127136
}
128137
}
129138

Sources/ComponentsKit/Components/TextInput/UKTextInput.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ open class UKTextInput: UIView, UKComponent {
7979
self.addSubview(self.placeholderLabel)
8080

8181
self.textView.delegate = self
82+
83+
if #available(iOS 17.0, *) {
84+
self.registerForTraitChanges([UITraitUserInterfaceStyle.self]) { (view: Self, _: UITraitCollection) in
85+
view.handleTraitChanges()
86+
}
87+
}
8288
}
8389

8490
// MARK: - Style
@@ -157,7 +163,18 @@ open class UKTextInput: UIView, UKComponent {
157163
return CGSize(width: width, height: height)
158164
}
159165

160-
// MARK: - Helpers
166+
open override func traitCollectionDidChange(
167+
_ previousTraitCollection: UITraitCollection?
168+
) {
169+
super.traitCollectionDidChange(previousTraitCollection)
170+
self.handleTraitChanges()
171+
}
172+
173+
// MARK: Helpers
174+
175+
@objc private func handleTraitChanges() {
176+
Self.Style.mainView(self, model: self.model)
177+
}
161178

162179
private func handleTextChanges() {
163180
self.onValueChange(self.text)
@@ -187,6 +204,8 @@ extension UKTextInput {
187204
static func mainView(_ view: UIView, model: TextInputVM) {
188205
view.backgroundColor = model.backgroundColor.uiColor
189206
view.layer.cornerRadius = model.adaptedCornerRadius(for: view.bounds.height)
207+
view.layer.borderColor = model.borderColor.cgColor
208+
view.layer.borderWidth = model.borderWidth
190209
}
191210

192211
static func textView(

0 commit comments

Comments
 (0)