diff --git a/Sources/ComponentsKit/Components/TextInput/Models/TextInputVM.swift b/Sources/ComponentsKit/Components/TextInput/Models/TextInputVM.swift index db000784..274cce4b 100644 --- a/Sources/ComponentsKit/Components/TextInput/Models/TextInputVM.swift +++ b/Sources/ComponentsKit/Components/TextInput/Models/TextInputVM.swift @@ -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 @@ -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") @@ -162,8 +156,4 @@ extension TextInputVM { var autocorrectionType: UITextAutocorrectionType { return self.isAutocorrectionEnabled ? .yes : .no } - - func shouldUpdateCornerRadius(_ oldModel: Self) -> Bool { - return self.adaptedCornerRadius != oldModel.adaptedCornerRadius - } } diff --git a/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift b/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift index 3616297c..47e68014 100644 --- a/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift +++ b/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift @@ -55,16 +55,14 @@ public struct SUTextInput: 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) @@ -112,11 +110,18 @@ public struct SUTextInput: 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() ) ) } diff --git a/Sources/ComponentsKit/Components/TextInput/UKTextInput.swift b/Sources/ComponentsKit/Components/TextInput/UKTextInput.swift index 1e539bc0..8e4b8519 100644 --- a/Sources/ComponentsKit/Components/TextInput/UKTextInput.swift +++ b/Sources/ComponentsKit/Components/TextInput/UKTextInput.swift @@ -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() @@ -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) } } @@ -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(