Skip to content

Commit aed772d

Browse files
Merge pull request #90 from componentskit/improve-text-input
Text Input improvements
2 parents e84ffd6 + 4653a34 commit aed772d

File tree

9 files changed

+106
-32
lines changed

9 files changed

+106
-32
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(InputFieldVM.Style.light)
66-
Text("Bordered").tag(InputFieldVM.Style.bordered)
67-
Text("Faded").tag(InputFieldVM.Style.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/InputField/Models/InputFieldStyle.swift

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public struct InputFieldVM: ComponentVM {
6565
/// The visual style of the input field.
6666
///
6767
/// Defaults to `.light`.
68-
public var style: Style = .light
68+
public var style: InputStyle = .light
6969

7070
/// The type of the submit button on the keyboard.
7171
///

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

Lines changed: 32 additions & 2 deletions
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 {
@@ -117,7 +147,7 @@ extension TextInputVM {
117147

118148
var maxTextInputHeight: CGFloat {
119149
if let maxRows {
120-
return self.height(forRows: maxRows)
150+
return self.height(forRows: max(maxRows, self.minRows))
121151
} else {
122152
return 10_000
123153
}

Sources/ComponentsKit/Components/TextInput/SUTextInput.swift

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,23 @@ public struct SUTextInput<FocusValue: Hashable>: View {
5555
TextEditor(text: self.$text)
5656
.contentMargins(self.model.contentPadding)
5757
.transparentScrollBackground()
58-
.frame(minHeight: self.model.minTextInputHeight)
59-
.frame(height: max(
60-
self.model.minTextInputHeight,
61-
min(
62-
self.model.maxTextInputHeight,
63-
self.textEditorPreferredHeight
58+
.frame(
59+
minHeight: self.model.minTextInputHeight,
60+
idealHeight: max(
61+
self.model.minTextInputHeight,
62+
min(
63+
self.model.maxTextInputHeight,
64+
self.textEditorPreferredHeight
65+
)
66+
),
67+
maxHeight: max(
68+
self.model.minTextInputHeight,
69+
min(
70+
self.model.maxTextInputHeight,
71+
self.textEditorPreferredHeight
72+
)
6473
)
65-
))
74+
)
6675
.lineSpacing(0)
6776
.font(self.model.preferredFont.font)
6877
.foregroundStyle(self.model.foregroundColor.color)
@@ -124,6 +133,15 @@ public struct SUTextInput<FocusValue: Hashable>: View {
124133
cornerRadius: self.model.adaptedCornerRadius()
125134
)
126135
)
136+
.overlay(
137+
RoundedRectangle(
138+
cornerRadius: self.model.cornerRadius.value()
139+
)
140+
.stroke(
141+
self.model.borderColor.color,
142+
lineWidth: self.model.borderWidth
143+
)
144+
)
127145
}
128146
}
129147

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(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
3+
/// The appearance style of inputs.
4+
public enum InputStyle: Hashable {
5+
/// An input with a partially transparent background.
6+
case light
7+
/// An input with a transparent background and a border.
8+
case bordered
9+
/// An input with a partially transparent background and a border.
10+
case faded
11+
}

0 commit comments

Comments
 (0)