Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions Sources/ComponentsKit/Components/TextInput/Models/TextInputVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,6 @@ public struct TextInputVM: ComponentVM {
// MARK: - Shared Helpers

extension TextInputVM {
var adaptedCornerRadius: ComponentRadius {
switch self.cornerRadius {
case .none:
return .none
case .small:
return .small
case .medium:
return .medium
case .large:
return .large
case .full:
return .custom(self.height(forRows: 1) / 2)
case .custom(let value):
return .custom(value)
}
}

var preferredFont: UniversalFont {
if let font {
return font
Expand Down Expand Up @@ -140,6 +123,17 @@ extension TextInputVM {
}
}

func adaptedCornerRadius(for height: CGFloat = 10_000) -> CGFloat {
switch self.cornerRadius {
case .none, .small, .medium, .large, .full:
let value = self.cornerRadius.value(for: height)
let maxValue = ComponentRadius.custom(self.height(forRows: 1) / 2).value(for: height)
return min(value, maxValue)
case .custom(let value):
return ComponentRadius.custom(value).value(for: height)
}
}

private func height(forRows rows: Int) -> CGFloat {
if rows < 1 {
assertionFailure("Number of rows in TextInput must be greater than or equal to 1")
Expand All @@ -162,8 +156,4 @@ extension TextInputVM {
var autocorrectionType: UITextAutocorrectionType {
return self.isAutocorrectionEnabled ? .yes : .no
}

func shouldUpdateCornerRadius(_ oldModel: Self) -> Bool {
return self.adaptedCornerRadius != oldModel.adaptedCornerRadius
}
}
25 changes: 15 additions & 10 deletions Sources/ComponentsKit/Components/TextInput/SUTextInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ public struct SUTextInput<FocusValue: Hashable>: View {
TextEditor(text: self.$text)
.contentMargins(self.model.contentPadding)
.transparentScrollBackground()
.frame(
minHeight: self.model.minTextInputHeight,
maxHeight: max(
self.model.minTextInputHeight,
min(
self.model.maxTextInputHeight,
self.textEditorPreferredHeight
)
.frame(minHeight: self.model.minTextInputHeight)
.frame(height: max(
self.model.minTextInputHeight,
min(
self.model.maxTextInputHeight,
self.textEditorPreferredHeight
)
)
))
.lineSpacing(0)
.font(self.model.preferredFont.font)
.foregroundStyle(self.model.foregroundColor.color)
Expand Down Expand Up @@ -112,11 +110,18 @@ public struct SUTextInput<FocusValue: Hashable>: View {
)
}
}
.onChange(of: geometry.size.width) { newValue in
self.textEditorPreferredHeight = TextInputHeightCalculator.preferredHeight(
for: self.text,
model: self.model,
width: newValue
)
}
}
)
.clipShape(
RoundedRectangle(
cornerRadius: self.model.adaptedCornerRadius.value()
cornerRadius: self.model.adaptedCornerRadius()
)
)
}
Expand Down
7 changes: 2 additions & 5 deletions Sources/ComponentsKit/Components/TextInput/UKTextInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ open class UKTextInput: UIView, UKComponent {

self.style()

if self.model.shouldUpdateCornerRadius(oldModel) {
self.updateCornerRadius()
}
if self.model.shouldUpdateLayout(oldModel) {
self.invalidateIntrinsicContentSize()
self.setNeedsLayout()
Expand Down Expand Up @@ -171,7 +168,7 @@ open class UKTextInput: UIView, UKComponent {
}

private func updateCornerRadius() {
self.layer.cornerRadius = self.model.adaptedCornerRadius.value(for: self.bounds.height)
self.layer.cornerRadius = self.model.adaptedCornerRadius(for: self.bounds.height)
}
}

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

static func textView(
Expand Down