Permalink
Browse files

implement android maxlength prop

Summary: Fixes #3864
But I don't sure that this code is correct.

But it works and works same as in iOS.
Closes #3873

Reviewed By: mikearmstrong001

Differential Revision: D2626122

Pulled By: andreicoman11

fb-gh-sync-id: 316915c99b218ed5f32ca90fd72ce9810571383a
  • Loading branch information...
nucleartux authored and facebook-github-bot-2 committed Nov 6, 2015
1 parent 527d11c commit b6340ee2b0a96a89f9c39dcf6fdda518bf382b70
@@ -78,15 +78,25 @@ class RewriteExample extends React.Component {
this.state = {text: ''};
}
render() {
+ var limit = 20;
+ var remainder = limit - this.state.text.length;
+ var remainderColor = remainder > 5 ? 'blue' : 'red';
return (
- <TextInput
- onChangeText={(text) => {
- text = text.replace(/ /g, '_');
- this.setState({text});
- }}
- style={styles.singleLine}
- value={this.state.text}
- />
+ <View style={styles.rewriteContainer}>
+ <TextInput
+ multiline={false}
+ maxLength={limit}
+ onChangeText={(text) => {
+ text = text.replace(/ /g, '_');
+ this.setState({text});
+ }}
+ style={styles.default}
+ value={this.state.text}
+ />
+ <Text style={[styles.remainder, {color: remainderColor}]}>
+ {remainder}
+ </Text>
+ </View>
);
}
}
@@ -181,7 +181,6 @@ var TextInput = React.createClass({
/**
* Limits the maximum number of characters that can be entered. Use this
* instead of implementing the logic in JS to avoid flicker.
- * @platform ios
*/
maxLength: PropTypes.number,
/**
@@ -501,6 +500,7 @@ var TextInput = React.createClass({
mostRecentEventCount={this.state.mostRecentEventCount}
multiline={this.props.multiline}
numberOfLines={this.props.numberOfLines}
+ maxLength={this.props.maxLength}
onFocus={this._onFocus}
onBlur={this._onBlur}
onChange={this._onChange}
@@ -24,6 +24,7 @@
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
+import android.text.InputFilter;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.JSApplicationCausedNativeException;
@@ -53,6 +54,7 @@
private static final String KEYBOARD_TYPE_EMAIL_ADDRESS = "email-address";
private static final String KEYBOARD_TYPE_NUMERIC = "numeric";
+ private static final InputFilter[] EMPTY_FILTERS = new InputFilter[0];
@Override
public String getName() {
@@ -199,6 +201,17 @@ public void setNumLines(ReactEditText view, int numLines) {
view.setLines(numLines);
}
+ @ReactProp(name = "maxLength")
+ public void setMaxLength(ReactEditText view, @Nullable Integer maxLength) {
+ if (maxLength == null) {
+ view.setFilters(EMPTY_FILTERS);
+ } else {
+ InputFilter[] filterArray = new InputFilter[1];
+ filterArray[0] = new InputFilter.LengthFilter(maxLength);
+ view.setFilters(filterArray);
+ }
+ }
+
@ReactProp(name = "autoCorrect")
public void setAutoCorrect(ReactEditText view, @Nullable Boolean autoCorrect) {
// clear auto correct flags, set SUGGESTIONS or NO_SUGGESTIONS depending on value

1 comment on commit b6340ee

@niftylettuce

This comment has been minimized.

Show comment
Hide comment
@niftylettuce

niftylettuce Nov 6, 2015

Contributor

Awesome!

Contributor

niftylettuce commented on b6340ee Nov 6, 2015

Awesome!

Please sign in to comment.