[RN] Introduce initialValue prop to fix TextInputExamples
Summary: Some of the examples relied on the fact that TextInput wasn't a controlled component before. This introduces a new `initialValue` prop which behaves the way the `value` prop used to - that is, you could type without updating it and it wouldn't get reset, thus acting as just an initial value - and switches the examples to use it where appropriate.
This commit is contained in:
parent
49b55804b1
commit
cec5360f1b
|
@ -315,7 +315,7 @@ exports.examples = [
|
||||||
return (
|
return (
|
||||||
<View>
|
<View>
|
||||||
<WithLabel label="true">
|
<WithLabel label="true">
|
||||||
<TextInput password={true} style={styles.default} value="abc" />
|
<TextInput password={true} style={styles.default} defaultValue="abc" />
|
||||||
</WithLabel>
|
</WithLabel>
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
|
@ -332,11 +332,11 @@ exports.examples = [
|
||||||
<View>
|
<View>
|
||||||
<TextInput
|
<TextInput
|
||||||
style={[styles.default, {color: 'blue'}]}
|
style={[styles.default, {color: 'blue'}]}
|
||||||
value="Blue"
|
defaultValue="Blue"
|
||||||
/>
|
/>
|
||||||
<TextInput
|
<TextInput
|
||||||
style={[styles.default, {color: 'green'}]}
|
style={[styles.default, {color: 'green'}]}
|
||||||
value="Green"
|
defaultValue="Green"
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
|
@ -383,7 +383,7 @@ exports.examples = [
|
||||||
<WithLabel label="clearTextOnFocus">
|
<WithLabel label="clearTextOnFocus">
|
||||||
<TextInput
|
<TextInput
|
||||||
placeholder="text is cleared on focus"
|
placeholder="text is cleared on focus"
|
||||||
value="text is cleared on focus"
|
defaultValue="text is cleared on focus"
|
||||||
style={styles.default}
|
style={styles.default}
|
||||||
clearTextOnFocus={true}
|
clearTextOnFocus={true}
|
||||||
/>
|
/>
|
||||||
|
@ -391,7 +391,7 @@ exports.examples = [
|
||||||
<WithLabel label="selectTextOnFocus">
|
<WithLabel label="selectTextOnFocus">
|
||||||
<TextInput
|
<TextInput
|
||||||
placeholder="text is selected on focus"
|
placeholder="text is selected on focus"
|
||||||
value="text is selected on focus"
|
defaultValue="text is selected on focus"
|
||||||
style={styles.default}
|
style={styles.default}
|
||||||
selectTextOnFocus={true}
|
selectTextOnFocus={true}
|
||||||
/>
|
/>
|
||||||
|
|
|
@ -245,6 +245,12 @@ var TextInput = React.createClass({
|
||||||
* unwanted edits without flicker.
|
* unwanted edits without flicker.
|
||||||
*/
|
*/
|
||||||
value: PropTypes.string,
|
value: PropTypes.string,
|
||||||
|
/**
|
||||||
|
* Provides an initial value that will change when the user starts typing.
|
||||||
|
* Useful for simple use-cases where you don't want to deal with listening
|
||||||
|
* to events and updating the value prop to keep the controlled state in sync.
|
||||||
|
*/
|
||||||
|
defaultValue: PropTypes.string,
|
||||||
/**
|
/**
|
||||||
* When the clear button should appear on the right side of the text view
|
* When the clear button should appear on the right side of the text view
|
||||||
*/
|
*/
|
||||||
|
@ -348,12 +354,17 @@ var TextInput = React.createClass({
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
_getText: function(): ?string {
|
||||||
|
return typeof this.props.value === 'string' ?
|
||||||
|
this.props.value :
|
||||||
|
this.props.defaultValue;
|
||||||
|
},
|
||||||
|
|
||||||
_renderIOS: function() {
|
_renderIOS: function() {
|
||||||
var textContainer;
|
var textContainer;
|
||||||
|
|
||||||
var props = Object.assign({}, this.props);
|
var props = Object.assign({}, this.props);
|
||||||
props.style = [styles.input, this.props.style];
|
props.style = [styles.input, this.props.style];
|
||||||
|
|
||||||
if (!props.multiline) {
|
if (!props.multiline) {
|
||||||
for (var propKey in onlyMultiline) {
|
for (var propKey in onlyMultiline) {
|
||||||
if (props[propKey]) {
|
if (props[propKey]) {
|
||||||
|
@ -370,7 +381,7 @@ var TextInput = React.createClass({
|
||||||
onBlur={this._onBlur}
|
onBlur={this._onBlur}
|
||||||
onChange={this._onChange}
|
onChange={this._onChange}
|
||||||
onSelectionChangeShouldSetResponder={() => true}
|
onSelectionChangeShouldSetResponder={() => true}
|
||||||
text={this.props.value}
|
text={this._getText()}
|
||||||
mostRecentEventCount={this.state.mostRecentEventCount}
|
mostRecentEventCount={this.state.mostRecentEventCount}
|
||||||
/>;
|
/>;
|
||||||
} else {
|
} else {
|
||||||
|
@ -407,7 +418,7 @@ var TextInput = React.createClass({
|
||||||
onSelectionChange={this._onSelectionChange}
|
onSelectionChange={this._onSelectionChange}
|
||||||
onTextInput={this._onTextInput}
|
onTextInput={this._onTextInput}
|
||||||
onSelectionChangeShouldSetResponder={emptyFunction.thatReturnsTrue}
|
onSelectionChangeShouldSetResponder={emptyFunction.thatReturnsTrue}
|
||||||
text={this.props.value}
|
text={this._getText()}
|
||||||
/>;
|
/>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -457,7 +468,7 @@ var TextInput = React.createClass({
|
||||||
password={this.props.password || this.props.secureTextEntry}
|
password={this.props.password || this.props.secureTextEntry}
|
||||||
placeholder={this.props.placeholder}
|
placeholder={this.props.placeholder}
|
||||||
placeholderTextColor={this.props.placeholderTextColor}
|
placeholderTextColor={this.props.placeholderTextColor}
|
||||||
text={this.props.value}
|
text={this._getText()}
|
||||||
underlineColorAndroid={this.props.underlineColorAndroid}
|
underlineColorAndroid={this.props.underlineColorAndroid}
|
||||||
children={children}
|
children={children}
|
||||||
/>;
|
/>;
|
||||||
|
|
Loading…
Reference in New Issue