import React from 'react'
import PropTypes from 'prop-types'
import { View, Text, TouchableHighlight } from 'react-native'
import autobind from 'autobind-decorator'
export class BoundButton extends React.Component {
static propTypes = {
name: PropTypes.string.isRequired,
title: PropTypes.string,
binder: PropTypes.object.isRequired,
submit: PropTypes.string,
onPress: PropTypes.func,
width: PropTypes.oneOfType([ PropTypes.string, PropTypes.number ]),
}
constructor(props) {
super(props)
let { name, binder } = this.props
binder.addListener(name, this.updateValue)
this.state = binder.getFieldState(name)
}
@autobind
updateValue(e) {
this.setState(e.state)
}
componentWillUnmount() {
this.props.binder.removeListener(this.props.name, this.updateValue)
}
componentWillReceiveProps(nextProps) {
if (nextProps.binder !== this.props.binder) {
this.props.binder.removeListener(this.props.name, this.updateValue)
nextProps.binder.addListener(nextProps.name, this.updateValue)
this.setState(nextProps.binder.getFieldState(nextProps.name))
}
}
render() {
const { name, title, submit, width, onPress } = this.props
const { visible, disabled } = this.state
if (!visible) {
return null
}
if (disabled) {
return (
{title}
)
} else {
return (
{title}
)
}
}
}