Explorar el Código

Add accessibilityLabel option to button (#5847)

* Add accessibilityLabel to tight and left buttons

* Set default accessibility label to back button

Co-authored-by: Yogev Ben David <yogevbd@wix.com>
Guy Carmeli hace 4 años
padre
commit
f635b5e8be

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/BackButton.java Ver fichero

@@ -3,6 +3,7 @@ package com.reactnativenavigation.parse;
3 3
 import com.reactnativenavigation.parse.params.Bool;
4 4
 import com.reactnativenavigation.parse.params.Button;
5 5
 import com.reactnativenavigation.parse.params.NullBool;
6
+import com.reactnativenavigation.parse.params.Text;
6 7
 import com.reactnativenavigation.parse.parsers.BoolParser;
7 8
 import com.reactnativenavigation.parse.parsers.ColorParser;
8 9
 import com.reactnativenavigation.parse.parsers.TextParser;
@@ -17,6 +18,7 @@ public class BackButton extends Button {
17 18
 
18 19
         result.hasValue = true;
19 20
         result.visible = BoolParser.parse(json, "visible");
21
+        result.accessibilityLabel = TextParser.parse(json, "accessibilityLabel", "Navigate Up");
20 22
         if (json.has("icon")) result.icon = TextParser.parse(json.optJSONObject("icon"), "uri");
21 23
         result.id = json.optString("id", Constants.BACK_BUTTON_ID);
22 24
         result.enabled = BoolParser.parse(json, "enabled");
@@ -30,6 +32,7 @@ public class BackButton extends Button {
30 32
 
31 33
     BackButton() {
32 34
         id = Constants.BACK_BUTTON_ID;
35
+        accessibilityLabel = new Text("Navigate Up");
33 36
     }
34 37
 
35 38
     public Bool visible = new NullBool();
@@ -41,6 +44,7 @@ public class BackButton extends Button {
41 44
 
42 45
     public void mergeWith(BackButton other) {
43 46
         if (!Constants.BACK_BUTTON_ID.equals(other.id)) id = other.id;
47
+        if (other.accessibilityLabel.hasValue()) accessibilityLabel = other.accessibilityLabel;
44 48
         if (other.icon.hasValue()) icon = other.icon;
45 49
         if (other.visible.hasValue()) visible = other.visible;
46 50
         if (other.color.hasValue()) color = other.color;
@@ -52,6 +56,7 @@ public class BackButton extends Button {
52 56
 
53 57
     void mergeWithDefault(final BackButton defaultOptions) {
54 58
         if (Constants.BACK_BUTTON_ID.equals(id)) id = defaultOptions.id;
59
+        if (!accessibilityLabel.hasValue()) accessibilityLabel = defaultOptions.accessibilityLabel;
55 60
         if (!icon.hasValue()) icon = defaultOptions.icon;
56 61
         if (!visible.hasValue()) visible = defaultOptions.visible;
57 62
         if (!color.hasValue()) color = defaultOptions.color;

+ 7
- 1
lib/android/app/src/main/java/com/reactnativenavigation/parse/params/Button.java Ver fichero

@@ -1,7 +1,6 @@
1 1
 package com.reactnativenavigation.parse.params;
2 2
 
3 3
 import android.graphics.Typeface;
4
-import androidx.annotation.Nullable;
5 4
 import android.view.MenuItem;
6 5
 
7 6
 import com.reactnativenavigation.parse.Component;
@@ -18,10 +17,13 @@ import org.json.JSONObject;
18 17
 import java.util.ArrayList;
19 18
 import java.util.Objects;
20 19
 
20
+import androidx.annotation.Nullable;
21
+
21 22
 public class Button {
22 23
     public String instanceId = "btn" + CompatUtils.generateViewId();
23 24
 
24 25
     @Nullable public String id;
26
+    public Text accessibilityLabel = new NullText();
25 27
     public Text text = new NullText();
26 28
     public Bool enabled = new NullBool();
27 29
     public Bool disableIconTint = new NullBool();
@@ -37,6 +39,7 @@ public class Button {
37 39
 
38 40
     public boolean equals(Button other) {
39 41
         return Objects.equals(id, other.id) &&
42
+               accessibilityLabel.equals(other.accessibilityLabel) &&
40 43
                text.equals(other.text) &&
41 44
                enabled.equals(other.enabled) &&
42 45
                disableIconTint.equals(other.disableIconTint) &&
@@ -54,6 +57,7 @@ public class Button {
54 57
     private static Button parseJson(JSONObject json, TypefaceLoader typefaceManager) {
55 58
         Button button = new Button();
56 59
         button.id = json.optString("id");
60
+        button.accessibilityLabel = TextParser.parse(json, "accessibilityLabel");
57 61
         button.text = TextParser.parse(json, "text");
58 62
         button.enabled = BoolParser.parse(json, "enabled");
59 63
         button.disableIconTint = BoolParser.parse(json, "disableIconTint");
@@ -133,6 +137,7 @@ public class Button {
133 137
 
134 138
     public void mergeWith(Button other) {
135 139
         if (other.text.hasValue()) text = other.text;
140
+        if (other.accessibilityLabel.hasValue()) accessibilityLabel = other.accessibilityLabel;
136 141
         if (other.enabled.hasValue()) enabled = other.enabled;
137 142
         if (other.disableIconTint.hasValue()) disableIconTint = other.disableIconTint;
138 143
         if (other.color.hasValue()) color = other.color;
@@ -150,6 +155,7 @@ public class Button {
150 155
 
151 156
     public void mergeWithDefault(Button defaultOptions) {
152 157
         if (!text.hasValue()) text = defaultOptions.text;
158
+        if (!accessibilityLabel.hasValue()) accessibilityLabel = defaultOptions.accessibilityLabel;
153 159
         if (!enabled.hasValue()) enabled = defaultOptions.enabled;
154 160
         if (!disableIconTint.hasValue()) disableIconTint = defaultOptions.disableIconTint;
155 161
         if (!color.hasValue()) color = defaultOptions.color;

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/parsers/TextParser.java Ver fichero

@@ -2,12 +2,17 @@ package com.reactnativenavigation.parse.parsers;
2 2
 
3 3
 import com.reactnativenavigation.parse.params.NullText;
4 4
 import com.reactnativenavigation.parse.params.Text;
5
+import com.reactnativenavigation.utils.ObjectUtils;
5 6
 
6 7
 import org.json.JSONObject;
7 8
 
8 9
 import javax.annotation.*;
9 10
 
10 11
 public class TextParser {
12
+    public static Text parse(@Nullable JSONObject json, String text, String defaultValue) {
13
+        return ObjectUtils.take(parse(json, text), new Text(defaultValue));
14
+    }
15
+
11 16
     public static Text parse(@Nullable JSONObject json, String text) {
12 17
         return json != null && json.has(text) ? new Text(json.optString(text)) : new NullText();
13 18
     }

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/utils/ObjectUtils.java Ver fichero

@@ -3,6 +3,7 @@ package com.reactnativenavigation.utils;
3 3
 import com.reactnativenavigation.utils.Functions.Func1;
4 4
 import com.reactnativenavigation.utils.Functions.FuncR1;
5 5
 
6
+import androidx.annotation.NonNull;
6 7
 import androidx.annotation.Nullable;
7 8
 
8 9
 public class ObjectUtils {
@@ -14,6 +15,10 @@ public class ObjectUtils {
14 15
         return obj == null ? defaultValue : action.run(obj);
15 16
     }
16 17
 
18
+    public static <T> T take(@Nullable T obj, @NonNull T defaultValue) {
19
+        return obj == null ? defaultValue : obj;
20
+    }
21
+
17 22
     public static boolean notNull(Object o) {
18 23
         return o != null;
19 24
     }

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/TitleBarButtonController.java Ver fichero

@@ -28,6 +28,7 @@ import androidx.annotation.NonNull;
28 28
 import androidx.annotation.RestrictTo;
29 29
 import androidx.appcompat.widget.ActionMenuView;
30 30
 import androidx.appcompat.widget.Toolbar;
31
+import androidx.core.view.MenuItemCompat;
31 32
 
32 33
 public class TitleBarButtonController extends ViewController<TitleBarReactButtonView> implements MenuItem.OnMenuItemClickListener {
33 34
     public interface OnClickListener {
@@ -105,6 +106,7 @@ public class TitleBarButtonController extends ViewController<TitleBarReactButton
105 106
             toolbar.setNavigationOnClickListener(view -> onPressListener.onPress(button.id));
106 107
             toolbar.setNavigationIcon(icon);
107 108
             setLeftButtonTestId(toolbar);
109
+            if (button.accessibilityLabel.hasValue()) toolbar.setNavigationContentDescription(button.accessibilityLabel.get());
108 110
         });
109 111
     }
110 112
 
@@ -125,7 +127,9 @@ public class TitleBarButtonController extends ViewController<TitleBarReactButton
125 127
         menuItem.setOnMenuItemClickListener(this);
126 128
         if (button.hasComponent()) {
127 129
             menuItem.setActionView(getView());
130
+            if (button.accessibilityLabel.hasValue()) getView().setContentDescription(button.accessibilityLabel.get());
128 131
         } else {
132
+            if (button.accessibilityLabel.hasValue()) MenuItemCompat.setContentDescription(menuItem, button.accessibilityLabel.get());
129 133
             if (button.hasIcon()) {
130 134
                 loadIcon(new ImageLoadingListenerAdapter() {
131 135
                     @Override