Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve BPKSelect testibility #2067

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions Backpack-SwiftUI/Select/Classes/BPKSelect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public struct BPKSelect: View {
@Binding private var selectedIndex: Int?

private let options: [String]
private let optionsIdentifiers: [String]
private let placeholder: String
private var state: State = .default
private var resolvedState: State {
Expand All @@ -79,17 +80,20 @@ public struct BPKSelect: View {
public init(
placeholder: String,
options: [String],
optionsIdentifiers: [String] = [],
selectedIndex: Binding<Int?>
) {
self.placeholder = placeholder
self.options = options
self.optionsIdentifiers = optionsIdentifiers
_selectedIndex = selectedIndex
}

public var body: some View {
Picker(placeholder, selection: $selectedIndex) {
ForEach(0..<options.count, id: \.self) { index in
Text(options[index])
.accessibilityIdentifier(accessibilityIdentifier(for: index))
.tag(Optional(index))
}
}
Expand All @@ -106,6 +110,10 @@ public struct BPKSelect: View {
result.state = state
return result
}

private func accessibilityIdentifier(for index: Int) -> String {
return optionsIdentifiers.isEmpty ? "select_option_\(index)" : optionsIdentifiers[index]
}
}

struct BPKSelect_Previews: PreviewProvider {
Expand Down
Loading