Skip to content

Commit

Permalink
Add accessibilityElements to accessibilityContainer API [UI-6096]
Browse files Browse the repository at this point in the history
  • Loading branch information
nsillik committed Apr 26, 2024
1 parent 04822e7 commit 19a232d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
13 changes: 9 additions & 4 deletions BlueprintUICommonControls/Sources/AccessibilityContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public struct AccessibilityContainer: Element {
/// An optional `accessibilityIdentifier` to give the container. Defaults to `nil`.
public var identifier: String?
public var wrapped: Element
public var accessibilityElements: [Any]?

/// Creates a new `AccessibilityContainer` wrapping the provided element.
public init(identifier: String? = nil, wrapping element: Element) {
public init(identifier: String? = nil, accessibilityElements: [Any]? = nil, wrapping element: Element) {
self.identifier = identifier
self.accessibilityElements = accessibilityElements
wrapped = element
}

Expand All @@ -36,6 +38,7 @@ public struct AccessibilityContainer: Element {
public func backingViewDescription(with context: ViewDescriptionContext) -> ViewDescription? {
AccessibilityContainerView.describe { config in
config[\.accessibilityIdentifier] = identifier
config[\.explicitAccessibilityElements] = self.accessibilityElements
}
}
}
Expand All @@ -44,15 +47,17 @@ extension Element {

/// Acts as an accessibility container for any subviews
/// where `isAccessibilityElement == true`.
public func accessibilityContainer(identifier: String? = nil) -> Element {
AccessibilityContainer(identifier: identifier, wrapping: self)
public func accessibilityContainer(identifier: String? = nil, accessibilityElements: [Any]? = nil) -> Element {
AccessibilityContainer(identifier: identifier, accessibilityElements: accessibilityElements, wrapping: self)
}
}

extension AccessibilityContainer {
private final class AccessibilityContainerView: UIView {
var explicitAccessibilityElements: [Any]?

override var accessibilityElements: [Any]? {
get { recursiveAccessibleSubviews() }
get { explicitAccessibilityElements ?? recursiveAccessibleSubviews() }
set { fatalError("This property is not settable") }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,50 @@ class AccessibilityContainerTests: XCTestCase {

XCTAssertNil(accessibleSubviews.first)
}

func test_overrideAccessibility() {
let label1 = Label(text: "One")
let label2 = Label(text: "Two")
let label3 = Label(text: "Three")
let element = Column {
label1
label2
label3
}

let view = BlueprintView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

element
.accessibilityContainer()
.accessBackingView(in: view) { view in
let textVals: [String] = view.accessibilityElements!.map {
($0 as! UILabel).text!
}

XCTAssertEqual(view.accessibilityElements?.count, 3)
XCTAssertEqual(textVals, ["One", "Two", "Three"])
}

element
.accessibilityContainer(accessibilityElements: [label1, label3])
.accessBackingView(in: view) { view in
let textVals: [String] = view.accessibilityElements!.map {
($0 as! BlueprintUICommonControls.Label).text
}

XCTAssertEqual(view.accessibilityElements?.count, 2)
XCTAssertEqual(textVals, ["One", "Three"])
}

element
.accessibilityContainer(accessibilityElements: [label3, label2, label1])
.accessBackingView(in: view) { view in
let textVals: [String] = view.accessibilityElements!.map {
($0 as! BlueprintUICommonControls.Label).text
}

XCTAssertEqual(view.accessibilityElements?.count, 3)
XCTAssertEqual(textVals, ["Three", "Two", "One"])
}
}
}

0 comments on commit 19a232d

Please sign in to comment.