Skip to content

Commit

Permalink
Merge pull request #21 from kemalturk/form-field-fix
Browse files Browse the repository at this point in the history
Resolved the issue with the FormField property wrapper not responding to changes from onChange or onReceive listeners.
MalekKamel authored Nov 14, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents e646120 + 41f7e75 commit 99c0e27
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions Sources/Wrappers/FormField.swift
Original file line number Diff line number Diff line change
@@ -5,39 +5,51 @@

import Combine


@propertyWrapper
public class FormField<Value: Equatable, Validator: Validatable> where Value == Validator.Value {
@Published
private var value: Value

private var subject: CurrentValueSubject<Value, Never>
private let validator: Validator

public var projectedValue: AnyPublisher<Value, Never> {
$value.eraseToAnyPublisher()
}

public var wrappedValue: Value {
get {
value
subject.value
}
set {
value = newValue
subject.send(newValue)
}
}

public init(wrappedValue value: Value, validator: () -> Validator) {
self.value = value
self.subject = CurrentValueSubject(value)
self.validator = validator()
}

public init(wrappedValue value: Value, validator: Validator) {
self.value = value
self.subject = CurrentValueSubject(value)
self.validator = validator
}

public init(initialValue value: Value, validator: () -> Validator) {
self.value = value
self.subject = CurrentValueSubject(value)
self.validator = validator()
}

public static subscript<EnclosingSelf>(
_enclosingInstance object: EnclosingSelf,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>,
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, FormField>
) -> Value {
get { object[keyPath: storageKeyPath].wrappedValue }
set {
if let observableObject = object as? (any ObservableObject),
let objectWillChange = (observableObject.objectWillChange as any Publisher) as? ObservableObjectPublisher {
objectWillChange.send()
}
object[keyPath: storageKeyPath].wrappedValue = newValue
}
}

public func validation(
manager: FormManager,
@@ -46,7 +58,7 @@ public class FormField<Value: Equatable, Validator: Validatable> where Value ==
},
onValidate: OnValidate? = nil
) -> ValidationContainer {
let pub: AnyPublisher<Value, Never> = $value.eraseToAnyPublisher()
let pub: AnyPublisher<Value, Never> = subject.eraseToAnyPublisher()
return ValidationFactory.create(
manager: manager,
validator: validator,

0 comments on commit 99c0e27

Please sign in to comment.