Skip to content

Commit e42e06e

Browse files
improve method for calculating adapted corner radius for the text input
1 parent 287d861 commit e42e06e

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

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

Lines changed: 11 additions & 21 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public struct SUTextInput<FocusValue: Hashable>: View {
116116
)
117117
.clipShape(
118118
RoundedRectangle(
119-
cornerRadius: self.model.adaptedCornerRadius.value()
119+
cornerRadius: self.model.adaptedCornerRadius()
120120
)
121121
)
122122
}

Sources/ComponentsKit/Components/TextInput/UKTextInput.swift

Lines changed: 2 additions & 5 deletions
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)