Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions Sources/SwiftNavigation/ButtonState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ public struct ButtonState<Action>: Identifiable {
public let action: ButtonStateAction<Action>
public let label: TextState
public let role: ButtonStateRole?
public let accessibilityIdentifier: String?

init(
id: UUID,
action: ButtonStateAction<Action>,
label: TextState,
role: ButtonStateRole?
role: ButtonStateRole?,
accessibilityIdentifier: String? = nil
) {
self.id = id
self.action = action
self.label = label
self.role = role
self.accessibilityIdentifier = accessibilityIdentifier
}

/// Creates button state.
Expand Down Expand Up @@ -55,6 +58,23 @@ public struct ButtonState<Action>: Identifiable {
self.init(id: UUID(), action: .send(action), label: label(), role: role)
}

/// Sets the identifier UI tests use to find this button.
///
/// Applied when the button is rendered in a SwiftUI alert or confirmation dialog. `UIAlertAction`
/// has no accessibility identifier, so it is not applied by `UIAlertController`.
///
/// - Parameter identifier: The accessibility identifier to apply.
/// - Returns: Button state with the given accessibility identifier.
public func accessibilityIdentifier(_ identifier: String) -> Self {
Self(
id: self.id,
action: self.action,
label: self.label,
role: self.role,
accessibilityIdentifier: identifier
)
}

/// Handle the button's action in a closure.
///
/// - Parameter perform: Unwraps and passes a button's action to a closure to be performed. If the
Expand Down Expand Up @@ -120,7 +140,8 @@ public struct ButtonState<Action>: Identifiable {
id: self.id,
action: self.action.map(transform),
label: self.label,
role: self.role
role: self.role,
accessibilityIdentifier: self.accessibilityIdentifier
)
}
}
Expand Down Expand Up @@ -194,6 +215,7 @@ extension ButtonState: Equatable where Action: Equatable {
lhs.action == rhs.action
&& lhs.label == rhs.label
&& lhs.role == rhs.role
&& lhs.accessibilityIdentifier == rhs.accessibilityIdentifier
}
}

Expand All @@ -216,6 +238,7 @@ extension ButtonState: Hashable where Action: Hashable {
hasher.combine(self.action)
hasher.combine(self.label)
hasher.combine(self.role)
hasher.combine(self.accessibilityIdentifier)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- ``init(role:action:label:)-8tmop``
- ``ButtonStateRole``
- ``ButtonStateAction``
- ``accessibilityIdentifier(_:)``

### Composing buttons

Expand All @@ -19,6 +20,7 @@
- ``role-swift.property``
- ``action``
- ``label``
- ``accessibilityIdentifier-swift.property``

### Performing actions

Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftNavigation/Traits/CustomDump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
}
children.append(("action", self.action))
children.append(("label", self.label))
if let accessibilityIdentifier = self.accessibilityIdentifier {
children.append(("accessibilityIdentifier", accessibilityIdentifier))
}
return Mirror(
self,
children: children,
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftUINavigation/Alert+State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
} actions: {
ForEach($0.buttons) {
Button($0, action: handler)
.accessibilityIdentifier(ifPresent: $0.accessibilityIdentifier)
}
} message: {
$0.message.map(Text.init)
Expand Down Expand Up @@ -74,6 +75,7 @@
} actions: {
ForEach($0.buttons) {
Button($0, action: handler)
.accessibilityIdentifier(ifPresent: $0.accessibilityIdentifier)
}
} message: {
$0.message.map(Text.init)
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftUINavigation/ConfirmationDialog+State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
} actions: {
ForEach($0.buttons) {
Button($0, action: handler)
.accessibilityIdentifier(ifPresent: $0.accessibilityIdentifier)
}
} message: {
$0.message.map(Text.init)
Expand Down Expand Up @@ -92,6 +93,7 @@
} actions: {
ForEach($0.buttons) {
Button($0, action: handler)
.accessibilityIdentifier(ifPresent: $0.accessibilityIdentifier)
}
} message: {
$0.message.map(Text.init)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#if canImport(SwiftUI)
import SwiftUI

extension View {
@ViewBuilder
func accessibilityIdentifier(ifPresent identifier: String?) -> some View {
if let identifier {
accessibilityIdentifier(identifier)
} else {
self
}
}
}
#endif
13 changes: 13 additions & 0 deletions Tests/SwiftNavigationTests/ButtonStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
import Testing

struct ButtonStateTests {
@Test
func testAccessibilityIdentifier() {
let button = ButtonState(action: 1) {
TextState("OK")
}
let identified = button.accessibilityIdentifier("ok")

#expect(button.accessibilityIdentifier == nil)
#expect(identified.accessibilityIdentifier == "ok")
#expect(identified.map { $0.map(String.init) }.accessibilityIdentifier == "ok")
#expect(button != identified)
}

@Test
func testAsyncAnimationWarning() async {
let button = ButtonState(action: .send((), animation: .easeInOut)) {
Expand Down