Skip to content

Commit 3d23bb4

Browse files
authored
Merge branch 'master' into shadowfacts/divider
2 parents ab2a46e + 79acd10 commit 3d23bb4

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

Sources/LiveViewNative/BuiltinRegistry.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ struct BuiltinRegistry {
4848
Link(element: element, context: context)
4949
case "divider":
5050
Divider()
51-
51+
case "edit-button":
52+
EditButton()
53+
case "toggle":
54+
Toggle(element: element, context: context)
5255
case "phx-form":
5356
PhxForm<R>(element: element, context: context)
5457
case "phx-submit-button":

Sources/LiveViewNative/ViewModel.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,15 @@ extension String: FormValue {
206206
self = formValue
207207
}
208208
}
209+
210+
extension Bool: FormValue {
211+
public var formValue: String {
212+
self.description
213+
}
214+
215+
public init?(formValue: String) {
216+
guard let value = Bool(formValue)
217+
else { return nil }
218+
self = value
219+
}
220+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// Toggle.swift
3+
//
4+
//
5+
// Created by Carson Katri on 1/17/23.
6+
//
7+
8+
import SwiftUI
9+
10+
struct Toggle<R: CustomRegistry>: View {
11+
@ObservedElement private var element: ElementNode
12+
let context: LiveContext<R>
13+
14+
@FormState(default: false) var value: Bool
15+
16+
init(element: ElementNode, context: LiveContext<R>) {
17+
self.context = context
18+
}
19+
20+
public var body: some View {
21+
SwiftUI.Toggle(isOn: $value) {
22+
context.buildChildren(of: element)
23+
}
24+
.applyToggleStyle(
25+
element.attributeValue(for: "toggle-style").flatMap(ToggleStyle.init) ?? .automatic
26+
)
27+
}
28+
}
29+
30+
fileprivate enum ToggleStyle: String {
31+
case automatic
32+
case button
33+
case `switch`
34+
#if os(macOS)
35+
case checkbox
36+
#endif
37+
}
38+
39+
fileprivate extension View {
40+
@ViewBuilder
41+
func applyToggleStyle(_ style: ToggleStyle) -> some View {
42+
switch style {
43+
case .automatic:
44+
self.toggleStyle(.automatic)
45+
case .button:
46+
self.toggleStyle(.button)
47+
case .`switch`:
48+
self.toggleStyle(.switch)
49+
#if os(macOS)
50+
case .checkbox:
51+
self.toggleStyle(.checkbox)
52+
#endif
53+
}
54+
}
55+
}

Sources/LiveViewNative/Views/Layout Containers/Scroll Views/ScrollView.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,28 @@ struct ScrollView<R: CustomRegistry>: View {
1616
}
1717

1818
public var body: some View {
19-
SwiftUI.ScrollView {
19+
SwiftUI.ScrollView(axes, showsIndicators: showsIndicators) {
2020
context.buildChildren(of: element)
2121
}
2222
}
23+
24+
private var axes: Axis.Set {
25+
switch element.attributeValue(for: "axes") {
26+
case "all":
27+
return [.horizontal, .vertical]
28+
case "horizontal":
29+
return .horizontal
30+
default:
31+
return .vertical
32+
}
33+
}
34+
35+
private var showsIndicators: Bool {
36+
if let attr = element.attributeValue(for: "shows-indicators") {
37+
return attr == "true"
38+
} else {
39+
return true
40+
}
41+
}
42+
2343
}

0 commit comments

Comments
 (0)