Skip to content
Merged
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
52 changes: 52 additions & 0 deletions Sources/SwiftCrossUI/Views/TextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,58 @@ public struct TextField: ElementaryView, View {
self._text = value ?? Binding(get: { dummy }, set: { dummy = $0 })
}

/// Creates an editable text field bound to a binary integer value.
///
/// The field's content is kept in sync with `value` via simple string
/// conversion. When the user enters text that cannot be parsed as the
/// target integer type, the binding is not updated (the previous value
/// is preserved), mirroring the behaviour of SwiftUI's
/// `TextField(_:value:formatter:)` when the formatter fails.
///
/// - Parameters:
/// - placeholder: The label to show when the field is empty.
/// - value: A binding to the integer value to edit.
public init<V: BinaryInteger & LosslessStringConvertible>(
_ placeholder: String = "",
value: Binding<V>
) {
self.placeholder = placeholder
self._text = Binding(
get: { String(value.wrappedValue) },
set: { newString in
if let parsed = V(newString), parsed != value.wrappedValue {
value.wrappedValue = parsed
}
}
)
}

/// Creates an editable text field bound to a binary floating-point value.
///
/// The field's content is kept in sync with `value` via simple string
/// conversion. When the user enters text that cannot be parsed as the
/// target floating-point type, the binding is not updated (the previous
/// value is preserved), mirroring the behaviour of SwiftUI's
/// `TextField(_:value:formatter:)` when the formatter fails.
///
/// - Parameters:
/// - placeholder: The label to show when the field is empty.
/// - value: A binding to the floating-point value to edit.
public init<V: BinaryFloatingPoint & LosslessStringConvertible>(
_ placeholder: String = "",
value: Binding<V>
) {
self.placeholder = placeholder
self._text = Binding(
get: { String(value.wrappedValue) },
set: { newString in
if let parsed = V(newString), parsed != value.wrappedValue {
value.wrappedValue = parsed
}
}
)
}

func asWidget<Backend: AppBackend>(backend: Backend) -> Backend.Widget {
return backend.createTextField()
}
Expand Down
Loading