Skip to content

Commit 57e9380

Browse files
committed
supporting controlled and uncontrolled TextInputs
1 parent 0e77f01 commit 57e9380

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

Example/TextInputEffectsExample.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default class TextInputEffectsExample extends Component {
3434
<Text style={styles.title}>Kaede</Text>
3535
<Kaede
3636
label={'Website'}
37+
defaultValue={'Github'}
3738
/>
3839
<Kaede
3940
style={styles.input}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ You also need to install [react-native-vector-icons](https://github.com/oblador/
2121
|**`style`**|View Style Object|Applied to the root container of the input.|
2222
|**`labelStyle`**|View Style Object|Applied to the container of the label view.|
2323
|**`inputStyle`**|Text Style Object|Applied to the TextInput component.|
24+
|**`value`**|String|This value will be applied to the TextInput and change it's state on every render. Use this prop if you want a [Controlled Component](https://facebook.github.io/react/docs/forms.html#controlled-components).|
25+
|**`defaultValue`**|String|If you want to initialize the component with a non-empty value, you can supply a defaultValue prop. This prop creates an [Uncontrolled Component](https://facebook.github.io/react/docs/forms.html#uncontrolled-components) and is only used during initial render.|
2426

2527
You can also use [TextInput Props](https://facebook.github.io/react-native/docs/textinput.html#props). They'll be passed into TextInput component.
2628

@@ -35,7 +37,6 @@ This component needs `Icon` component from `react-native-vector-icons` to operat
3537
|**`iconClass`**|Object|The Icon component class you've imported from react-native-vector-icons.|
3638
|**`iconName`**|String|Name of the icon that is passed to Icon component.|
3739
|**`iconColor`**|String|Applied to the container of the label view.|
38-
|**`inputStyle`**|Text Style Object|Applied to the TextInput component.|
3940

4041

4142
## Example

lib/BaseInput.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class BaseInput extends Component {
1414
static propTypes = {
1515
label: PropTypes.string,
1616
value: PropTypes.string,
17+
defaultValue: PropTypes.string,
1718
style: View.propTypes.style,
1819
inputStyle: Text.propTypes.style,
1920
labelStyle: Text.propTypes.style,
@@ -37,15 +38,17 @@ export default class BaseInput extends Component {
3738
this._onFocus = this._onFocus.bind(this);
3839
this._focus = this._focus.bind(this);
3940

41+
const value = props.value || props.defaultValue;
42+
4043
this.state = {
41-
value: props.value,
42-
focusedAnim: new Animated.Value(props.value ? 1 : 0),
44+
value,
45+
focusedAnim: new Animated.Value(value ? 1 : 0),
4346
};
4447
}
4548

4649
componentWillReceiveProps(newProps) {
4750
const newValue = newProps.value;
48-
if (newValue !== this.state.value) {
51+
if (newProps.hasOwnProperty('value') && newValue !== this.state.value) {
4952
this.setState({
5053
value: newValue,
5154
});

0 commit comments

Comments
 (0)