Browse Source

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 4 years ago
parent
commit
f635b5e8be

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

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

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

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

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

2
 
2
 
3
 import com.reactnativenavigation.parse.params.NullText;
3
 import com.reactnativenavigation.parse.params.NullText;
4
 import com.reactnativenavigation.parse.params.Text;
4
 import com.reactnativenavigation.parse.params.Text;
5
+import com.reactnativenavigation.utils.ObjectUtils;
5
 
6
 
6
 import org.json.JSONObject;
7
 import org.json.JSONObject;
7
 
8
 
8
 import javax.annotation.*;
9
 import javax.annotation.*;
9
 
10
 
10
 public class TextParser {
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
     public static Text parse(@Nullable JSONObject json, String text) {
16
     public static Text parse(@Nullable JSONObject json, String text) {
12
         return json != null && json.has(text) ? new Text(json.optString(text)) : new NullText();
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 View File

3
 import com.reactnativenavigation.utils.Functions.Func1;
3
 import com.reactnativenavigation.utils.Functions.Func1;
4
 import com.reactnativenavigation.utils.Functions.FuncR1;
4
 import com.reactnativenavigation.utils.Functions.FuncR1;
5
 
5
 
6
+import androidx.annotation.NonNull;
6
 import androidx.annotation.Nullable;
7
 import androidx.annotation.Nullable;
7
 
8
 
8
 public class ObjectUtils {
9
 public class ObjectUtils {
14
         return obj == null ? defaultValue : action.run(obj);
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
     public static boolean notNull(Object o) {
22
     public static boolean notNull(Object o) {
18
         return o != null;
23
         return o != null;
19
     }
24
     }

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

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