Skip to content

Commit

Permalink
Fixed state issue preventing modifications to string properties being…
Browse files Browse the repository at this point in the history
… applied
  • Loading branch information
SteveBarnegren committed Sep 15, 2023
1 parent d073e4d commit 5ca334d
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions MirrorUI/MirrorUI/View Mapping/ViewMapping+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,29 @@ extension ViewMapping {

static let string: ViewMapping = {
let mapping = ViewMapping(for: String.self) { ref, context in
return AnyView(StringEditView(ref: ref, context: context))
}
return mapping
}()
}

var partial: String {
get { context.state.value["text"] as? String ?? ref.value }
set { context.state.value["text"] = newValue }
}
private struct StringEditView: View {

let binding = Binding(get: { partial },
set: { partial = $0 })
private let ref: PropertyRef<String>
private let context: ViewMappingContext
@State private var text: String

let view = HStack {
Text("\(context.propertyName):")
TextField(context.propertyName, text: binding, onCommit: {
ref.value = partial
context.state.value["text"] = nil
})
}
init(ref: PropertyRef<String>, context: ViewMappingContext) {
self.ref = ref
self.context = context
self.text = ref.value
}

return AnyView(view)
}
return mapping
}()
@ViewBuilder
var body: some View {
Text("\(context.propertyName):")
TextField(context.propertyName, text: $text, onCommit: {
ref.value = text
})
}
}

0 comments on commit 5ca334d

Please sign in to comment.