-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KeyboardFocusTextInput: text input wrapper for standardize TextInput …
…focusing behavior (#80) * feat: add text input wrapper * chore: update readme with example
- Loading branch information
1 parent
e383cc9
commit a2c7cde
Showing
59 changed files
with
941 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...m/reactnativea11y/RCA11yFocusWrapper.java → ...vea11y/components/RCA11yFocusWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...nativea11y/RCA11yFocusWrapperManager.java → ...components/RCA11yFocusWrapperManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
android/src/main/java/com/reactnativea11y/components/RCA11yTextInputWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.reactnativea11y.components; | ||
|
||
|
||
import android.content.Context; | ||
import android.graphics.Rect; | ||
import android.view.KeyEvent; | ||
import android.view.View; | ||
|
||
import com.facebook.react.views.textinput.ReactEditText; | ||
import com.facebook.react.views.view.ReactViewGroup; | ||
|
||
public class RCA11yTextInputWrapper extends ReactViewGroup { | ||
|
||
public static final byte FOCUS_BY_PRESS = 1; | ||
private ReactEditText reactEditText = null; | ||
|
||
private int focusType = 0; | ||
|
||
public RCA11yTextInputWrapper(Context context) { | ||
super(context); | ||
} | ||
|
||
public void setEditText(ReactEditText editText) { | ||
if (editText != null) { | ||
this.reactEditText = editText; | ||
} else { | ||
this.reactEditText.setOnFocusChangeListener(null); | ||
} | ||
} | ||
|
||
public void setFocusType(int focusType) { | ||
this.focusType = focusType; | ||
} | ||
|
||
public void setBlurType(int blurType) { | ||
// Stub, Android does not allow to type in EditField from another view. Even focus remains typing with soft or hard keyboard won't work | ||
} | ||
|
||
|
||
@Override | ||
public boolean onKeyDown(int keyCode, KeyEvent event) { | ||
if (focusType == FOCUS_BY_PRESS && keyCode == KeyEvent.KEYCODE_SPACE) { | ||
this.handleTextInputFocus(); | ||
} | ||
return super.onKeyDown(keyCode, event); | ||
} | ||
|
||
@Override | ||
public boolean requestFocus(int direction, Rect previouslyFocusedRect) { | ||
if ((direction == View.FOCUS_FORWARD || direction == View.FOCUS_BACKWARD) && focusType != FOCUS_BY_PRESS) { | ||
this.handleTextInputFocus(); | ||
return true; | ||
} | ||
|
||
return super.requestFocus(direction, previouslyFocusedRect); | ||
} | ||
|
||
private void handleTextInputFocus() { | ||
this.reactEditText.requestFocusFromJS(); | ||
this.setFocusable(false); | ||
|
||
this.reactEditText.setOnFocusChangeListener((textInput, hasTextEditFocus) -> { | ||
if (!hasTextEditFocus) { | ||
this.setFocusable(true); | ||
this.reactEditText.setFocusable(false); | ||
} | ||
}); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
android/src/main/java/com/reactnativea11y/components/RCA11yTextInputWrapperManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.reactnativea11y.components; | ||
|
||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.facebook.react.common.MapBuilder; | ||
import com.facebook.react.module.annotations.ReactModule; | ||
import com.facebook.react.uimanager.ThemedReactContext; | ||
import com.facebook.react.uimanager.UIManagerHelper; | ||
import com.facebook.react.uimanager.annotations.ReactProp; | ||
import com.facebook.react.views.textinput.ReactEditText; | ||
|
||
import com.facebook.react.views.view.ReactViewGroup; | ||
import com.reactnativea11y.events.FocusChangeEvent; | ||
|
||
import java.util.Map; | ||
|
||
@ReactModule(name = RCA11yTextInputWrapperManager.NAME) | ||
public class RCA11yTextInputWrapperManager extends com.reactnativea11y.RCA11yTextInputWrapperManagerSpec<RCA11yTextInputWrapper> { | ||
|
||
public static final String NAME = "RCA11yTextInputWrapper"; | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public RCA11yTextInputWrapper createViewInstance(ThemedReactContext context) { | ||
return subscribeOnHierarchy(new RCA11yTextInputWrapper(context)); | ||
} | ||
|
||
@Override | ||
protected void addEventEmitters(final ThemedReactContext reactContext, ReactViewGroup viewGroup) { | ||
viewGroup.setFocusable(true); | ||
viewGroup.setOnFocusChangeListener( | ||
(v, hasFocus) -> { | ||
FocusChangeEvent event = new FocusChangeEvent(viewGroup.getId(), hasFocus); | ||
UIManagerHelper.getEventDispatcherForReactTag(reactContext, v.getId()).dispatchEvent(event); | ||
}); | ||
} | ||
|
||
protected RCA11yTextInputWrapper subscribeOnHierarchy(RCA11yTextInputWrapper viewGroup) { | ||
viewGroup.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() { | ||
@Override | ||
public void onChildViewAdded(View parent, View child) { | ||
if (child instanceof ReactEditText) { | ||
viewGroup.setEditText((ReactEditText) child); | ||
} | ||
} | ||
|
||
@Override | ||
public void onChildViewRemoved(View parent, View child) { | ||
if (child instanceof ReactEditText) { | ||
viewGroup.setEditText(null); | ||
} | ||
} | ||
}); | ||
|
||
return viewGroup; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Map<String, Object> getExportedCustomDirectEventTypeConstants() { | ||
Map<String, Object> export = MapBuilder.<String, Object>builder().build(); | ||
if (export == null) { | ||
export = MapBuilder.newHashMap(); | ||
} | ||
|
||
export.put(FocusChangeEvent.EVENT_NAME, MapBuilder.of("registrationName", "onFocusChange")); | ||
|
||
return export; | ||
} | ||
|
||
@Override | ||
@ReactProp(name = "focusType") | ||
public void setFocusType(RCA11yTextInputWrapper view, int value) { | ||
view.setFocusType(value); | ||
} | ||
|
||
@Override | ||
@ReactProp(name = "blurType") | ||
public void setBlurType(RCA11yTextInputWrapper view, int value) { | ||
view.setBlurType(value); | ||
} | ||
|
||
|
||
@Override | ||
@ReactProp(name = "canBeFocused", defaultBoolean = true) | ||
public void setCanBeFocused(RCA11yTextInputWrapper view, boolean value) { | ||
view.setFocusable(value); | ||
} | ||
|
||
@Override | ||
public void onDropViewInstance(@NonNull ReactViewGroup viewGroup) { | ||
if (viewGroup instanceof RCA11yTextInputWrapper) { | ||
RCA11yTextInputWrapper wrapper = (RCA11yTextInputWrapper) viewGroup; | ||
wrapper.setEditText(null); | ||
wrapper.setOnFocusChangeListener(null); | ||
} | ||
super.onDropViewInstance(viewGroup); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
android/src/newarch/RCA11yTextInputWrapperManagerSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.reactnativea11y; | ||
|
||
import com.facebook.react.viewmanagers.RCA11yTextInputWrapperManagerInterface; | ||
import com.facebook.react.views.view.ReactViewGroup; | ||
import com.facebook.react.views.view.ReactViewManager; | ||
import com.facebook.soloader.SoLoader; | ||
import com.reactnativea11y.BuildConfig; | ||
|
||
public abstract class RCA11yTextInputWrapperManagerSpec<T extends ReactViewGroup> extends ReactViewManager implements RCA11yTextInputWrapperManagerInterface<T> { | ||
static { | ||
if (BuildConfig.CODEGEN_MODULE_REGISTRATION != null) { | ||
SoLoader.loadLibrary(BuildConfig.CODEGEN_MODULE_REGISTRATION); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
android/src/oldarch/RCA11yTextInputWrapperManagerSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.reactnativea11y; | ||
|
||
import com.facebook.react.views.view.ReactViewGroup; | ||
import com.facebook.react.views.view.ReactViewManager; | ||
import com.reactnativea11y.components.RCA11yTextInputWrapper; | ||
|
||
public abstract class RCA11yTextInputWrapperManagerSpec<T extends ReactViewGroup> extends ReactViewManager { | ||
public abstract void setCanBeFocused(T wrapper, boolean canBeFocused); | ||
|
||
public abstract void setFocusType(RCA11yTextInputWrapper view, int value); | ||
|
||
public abstract void setBlurType(RCA11yTextInputWrapper view, int value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
examples/A11yNewArch/android/app/src/main/java/com/a11ynewarch/textinput/RCTEditText.java
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
...s/A11yNewArch/android/app/src/main/java/com/a11ynewarch/textinput/RCTEditTextManager.java
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
...s/A11yNewArch/android/app/src/main/java/com/a11ynewarch/textinput/RCTEditTextPackage.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.