-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicker.swift
More file actions
61 lines (54 loc) · 1.84 KB
/
Copy pathPicker.swift
File metadata and controls
61 lines (54 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Picker.swift
// AndroidSwiftUICore
//
/// Associates a selection value with a picker row.
public struct _TagModifier: RenderModifier {
let value: String
public var _modifierNode: ModifierNode {
ModifierNode(kind: "tag", args: ["value": .string(value)])
}
}
public extension View {
func tag<V: Hashable>(_ value: V) -> ModifiedContent<Self, _TagModifier> {
modifier(_TagModifier(value: identityString(value)))
}
}
/// A control for selecting one of several tagged options.
///
/// Selection values round-trip the bridge as strings, so they must be
/// `LosslessStringConvertible` (String and Int both are).
public struct Picker<SelectionValue: Hashable & LosslessStringConvertible, Content: View>: View {
internal let title: String
internal let selection: Binding<SelectionValue>
internal let content: Content
public init<S: StringProtocol>(
_ title: S,
selection: Binding<SelectionValue>,
@ViewBuilder content: () -> Content
) {
self.title = String(title)
self.selection = selection
self.content = content()
}
public typealias Body = Never
}
extension Picker: PrimitiveView {
public func _render(in context: ResolveContext) -> RenderNode {
let binding = selection
let callbackID = context.callbacks.register(.string { raw in
guard let value = SelectionValue(raw) else { return }
binding.wrappedValue = value
})
return RenderNode(
type: "Picker",
id: context.path,
props: [
"title": .string(title),
"selection": .string(identityString(selection.wrappedValue)),
"onChange": .int(Int(callbackID)),
],
children: Evaluator.resolveChildren(content, context.descending("content"))
)
}
}