Skip to content

Commit

Permalink
draft solution to add errorMessage with onChangeText
Browse files Browse the repository at this point in the history
  • Loading branch information
fabOnReact committed Mar 22, 2022
1 parent c5762a7 commit d3d54e1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ export const __INTERNAL_VIEW_CONFIG: PartialViewConfig = {
inlineImageLeft: true,
editable: true,
fontVariant: true,
android_errorMessage: true,
borderBottomRightRadius: true,
borderBottomColor: {process: require('../../StyleSheet/processColor')},
borderRadius: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class ViewProps {
public static final String BACKGROUND_COLOR = "backgroundColor";
public static final String FOREGROUND_COLOR = "foregroundColor";
public static final String COLOR = "color";
public static final String ERROR_MESSAGE = "errorMessage";
public static final String ANDROID_ERROR_MESSAGE = "android_errorMessage";
public static final String FONT_SIZE = "fontSize";
public static final String FONT_WEIGHT = "fontWeight";
public static final String FONT_STYLE = "fontStyle";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public class ReactEditText extends AppCompatEditText
private boolean mDetectScrollMovement = false;
private boolean mOnKeyPress = false;
private TextAttributes mTextAttributes;
private @Nullable String mErrorMessage;
private boolean mTypefaceDirty = false;
private @Nullable String mFontFamily = null;
private int mFontWeight = UNSET;
Expand Down Expand Up @@ -144,6 +145,7 @@ public ReactEditText(Context context) {
mKeyListener = new InternalKeyListener();
mScrollWatcher = null;
mTextAttributes = new TextAttributes();
mErrorMessage = null;

applyTextAttributes();

Expand Down Expand Up @@ -235,6 +237,11 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
return super.onKeyUp(keyCode, event);
}

public void setErrorMessage(String error) {
mErrorMessage = error;
setError(error);
}

@Override
protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) {
super.onScrollChanged(horiz, vert, oldHoriz, oldVert);
Expand Down Expand Up @@ -565,6 +572,9 @@ public void maybeSetText(ReactTextUpdate reactTextUpdate) {
// try to update state if the wrapper is available. Temporarily disable
// to prevent an infinite loop.
getText().replace(0, length(), spannableStringBuilder);
if (mErrorMessage != null && getError() != mErrorMessage) {
setError(mErrorMessage);
}
}
mDisableTextDiffing = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,9 @@ public void updateExtraData(ReactEditText view, Object extraData) {
}
}

@ReactProp(name = ViewProps.ERROR_MESSAGE)
public void setAndroidErrorMessage(ReactEditText view, String error) {
view.setError(error);
@ReactProp(name = ViewProps.ANDROID_ERROR_MESSAGE)
public void setErrorMessage(ReactEditText view, String error) {
view.setErrorMessage(error);
}

@ReactProp(name = ViewProps.FONT_SIZE, defaultFloat = ViewDefaults.FONT_SIZE_SP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ function ErrorExample(): React.Node {
Type error in the below TextInput to display an error message.
</Text>
<TextInput
errorMessage={error}
android_errorMessage={error}
onBlur={() => setError('onBlur')}
onEndEditing={() => setError('onEndEditing')}
onChangeText={newText => {
Expand Down

0 comments on commit d3d54e1

Please sign in to comment.