Skip to content

Commit b099ceb

Browse files
authored
Add role attribute to Button and remove disabled (#1282)
1 parent d2f9faf commit b099ceb

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// ButtonRole.swift
3+
//
4+
//
5+
// Created by Carson Katri on 2/29/24.
6+
//
7+
8+
import SwiftUI
9+
import LiveViewNativeCore
10+
11+
/// The semantic role of a ``Button``.
12+
///
13+
/// Possible values:
14+
/// * `destructive`
15+
/// * `cancel`
16+
extension ButtonRole: AttributeDecodable {
17+
public init(from attribute: LiveViewNativeCore.Attribute?) throws {
18+
guard let value = attribute?.value
19+
else { throw AttributeDecodingError.missingAttribute(Self.self) }
20+
switch value {
21+
case "destructive":
22+
self = .destructive
23+
case "cancel":
24+
self = .cancel
25+
default:
26+
throw AttributeDecodingError.badValue(Self.self)
27+
}
28+
}
29+
}

Sources/LiveViewNative/Views/Controls and Indicators/Buttons/Button.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import SwiftUI
2222
/// ```
2323
///
2424
/// ## Attributes
25-
/// * ``disabled``
26-
///
25+
/// * ``role``
26+
///
2727
/// ## Events
2828
/// * ``click``
2929
@_documentation(visibility: public)
@@ -38,19 +38,22 @@ public struct Button<R: RootRegistry>: View {
3838
@_documentation(visibility: public)
3939
@Event("phx-click", type: "click") private var click
4040

41-
/// Boolean attribute that indicates whether the button is tappable.
41+
/// The semantic role of the button.
42+
///
43+
/// Possible values:
44+
/// * `destructive`
45+
/// * `cancel`
4246
@_documentation(visibility: public)
43-
@Attribute("disabled") private var disabled: Bool
47+
@Attribute("role") private var role: ButtonRole?
4448

4549
@_spi(LiveForm) public init(action: (() -> Void)? = nil) {
4650
self.action = action
4751
}
4852

4953
public var body: some View {
50-
SwiftUI.Button(action: self.handleClick) {
54+
SwiftUI.Button(role: role, action: self.handleClick) {
5155
context.buildChildren(of: element)
5256
}
53-
.disabled(disabled)
5457
.preference(key: _ProvidedBindingsKey.self, value: ["phx-click"])
5558
}
5659

0 commit comments

Comments
 (0)