Skip to content

Commit c55bc44

Browse files
Merge pull request #78 from componentskit/fix-text-input-layout
Fix text input layout
2 parents 287d861 + e539c85 commit c55bc44

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

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

+11-21
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,6 @@ public struct TextInputVM: ComponentVM {
6969
// MARK: - Shared Helpers
7070

7171
extension TextInputVM {
72-
var adaptedCornerRadius: ComponentRadius {
73-
switch self.cornerRadius {
74-
case .none:
75-
return .none
76-
case .small:
77-
return .small
78-
case .medium:
79-
return .medium
80-
case .large:
81-
return .large
82-
case .full:
83-
return .custom(self.height(forRows: 1) / 2)
84-
case .custom(let value):
85-
return .custom(value)
86-
}
87-
}
88-
8972
var preferredFont: UniversalFont {
9073
if let font {
9174
return font
@@ -140,6 +123,17 @@ extension TextInputVM {
140123
}
141124
}
142125

126+
func adaptedCornerRadius(for height: CGFloat = 10_000) -> CGFloat {
127+
switch self.cornerRadius {
128+
case .none, .small, .medium, .large, .full:
129+
let value = self.cornerRadius.value(for: height)
130+
let maxValue = ComponentRadius.custom(self.height(forRows: 1) / 2).value(for: height)
131+
return min(value, maxValue)
132+
case .custom(let value):
133+
return ComponentRadius.custom(value).value(for: height)
134+
}
135+
}
136+
143137
private func height(forRows rows: Int) -> CGFloat {
144138
if rows < 1 {
145139
assertionFailure("Number of rows in TextInput must be greater than or equal to 1")
@@ -162,8 +156,4 @@ extension TextInputVM {
162156
var autocorrectionType: UITextAutocorrectionType {
163157
return self.isAutocorrectionEnabled ? .yes : .no
164158
}
165-
166-
func shouldUpdateCornerRadius(_ oldModel: Self) -> Bool {
167-
return self.adaptedCornerRadius != oldModel.adaptedCornerRadius
168-
}
169159
}

Sources/ComponentsKit/Components/TextInput/SUTextInput.swift

+15-10
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,14 @@ public struct SUTextInput<FocusValue: Hashable>: View {
5555
TextEditor(text: self.$text)
5656
.contentMargins(self.model.contentPadding)
5757
.transparentScrollBackground()
58-
.frame(
59-
minHeight: self.model.minTextInputHeight,
60-
maxHeight: max(
61-
self.model.minTextInputHeight,
62-
min(
63-
self.model.maxTextInputHeight,
64-
self.textEditorPreferredHeight
65-
)
58+
.frame(minHeight: self.model.minTextInputHeight)
59+
.frame(height: max(
60+
self.model.minTextInputHeight,
61+
min(
62+
self.model.maxTextInputHeight,
63+
self.textEditorPreferredHeight
6664
)
67-
)
65+
))
6866
.lineSpacing(0)
6967
.font(self.model.preferredFont.font)
7068
.foregroundStyle(self.model.foregroundColor.color)
@@ -112,11 +110,18 @@ public struct SUTextInput<FocusValue: Hashable>: View {
112110
)
113111
}
114112
}
113+
.onChange(of: geometry.size.width) { newValue in
114+
self.textEditorPreferredHeight = TextInputHeightCalculator.preferredHeight(
115+
for: self.text,
116+
model: self.model,
117+
width: newValue
118+
)
119+
}
115120
}
116121
)
117122
.clipShape(
118123
RoundedRectangle(
119-
cornerRadius: self.model.adaptedCornerRadius.value()
124+
cornerRadius: self.model.adaptedCornerRadius()
120125
)
121126
)
122127
}

Sources/ComponentsKit/Components/TextInput/UKTextInput.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ open class UKTextInput: UIView, UKComponent {
118118

119119
self.style()
120120

121-
if self.model.shouldUpdateCornerRadius(oldModel) {
122-
self.updateCornerRadius()
123-
}
124121
if self.model.shouldUpdateLayout(oldModel) {
125122
self.invalidateIntrinsicContentSize()
126123
self.setNeedsLayout()
@@ -171,7 +168,7 @@ open class UKTextInput: UIView, UKComponent {
171168
}
172169

173170
private func updateCornerRadius() {
174-
self.layer.cornerRadius = self.model.adaptedCornerRadius.value(for: self.bounds.height)
171+
self.layer.cornerRadius = self.model.adaptedCornerRadius(for: self.bounds.height)
175172
}
176173
}
177174

@@ -189,7 +186,7 @@ extension UKTextInput {
189186
fileprivate enum Style {
190187
static func mainView(_ view: UIView, model: TextInputVM) {
191188
view.backgroundColor = model.backgroundColor.uiColor
192-
view.layer.cornerRadius = model.adaptedCornerRadius.value(for: view.bounds.height)
189+
view.layer.cornerRadius = model.adaptedCornerRadius(for: view.bounds.height)
193190
}
194191

195192
static func textView(

0 commit comments

Comments
 (0)