Skip to content

Commit

Permalink
implementing iOS functionalities for facebook#30848 (comment)
Browse files Browse the repository at this point in the history
iOS has no standard pattern for the presentation of error states on text inputs, so I'd recommend we follow Android's pattern here. To do this, you'll need to implement a key press listener, and on change, detect if the field is now in an error state. If it is, you'll need to make a manual screen reader announcement of the error message, and append this message to the accessibilityValue for the field.
  • Loading branch information
fabOnReact committed May 3, 2022
1 parent ea9bb4c commit 7f7b281
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions Libraries/Components/TextInput/RCTTextInputViewConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const RCTTextInputViewConfig = {
allowFontScaling: true,
fontStyle: true,
textTransform: true,
screenreaderErrorAndroid: true,
textAlign: true,
fontFamily: true,
lineHeight: true,
Expand Down
1 change: 1 addition & 0 deletions Libraries/Text/TextInput/RCTBaseTextInputViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ @implementation RCTBaseTextInputViewManager
RCT_REMAP_VIEW_PROPERTY(placeholder, backedTextInputView.placeholder, NSString)
RCT_REMAP_VIEW_PROPERTY(placeholderTextColor, backedTextInputView.placeholderColor, UIColor)
RCT_REMAP_VIEW_PROPERTY(returnKeyType, backedTextInputView.returnKeyType, UIReturnKeyType)
RCT_REMAP_VIEW_PROPERTY(screenreaderErrorAndroid, reactAccessibilityElement.screenreaderErrorAndroid, NSString)
RCT_REMAP_VIEW_PROPERTY(selectionColor, backedTextInputView.tintColor, UIColor)
RCT_REMAP_VIEW_PROPERTY(spellCheck, backedTextInputView.spellCheckingType, UITextSpellCheckingType)
RCT_REMAP_VIEW_PROPERTY(caretHidden, backedTextInputView.caretHidden, BOOL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
_backedTextInputView.editable = newTextInputProps.traits.editable;
}

if (newTextInputProps.screenreaderErrorAndroid != oldTextInputProps.screenreaderErrorAndroid) {
self.accessibilityElement.accessibilityValue = RCTNSStringFromString(newTextInputProps.screenreaderErrorAndroid);
}

if (newTextInputProps.traits.enablesReturnKeyAutomatically !=
oldTextInputProps.traits.enablesReturnKeyAutomatically) {
_backedTextInputView.enablesReturnKeyAutomatically = newTextInputProps.traits.enablesReturnKeyAutomatically;
Expand Down
1 change: 1 addition & 0 deletions React/Views/UIView+React.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
@property (nonatomic, copy) NSArray<NSDictionary *> *accessibilityActions;
@property (nonatomic, copy) NSDictionary *accessibilityValueInternal;
@property (nonatomic, copy) NSString *accessibilityLanguage;
@property (nonatomic, copy) NSString *screenreaderErrorAndroid;

/**
* Used in debugging to get a description of the view hierarchy rooted at
Expand Down
5 changes: 5 additions & 0 deletions React/Views/UIView+React.m
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ - (NSString *)accessibilityLanguage
return objc_getAssociatedObject(self, _cmd);
}

- (NSString *)screenreaderErrorAndroid
{
return objc_getAssociatedObject(self, _cmd);
}

- (void)setAccessibilityLanguage:(NSString *)accessibilityLanguage
{
objc_setAssociatedObject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ static inline MapBuffer viewPropsDiff(
builder.putString(VP_ACCESSIBILITY_LABEL, newProps.accessibilityLabel);
}

if (oldProps.screenreaderErrorAndroid != newProps.screenreaderErrorAndroid) {
builder.putString(VP_ACCESSIBILITY_LABEL, newProps.screenreaderErrorAndroid);
}

if (oldProps.accessibilityLabelledBy != newProps.accessibilityLabelledBy) {
builder.putMapBuffer(
VP_ACCESSIBILITY_LABELLED_BY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ TextInputProps::TextInputProps(
"selection",
sourceProps.selection,
std::optional<Selection>())),
screenreaderErrorAndroid(convertRawProp(
context,
rawProps,
"screenreaderErrorAndroid",
sourceProps.screenreaderErrorAndroid,
"")),
inputAccessoryViewID(convertRawProp(
context,
rawProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class TextInputProps final : public ViewProps, public BaseTextProps {

std::string const inputAccessoryViewID{};

std::string screenreaderErrorAndroid{""};

bool onKeyPressSync{false};
bool onChangeSync{false};

Expand Down

0 comments on commit 7f7b281

Please sign in to comment.