Skip to content

Commit c2020d4

Browse files
authored
Use value from document as default for unbound FormState (#1464)
* Use value from document as default for unbound `FormState` * Update changelog * Allow client side updates to unbound fields
1 parent 3c4ddc1 commit c2020d4

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- `+` characters are properly encoded as `%2B` in form events (#1449)
2222
- Fixed retain cycle in `LiveViewCoordinator` (#1455)
2323
- Made `StylesheetCache` thread-safe, fixing occasional crashes (#1461)
24+
- Form elements outside of a `LiveForm` will show the value provided in the template (#1464)
2425

2526
## [0.3.0] 2024-08-21
2627

Sources/LiveViewNative/Property Wrappers/FormState.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private let logger = Logger(subsystem: "LiveViewNative", category: "FormState")
5858
public struct FormState<Value: FormValue> {
5959
private let defaultValue: Value
6060
private let sendChangeEvents: Bool
61-
@StateObject private var data = FormStateData<Value>()
61+
@StateObject private var data: FormStateData<Value>
6262

6363
let valueAttribute: AttributeName
6464

@@ -98,6 +98,7 @@ public struct FormState<Value: FormValue> {
9898
self.valueAttribute = valueAttribute
9999
self.defaultValue = `default`
100100
self.sendChangeEvents = sendChangeEvents
101+
self._data = StateObject(wrappedValue: FormStateData<Value>(default: `default`))
101102
}
102103

103104
/// Convenience initializer that creates a `FormState` property wrapper with `nil` as its default value.
@@ -128,7 +129,7 @@ public struct FormState<Value: FormValue> {
128129
case .unknown:
129130
fatalError("@FormState cannot be accessed before being installed in a view")
130131
case .local:
131-
return defaultValue
132+
return data.localValue
132133
case .form(let formModel):
133134
guard let elementName = element.attributeValue(for: "name") else {
134135
logger.log(level: .error, "Expected @FormState in form mode to have element with name")
@@ -148,7 +149,7 @@ public struct FormState<Value: FormValue> {
148149
case .unknown:
149150
fatalError("@FormState cannot be accessed before being installed in a view")
150151
case .local:
151-
break
152+
data.localValue = newValue
152153
case .form(let formModel):
153154
guard let elementName = element.attributeValue(for: "name") else {
154155
logger.log(level: .error, "Expected @FormState in form mode to have element with name")
@@ -183,7 +184,8 @@ public struct FormState<Value: FormValue> {
183184

184185
private func resolveMode() {
185186
let elementName = element.attributeValue(for: "name")
186-
if case .unknown = data.mode {
187+
switch data.mode {
188+
case .unknown:
187189
if let formModel {
188190
if let elementName {
189191
data.bind(
@@ -204,7 +206,13 @@ public struct FormState<Value: FormValue> {
204206
} else {
205207
logger.warning("Form element used outside of a <LiveForm>. This may not behave as expected.")
206208
data.mode = .local
209+
data.localValue = initialValue
207210
}
211+
case .local:
212+
guard !isFocused && !isEditing else { return }
213+
data.localValue = initialValue
214+
default:
215+
break
208216
}
209217
}
210218

@@ -238,10 +246,17 @@ extension FormState: DynamicProperty {
238246
// It also serves to notify SwiftUI when this @FormState's particular field has changed (as opposed to updates for other fields).
239247
private class FormStateData<Value: FormValue>: ObservableObject {
240248
var mode: Mode = .unknown
249+
250+
var localValue: Value
251+
241252
private var cancellable: AnyCancellable?
242253
private var elementCancellable: AnyCancellable?
243254
private var focusCancellable: AnyCancellable?
244255

256+
init(default localValue: Value) {
257+
self.localValue = localValue
258+
}
259+
245260
func bind(
246261
_ formModel: FormModel,
247262
to element: ObservedElement,

0 commit comments

Comments
 (0)