Skip to content

Commit

Permalink
Add accessibilityLabel option to button (#5847)
Browse files Browse the repository at this point in the history
* Add accessibilityLabel to tight and left buttons

* Set default accessibility label to back button

Co-authored-by: Yogev Ben David <yogevbd@wix.com>
  • Loading branch information
guyca and yogevbd committed Jan 14, 2020
1 parent d9673ad commit f635b5e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
Expand Up @@ -3,6 +3,7 @@
import com.reactnativenavigation.parse.params.Bool;
import com.reactnativenavigation.parse.params.Button;
import com.reactnativenavigation.parse.params.NullBool;
import com.reactnativenavigation.parse.params.Text;
import com.reactnativenavigation.parse.parsers.BoolParser;
import com.reactnativenavigation.parse.parsers.ColorParser;
import com.reactnativenavigation.parse.parsers.TextParser;
Expand All @@ -17,6 +18,7 @@ public static BackButton parse(JSONObject json) {

result.hasValue = true;
result.visible = BoolParser.parse(json, "visible");
result.accessibilityLabel = TextParser.parse(json, "accessibilityLabel", "Navigate Up");
if (json.has("icon")) result.icon = TextParser.parse(json.optJSONObject("icon"), "uri");
result.id = json.optString("id", Constants.BACK_BUTTON_ID);
result.enabled = BoolParser.parse(json, "enabled");
Expand All @@ -30,6 +32,7 @@ public static BackButton parse(JSONObject json) {

BackButton() {
id = Constants.BACK_BUTTON_ID;
accessibilityLabel = new Text("Navigate Up");
}

public Bool visible = new NullBool();
Expand All @@ -41,6 +44,7 @@ public boolean hasValue() {

public void mergeWith(BackButton other) {
if (!Constants.BACK_BUTTON_ID.equals(other.id)) id = other.id;
if (other.accessibilityLabel.hasValue()) accessibilityLabel = other.accessibilityLabel;
if (other.icon.hasValue()) icon = other.icon;
if (other.visible.hasValue()) visible = other.visible;
if (other.color.hasValue()) color = other.color;
Expand All @@ -52,6 +56,7 @@ public void mergeWith(BackButton other) {

void mergeWithDefault(final BackButton defaultOptions) {
if (Constants.BACK_BUTTON_ID.equals(id)) id = defaultOptions.id;
if (!accessibilityLabel.hasValue()) accessibilityLabel = defaultOptions.accessibilityLabel;
if (!icon.hasValue()) icon = defaultOptions.icon;
if (!visible.hasValue()) visible = defaultOptions.visible;
if (!color.hasValue()) color = defaultOptions.color;
Expand Down
@@ -1,7 +1,6 @@
package com.reactnativenavigation.parse.params;

import android.graphics.Typeface;
import androidx.annotation.Nullable;
import android.view.MenuItem;

import com.reactnativenavigation.parse.Component;
Expand All @@ -18,10 +17,13 @@
import java.util.ArrayList;
import java.util.Objects;

import androidx.annotation.Nullable;

public class Button {
public String instanceId = "btn" + CompatUtils.generateViewId();

@Nullable public String id;
public Text accessibilityLabel = new NullText();
public Text text = new NullText();
public Bool enabled = new NullBool();
public Bool disableIconTint = new NullBool();
Expand All @@ -37,6 +39,7 @@ public class Button {

public boolean equals(Button other) {
return Objects.equals(id, other.id) &&
accessibilityLabel.equals(other.accessibilityLabel) &&
text.equals(other.text) &&
enabled.equals(other.enabled) &&
disableIconTint.equals(other.disableIconTint) &&
Expand All @@ -54,6 +57,7 @@ public boolean equals(Button other) {
private static Button parseJson(JSONObject json, TypefaceLoader typefaceManager) {
Button button = new Button();
button.id = json.optString("id");
button.accessibilityLabel = TextParser.parse(json, "accessibilityLabel");
button.text = TextParser.parse(json, "text");
button.enabled = BoolParser.parse(json, "enabled");
button.disableIconTint = BoolParser.parse(json, "disableIconTint");
Expand Down Expand Up @@ -133,6 +137,7 @@ private static Number parseShowAsAction(JSONObject json) {

public void mergeWith(Button other) {
if (other.text.hasValue()) text = other.text;
if (other.accessibilityLabel.hasValue()) accessibilityLabel = other.accessibilityLabel;
if (other.enabled.hasValue()) enabled = other.enabled;
if (other.disableIconTint.hasValue()) disableIconTint = other.disableIconTint;
if (other.color.hasValue()) color = other.color;
Expand All @@ -150,6 +155,7 @@ public void mergeWith(Button other) {

public void mergeWithDefault(Button defaultOptions) {
if (!text.hasValue()) text = defaultOptions.text;
if (!accessibilityLabel.hasValue()) accessibilityLabel = defaultOptions.accessibilityLabel;
if (!enabled.hasValue()) enabled = defaultOptions.enabled;
if (!disableIconTint.hasValue()) disableIconTint = defaultOptions.disableIconTint;
if (!color.hasValue()) color = defaultOptions.color;
Expand Down
Expand Up @@ -2,12 +2,17 @@

import com.reactnativenavigation.parse.params.NullText;
import com.reactnativenavigation.parse.params.Text;
import com.reactnativenavigation.utils.ObjectUtils;

import org.json.JSONObject;

import javax.annotation.*;

public class TextParser {
public static Text parse(@Nullable JSONObject json, String text, String defaultValue) {
return ObjectUtils.take(parse(json, text), new Text(defaultValue));
}

public static Text parse(@Nullable JSONObject json, String text) {
return json != null && json.has(text) ? new Text(json.optString(text)) : new NullText();
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.reactnativenavigation.utils.Functions.Func1;
import com.reactnativenavigation.utils.Functions.FuncR1;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class ObjectUtils {
Expand All @@ -14,6 +15,10 @@ public static <T, S> S perform(@Nullable T obj, S defaultValue, FuncR1<T, S> act
return obj == null ? defaultValue : action.run(obj);
}

public static <T> T take(@Nullable T obj, @NonNull T defaultValue) {
return obj == null ? defaultValue : obj;
}

public static boolean notNull(Object o) {
return o != null;
}
Expand Down
Expand Up @@ -28,6 +28,7 @@
import androidx.annotation.RestrictTo;
import androidx.appcompat.widget.ActionMenuView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.MenuItemCompat;

public class TitleBarButtonController extends ViewController<TitleBarReactButtonView> implements MenuItem.OnMenuItemClickListener {
public interface OnClickListener {
Expand Down Expand Up @@ -105,6 +106,7 @@ public void applyNavigationIcon(Toolbar toolbar) {
toolbar.setNavigationOnClickListener(view -> onPressListener.onPress(button.id));
toolbar.setNavigationIcon(icon);
setLeftButtonTestId(toolbar);
if (button.accessibilityLabel.hasValue()) toolbar.setNavigationContentDescription(button.accessibilityLabel.get());
});
}

Expand All @@ -125,7 +127,9 @@ public void addToMenu(Toolbar toolbar, int position) {
menuItem.setOnMenuItemClickListener(this);
if (button.hasComponent()) {
menuItem.setActionView(getView());
if (button.accessibilityLabel.hasValue()) getView().setContentDescription(button.accessibilityLabel.get());
} else {
if (button.accessibilityLabel.hasValue()) MenuItemCompat.setContentDescription(menuItem, button.accessibilityLabel.get());
if (button.hasIcon()) {
loadIcon(new ImageLoadingListenerAdapter() {
@Override
Expand Down

0 comments on commit f635b5e

Please sign in to comment.