Skip to content

Commit a7b57ee

Browse files
add no focus init to SUTextInput
1 parent 71adcf1 commit a7b57ee

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

Sources/ComponentsKit/Components/TextInput/SUTextInput.swift

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct SUTextInput<FocusValue: Hashable>: View {
1414
///
1515
/// When the `localFocus` value matches `globalFocus`, this text input becomes focused.
1616
/// This enables centralized focus management for multiple text inputs and input fields within a single view.
17-
@FocusState.Binding public var globalFocus: FocusValue
17+
public let globalFocus: FocusState<FocusValue>.Binding?
1818

1919
/// The unique value for this field to match against the global focus state to determine whether this text input is focused.
2020
///
@@ -24,7 +24,7 @@ public struct SUTextInput<FocusValue: Hashable>: View {
2424
///
2525
/// - Warning: The `localFocus` value must be unique to each text input and input field, to ensure that different
2626
/// text inputs and input fields within the same view can be independently focused based on the shared `globalFocus`.
27-
public var localFocus: FocusValue
27+
public let localFocus: FocusValue
2828

2929
@State private var textEditorPreferredHeight: CGFloat = 0
3030

@@ -43,7 +43,7 @@ public struct SUTextInput<FocusValue: Hashable>: View {
4343
model: TextInputVM = .init()
4444
) {
4545
self._text = text
46-
self._globalFocus = globalFocus
46+
self.globalFocus = globalFocus
4747
self.localFocus = localFocus
4848
self.model = model
4949
}
@@ -67,7 +67,7 @@ public struct SUTextInput<FocusValue: Hashable>: View {
6767
.font(self.model.preferredFont.font)
6868
.foregroundStyle(self.model.foregroundColor.color)
6969
.tint(self.model.tintColor.color)
70-
.focused(self.$globalFocus, equals: self.localFocus)
70+
.applyFocus(globalFocus: self.globalFocus, localFocus: self.localFocus)
7171
.disabled(!self.model.isEnabled)
7272
.keyboardType(self.model.keyboardType)
7373
.submitLabel(self.model.submitType.submitLabel)
@@ -154,6 +154,18 @@ extension View {
154154
UITextView.appearance().textContainer.lineFragmentPadding = 0
155155
}
156156
}
157+
158+
@ViewBuilder
159+
fileprivate func applyFocus<FocusValue: Hashable>(
160+
globalFocus: FocusState<FocusValue>.Binding?,
161+
localFocus: FocusValue,
162+
) -> some View {
163+
if let globalFocus {
164+
self.focused(globalFocus, equals: localFocus)
165+
} else {
166+
self
167+
}
168+
}
157169
}
158170

159171
// MARK: - Boolean Focus Value
@@ -170,7 +182,25 @@ extension SUTextInput where FocusValue == Bool {
170182
model: TextInputVM = .init()
171183
) {
172184
self._text = text
173-
self._globalFocus = isFocused
185+
self.globalFocus = isFocused
186+
self.localFocus = true
187+
self.model = model
188+
}
189+
}
190+
191+
// MARK: - No Focus Value
192+
193+
extension SUTextInput where FocusValue == Bool {
194+
/// Initializer.
195+
/// - Parameters:
196+
/// - text: A Binding value to control the inputted text.
197+
/// - model: A model that defines the appearance properties.
198+
public init(
199+
text: Binding<String>,
200+
model: TextInputVM = .init()
201+
) {
202+
self._text = text
203+
self.globalFocus = nil
174204
self.localFocus = true
175205
self.model = model
176206
}

0 commit comments

Comments
 (0)