Skip to content

Commit e23c04f

Browse files
committed
DON-335: Additional changes
1 parent 552feb4 commit e23c04f

File tree

6 files changed

+43
-10
lines changed

6 files changed

+43
-10
lines changed

Backpack-SwiftUI/AppSearchModal/Classes/AppSearchModalContentView.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct AppSearchModalContentView: View {
4545
BPKSingleSelectChipGroup(
4646
chips: shortcuts.map({ BPKSingleSelectChipGroup.ChipItem( text: $0.text, icon: $0.icon) }),
4747
selectedIndex: .constant(nil),
48+
accessibilityPrefix: "shortcuts",
4849
onItemClick: { shortcuts[$0].onShortcutSelected() }
4950
)
5051
.insetPadding(.horizontal, .base)
@@ -56,16 +57,20 @@ struct AppSearchModalContentView: View {
5657
HStack {
5758
BPKText(heading.title, style: .label1)
5859
.accessibilityAddTraits(.isHeader)
60+
.accessibilityIdentifier("\(section.accessibilityPrefix)-header-text")
5961
Spacer()
6062
if let action = heading.action {
6163
BPKButton(action.text, action: action.onActionSelected)
6264
.buttonStyle(.link)
65+
.accessibilityIdentifier("\(section.accessibilityPrefix)-header-action")
6366
}
6467
}
6568
.padding(.horizontal, .base)
6669
}
67-
ForEach(section.items, id: \.self) { item in
68-
ItemCell(item: item)
70+
ForEach(Array(section.items.enumerated()), id: \.element) { index, item in
71+
ItemCell(
72+
item: item,
73+
accessibilityID: itemAccessibilityID(prefix: section.accessibilityPrefix, index: index))
6974
}
7075
}
7176
}
@@ -75,6 +80,8 @@ struct AppSearchModalContentView: View {
7580

7681
let item: BPKAppSearchModalContent.Item
7782

83+
let accessibilityID: String
84+
7885
var body: some View {
7986
Button(action: item.onItemSelected) {
8087
HStack(spacing: .base) {
@@ -100,6 +107,7 @@ struct AppSearchModalContentView: View {
100107
.contentShape(Rectangle())
101108
}
102109
.buttonStyle(ItemCellButtonStyle())
110+
.accessibilityIdentifier(accessibilityID)
103111
}
104112

105113
private func subtitleLineLimit() -> Int? {
@@ -117,6 +125,10 @@ struct AppSearchModalContentView: View {
117125
.background(configuration.isPressed ? .canvasContrastColor : .canvasContrastColor.withAlphaComponent(0))
118126
}
119127
}
128+
129+
private func itemAccessibilityID(prefix: String, index: Int) -> String {
130+
return "\(prefix.trimmingCharacters(in: .whitespacesAndNewlines))-\(index)"
131+
}
120132
}
121133

122134
struct AppSearchModalContentView_Previews: PreviewProvider {
@@ -130,7 +142,8 @@ struct AppSearchModalContentView_Previews: PreviewProvider {
130142
static func buildSection(with index: Int) -> BPKAppSearchModalContent.Section {
131143
return .init(
132144
heading: .init(title: "Section \(index + 1)", action: .init(text: "Action", onActionSelected: { })),
133-
items: (0..<3).map(buildItem)
145+
items: (0..<3).map(buildItem),
146+
accessibilityPrefix: "section-\(index)"
134147
)
135148
}
136149

Backpack-SwiftUI/AppSearchModal/Classes/BPKAppSearchModal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ struct BPKAppSearchModal_Previews: PreviewProvider {
161161
static func buildSection(with index: Int) -> BPKAppSearchModalContent.Section {
162162
return .init(
163163
heading: .init(title: "Section \(index + 1)", action: .init(text: "Action", onActionSelected: { })),
164-
items: (0..<1).map(buildItem)
164+
items: (0..<1).map(buildItem), accessibilityPrefix: "section-\(index)"
165165
)
166166
}
167167

Backpack-SwiftUI/AppSearchModal/Classes/BPKAppSearchModalResults.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ public struct BPKAppSearchModalContent: Equatable {
4848
public struct Section: Hashable {
4949
let heading: SectionHeading?
5050
let items: [Item]
51-
52-
public init(heading: SectionHeading?, items: [Item]) {
51+
let accessibilityPrefix: String
52+
53+
public init(
54+
heading: SectionHeading?,
55+
items: [Item],
56+
accessibilityPrefix: String
57+
) {
5358
self.heading = heading
5459
self.items = items
60+
self.accessibilityPrefix = accessibilityPrefix
5561
}
5662
}
5763

Backpack-SwiftUI/ChipGroup/Classes/BPKSingleSelectChipGroup.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public struct BPKSingleSelectChipGroup: View {
2525
private let type: BPKChipGroupType
2626
private let onItemClick: (_ index: Int) -> Void
2727
private var insetSpacing: (Edge.Set, BPKSpacing)
28+
private var accessibilityPrefix: String
2829

2930
@Binding private var selectedIndex: Int?
3031

@@ -33,13 +34,15 @@ public struct BPKSingleSelectChipGroup: View {
3334
style: BPKChipStyle = .default,
3435
selectedIndex: Binding<Int?>,
3536
type: BPKChipGroupType = .rail,
37+
accessibilityPrefix: String? = nil,
3638
onItemClick: @escaping (_ index: Int) -> Void
3739
) {
3840
self.chips = chips
3941
self.style = style
4042
self._selectedIndex = selectedIndex
4143
self.type = type
4244
self.insetSpacing = (.all, .none)
45+
self.accessibilityPrefix = accessibilityPrefix ?? "chipgroup"
4346
self.onItemClick = onItemClick
4447
}
4548

@@ -81,6 +84,12 @@ public struct BPKSingleSelectChipGroup: View {
8184
selectedIndex = index
8285
onItemClick(index)
8386
}
87+
.accessibilityIdentifier(formAccessibilityIdentifier(for: index))
88+
}
89+
90+
private func formAccessibilityIdentifier(for index: Int) -> String {
91+
let identifier = [accessibilityPrefix, "chip", String(index)].compactMap({ $0 }).joined(separator: "-")
92+
return identifier
8493
}
8594
}
8695

Backpack-SwiftUI/Tests/AppSearchModal/BPKAppSearchModalTests.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,19 @@ class BPKAppSearchModalTests: XCTestCase {
9898
private func givenContentState() -> BPKAppSearchModalContent {
9999
return .init(
100100
sections: [
101-
.init(heading: nil, items: [buildItem(with: 0)]),
102-
.init(heading: .init(title: "Section 2", action: nil), items: (0..<3).map(buildItem)),
101+
.init(heading: nil, items: [buildItem(with: 0)], accessibilityPrefix: "Section 1"),
102+
.init(
103+
heading: .init(title: "Section 2", action: nil),
104+
items: (0..<3).map(buildItem),
105+
accessibilityPrefix: "Section 2"
106+
),
103107
.init(
104108
heading: .init(
105109
title: "Section 3",
106110
action: .init(text: "Action", onActionSelected: { })
107111
),
108-
items: (0..<3).map(buildItem)
112+
items: (0..<3).map(buildItem),
113+
accessibilityPrefix: "Section 3"
109114
)
110115
],
111116
shortcuts: (0..<4).map(buildShortcut)

Example/Backpack/SwiftUI/Components/AppSearchModal/AppSearchModalExampleView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct AppSearchModalExampleView: View {
9494
}
9595
)
9696
),
97-
items: (0..<3).map { buildItem(with: $0, section: index) }
97+
items: (0..<3).map { buildItem(with: $0, section: index) }, accessibilityPrefix: "section-\(index)"
9898
)
9999
}
100100

0 commit comments

Comments
 (0)