Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,17 @@ public void setNativeId(@NonNull T view, @Nullable String nativeId) {

@ReactProp(name = ViewProps.ACCESSIBILITY_LABELLED_BY)
public void setAccessibilityLabelledBy(@NonNull T view, @Nullable Dynamic nativeId) {
if (nativeId.isNull()) {
if (nativeId == null || nativeId.isNull()) {
view.setTag(R.id.labelled_by, null);
return;
}
if (nativeId.getType() == ReadableType.String) {
view.setTag(R.id.labelled_by, nativeId.asString());
} else if (nativeId.getType() == ReadableType.Array) {
// On Android, this takes a single View as labeledBy. If an array is specified, set the first
// element in the tag.
view.setTag(R.id.labelled_by, nativeId.asArray().getString(0));
ReadableArray array = nativeId.asArray();
view.setTag(R.id.labelled_by, array.size() > 0 ? array.getString(0) : null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ package com.facebook.react.uimanager
import android.view.View.OnFocusChangeListener
import com.facebook.react.R
import com.facebook.react.bridge.BridgeReactContext
import com.facebook.react.bridge.DynamicFromObject
import com.facebook.react.bridge.JavaOnlyArray
import com.facebook.react.bridge.JavaOnlyMap
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
import com.facebook.react.views.view.ReactViewGroup
Expand Down Expand Up @@ -101,4 +103,31 @@ class BaseViewManagerTest {
verify(originalFocusListener, times(2)).onFocusChange(view, true)
Assertions.assertThat(originalFocusListener).isEqualTo(view.onFocusChangeListener)
}

@Test
fun testAccessibilityLabelledByString() {
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject("target_id"))
Assertions.assertThat(view.getTag(R.id.labelled_by)).isEqualTo("target_id")
}

@Test
fun testAccessibilityLabelledByArray() {
val array = JavaOnlyArray.of("first_id", "second_id")
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject(array))
Assertions.assertThat(view.getTag(R.id.labelled_by)).isEqualTo("first_id")
}

@Test
fun testAccessibilityLabelledByNullClearsTag() {
view.setTag(R.id.labelled_by, "stale_id")
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject(null))
Assertions.assertThat(view.getTag(R.id.labelled_by)).isNull()
}

@Test
fun testAccessibilityLabelledByEmptyArrayDoesNotCrash() {
view.setTag(R.id.labelled_by, "stale_id")
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject(JavaOnlyArray()))
Assertions.assertThat(view.getTag(R.id.labelled_by)).isNull()
}
}
Loading