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
20 changes: 20 additions & 0 deletions Sources/HandySwiftUI/Modifiers/ValueChangeModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import SwiftUI

extension View {
/// Calls `action` with the new value whenever `value` changes, using the `onChange` variant that is not deprecated on the running OS.
///
/// `onChange(of:perform:)` is deprecated starting with iOS 17 (and from the start on visionOS), while its replacement needs iOS 17,
/// one above the package's minimum.
@ViewBuilder
func onValueChange<Value: Equatable>(of value: Value, perform action: @escaping (Value) -> Void) -> some View {
#if os(visionOS)
self.onChange(of: value) { _, newValue in action(newValue) }
#else
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) {
self.onChange(of: value) { _, newValue in action(newValue) }
} else {
self.onChange(of: value, perform: action)
}
#endif
}
}
2 changes: 1 addition & 1 deletion Sources/HandySwiftUI/Types/Views/LimitedTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct LimitedTextField: View {
public var body: some View {
VStack(alignment: .trailing, spacing: 4) {
TextField(self.title, text: self.$text)
.onChange(of: self.text) { _ in
.onValueChange(of: self.text) { _ in
if self.text.count > self.characterLimit {
self.text = String(self.text.prefix(self.characterLimit))
}
Expand Down
Loading