Skip to content

Commit

Permalink
Fix InputField and Textarea accessibility
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelHolec committed Sep 10, 2024
1 parent 91a17ba commit f66d2e7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
16 changes: 9 additions & 7 deletions Sources/Orbit/Components/InputField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
@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

Expand Down Expand Up @@ -136,19 +137,19 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
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) {
Expand Down Expand Up @@ -178,6 +179,7 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
.lineLimit(1)
.padding(.horizontal, .small)
.allowsHitTesting(false)
.accessibility(hidden: true)
}
}

Expand Down
10 changes: 10 additions & 0 deletions Sources/Orbit/Components/Textarea.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public struct Textarea<Label: View, Prompt: View>: 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
Expand Down Expand Up @@ -85,6 +87,13 @@ public struct Textarea<Label: View, Prompt: View>: View, TextFieldBuildable {
isFocused = false
inputFieldEndEditingAction()
}
.accessibility(children: nil) {
label
} value: {
Text(value)
} hint: {
prompt
}
.overlay(resolvedPrompt, alignment: .topLeading)
}

Expand All @@ -94,6 +103,7 @@ public struct Textarea<Label: View, Prompt: View>: View, TextFieldBuildable {
.textColor(isEnabled ? state.placeholderColor : .cloudDarkActive)
.padding(.small)
.allowsHitTesting(false)
.accessibility(hidden: true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@ struct AccessibilityLabelValueModifier<Label: View, Value: View, Hint: View>: 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)
Expand All @@ -42,10 +51,11 @@ struct AccessibilityLabelValueModifier<Label: View, Value: View, Hint: View>: Vi
extension View {

func accessibility<Label: View, Value: View, Hint: View>(
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))
}
}

0 comments on commit f66d2e7

Please sign in to comment.