import React from "react" import PropTypes from "prop-types" import { TextInput, Text, View, Platform } from "react-native" import { reactAutoBind } from "auto-bind2" export class BoundInput extends React.Component { static propTypes = { name: PropTypes.string.isRequired, message: PropTypes.string, label: PropTypes.string, binder: PropTypes.object.isRequired, password: PropTypes.bool, placeholder: PropTypes.string, lines: PropTypes.number, } static defaultProps = { lines: 1, } constructor(props) { super(props) reactAutoBind(this) const { name, binder } = this.props this.state = binder.getBindingState(name) } componentWillReceiveProps(nextProps) { if (nextProps.binder !== this.props.binder) { this.setState(nextProps.binder.getBindingState(nextProps.name)) } } handleChangeText(newText) { const { binder, name } = this.props if (binder) { this.setState(binder.updateBindingValue(name, newText)) } } render() { const { label, password, name, placeholder, message, lines } = this.props const { visible, disabled, readOnly, value, valid } = this.state if (!visible) { return null } return ( {label} 1} numberOfLines={lines} editable={!disabled && !readOnly} autoCapitalize="none" underlineColorAndroid="white" value={value} secureTextEntry={password} onChangeText={this.handleChangeText} placeholder={placeholder} /> {valid ? " " : message} ) } }