import RX = require('reactxp'); export default class Form extends RX.Component { constructor(props) { super(props); } componentWillMount() { // set initial state of all the fields of the form let newState = {}; for (let field of this.props.fields) { if (field.hidden === "true") { newState[field.name] = "default-hidden"; } else if (field.type === "country") { newState[field.name] = this.props.countries[0]; } else if (field.type === "select") { newState[field.name] = field.options[0].value; } else { newState[field.name] = ""; } console.log(field); } this.setState(newState); console.log("this is the newstate", newState); } _onSubmit = () => { console.log(this.state) } _generatePicker = (field, index) => { return ( {field.label} this.setState({ [field.name]: value })} /> ); } _generateTextInput = (field, index) => { return ( {field.label} this.setState({ [field.name]: value })} /> ); } _generateForm = () => { let formFields = this.props.fields.map((field, index) => { if (field.hidden === "true") { return null; } else if (field.type === "country" || field.type === "select") { return this._generatePicker(field, index); } else if (field.type === "textarea" || field.type === "text") { return this._generateTextInput(field, index); } else { return null; } }); return formFields; } _generateButton() { return ( this._onSubmit()}> Submit ); } render(): JSX.Element { return ( {this._generateForm()} {this._generateButton()} ); } } const styles = { container: RX.Styles.createScrollViewStyle({ margin: 10, flex: 1 }), field: RX.Styles.createViewStyle({ marginTop: 10, marginBottom: 10 }), textInput: RX.Styles.createTextInputStyle({ borderColor: 'lightgrey', borderWidth: 2, paddingLeft: 10, paddingRight: 10, minHeight: 50, borderRadius: 10 }), textArea: RX.Styles.createTextInputStyle({ minHeight: 100 }), button: RX.Styles.createButtonStyle({ flex: 1, flexDirection: "row", padding: 10, minHeight: 50, borderRadius: 10, backgroundColor: 'lightgrey', }), buttonTitle: RX.Styles.createTextStyle({ justifyContent: "center", color: "white", fontSize: 18, fontWeight: "bold" }), label: RX.Styles.createTextStyle({ fontWeight: "bold", color: "black" }) }