From f66d2e79adeb5c138cf43b91973e3e4cefd4727c Mon Sep 17 00:00:00 2001 From: Pavel Holec Date: Tue, 10 Sep 2024 11:23:58 +0200 Subject: [PATCH] Fix InputField and Textarea accessibility --- Sources/Orbit/Components/InputField.swift | 16 ++++++++------ Sources/Orbit/Components/Textarea.swift | 10 +++++++++ .../AccessibilityLabelValueModifier.swift | 22 ++++++++++++++----- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Sources/Orbit/Components/InputField.swift b/Sources/Orbit/Components/InputField.swift index 9ba73c8a8f1..ced4b2df929 100644 --- a/Sources/Orbit/Components/InputField.swift +++ b/Sources/Orbit/Components/InputField.swift @@ -54,6 +54,7 @@ public struct InputField: @Environment(\.inputFieldEndEditingAction) private var inputFieldEndEditingAction @Environment(\.isEnabled) private var isEnabled @Environment(\.locale) private var locale + @Environment(\.localizationBundle) private var localizationBundle @Environment(\.sizeCategory) private var sizeCategory @Environment(\.textColor) private var textColor @@ -136,19 +137,19 @@ public struct InputField: isFocused = false inputFieldEndEditingAction() } - .overlay( - resolvedPrompt, - alignment: .leadingFirstTextBaseline - ) - .accessibility { + .accessibility(children: nil) { label } value: { Text(value) } hint: { - Text(message?.description ?? "") + prompt } + .overlay( + resolvedPrompt, + alignment: .leadingFirstTextBaseline + ) } - + @ViewBuilder private var secureTextRedactedButton: some View { if showSecureTextRedactedButton { IconButton(isSecureTextRedacted ? .visibility : .visibilityOff) { @@ -178,6 +179,7 @@ public struct InputField: .lineLimit(1) .padding(.horizontal, .small) .allowsHitTesting(false) + .accessibility(hidden: true) } } diff --git a/Sources/Orbit/Components/Textarea.swift b/Sources/Orbit/Components/Textarea.swift index 199a91873fb..feac5d387ec 100644 --- a/Sources/Orbit/Components/Textarea.swift +++ b/Sources/Orbit/Components/Textarea.swift @@ -27,6 +27,8 @@ public struct Textarea: View, TextFieldBuildable { @Environment(\.inputFieldBeginEditingAction) private var inputFieldBeginEditingAction @Environment(\.inputFieldEndEditingAction) private var inputFieldEndEditingAction @Environment(\.isEnabled) private var isEnabled + @Environment(\.locale) private var locale + @Environment(\.localizationBundle) private var localizationBundle @Environment(\.sizeCategory) private var sizeCategory @State private var isFocused: Bool = false @@ -85,6 +87,13 @@ public struct Textarea: View, TextFieldBuildable { isFocused = false inputFieldEndEditingAction() } + .accessibility(children: nil) { + label + } value: { + Text(value) + } hint: { + prompt + } .overlay(resolvedPrompt, alignment: .topLeading) } @@ -94,6 +103,7 @@ public struct Textarea: View, TextFieldBuildable { .textColor(isEnabled ? state.placeholderColor : .cloudDarkActive) .padding(.small) .allowsHitTesting(false) + .accessibility(hidden: true) } } diff --git a/Sources/Orbit/Support/Accessibility/AccessibilityLabelValueModifier.swift b/Sources/Orbit/Support/Accessibility/AccessibilityLabelValueModifier.swift index c2021e3e806..f83c1d80bd9 100644 --- a/Sources/Orbit/Support/Accessibility/AccessibilityLabelValueModifier.swift +++ b/Sources/Orbit/Support/Accessibility/AccessibilityLabelValueModifier.swift @@ -5,17 +5,26 @@ struct AccessibilityLabelValueModifier: Vi @Environment(\.localizationBundle) private var localizationBundle @Environment(\.locale) private var locale + let children: AccessibilityChildBehavior? @ViewBuilder let label: Label @ViewBuilder let value: Value @ViewBuilder let hint: Hint func body(content: Content) -> some View { if isLabelTextual { - content - .accessibilityElement(children: .ignore) - .accessibility(label: textualLabel ?? SwiftUI.Text("")) - .accessibility(value: textualValue ?? SwiftUI.Text("")) - .accessibility(hint: textualHint ?? SwiftUI.Text("")) + if let children { + content + .accessibilityElement(children: children) + .accessibility(label: textualLabel ?? SwiftUI.Text("")) + .accessibility(value: textualValue ?? SwiftUI.Text("")) + .accessibility(hint: textualHint ?? SwiftUI.Text("")) + + } else { + content + .accessibility(label: textualLabel ?? SwiftUI.Text("")) + .accessibility(value: textualValue ?? SwiftUI.Text("")) + .accessibility(hint: textualHint ?? SwiftUI.Text("")) + } } else { content .accessibilityElement(children: .contain) @@ -42,10 +51,11 @@ struct AccessibilityLabelValueModifier: Vi extension View { func accessibility( + children: AccessibilityChildBehavior? = .ignore, @ViewBuilder label: () -> Label, @ViewBuilder value: () -> Value = { EmptyView() }, @ViewBuilder hint: () -> Hint = { EmptyView() } ) -> some View { - modifier(AccessibilityLabelValueModifier(label: label, value: value, hint: hint)) + modifier(AccessibilityLabelValueModifier(children: children, label: label, value: value, hint: hint)) } }