Last active
September 22, 2018 12:59
-
-
Save calendee/05f3d708f534f348cceab68f25b72ce9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from "react"; | |
| import { connect } from "react-redux"; | |
| import { Field, FieldArray, reduxForm, arrayPush } from "redux-form"; | |
| import { customInput, customSelect, discounts } from "./fields"; | |
| import capitalize from "capitalize"; | |
| import { | |
| required, | |
| minLength, | |
| maxLength, | |
| matchesPassword, | |
| asyncValidate | |
| } from "../validation"; | |
| import "./RegisterForm.css"; | |
| class RegisterForm extends Component { | |
| componentWillMount() { | |
| this.props.initialize({ discountCodes: ["ABC200", "XYZ500"] }); | |
| } | |
| render() { | |
| const { handleSubmit } = this.props; | |
| const addFieldsExternally = () => { | |
| this.props.pushArray("register", "discountCodes", ""); | |
| }; | |
| return ( | |
| <form onSubmit={handleSubmit}> | |
| <Field | |
| name="firstname" | |
| component={customInput} | |
| type="text" | |
| label="First Name" | |
| validate={[required]} | |
| normalize={capitalize} | |
| /> | |
| <FieldArray name="discountCodes" component={discounts} /> | |
| <button type="submit">Submit</button> | |
| <button | |
| type="button" | |
| onClick={addFieldsExternally} | |
| style={{ marginTop: 20 }} | |
| > | |
| Add Discount Codes from External Source | |
| </button> | |
| </form> | |
| ); | |
| } | |
| } | |
| const mapDispatchToProps = { | |
| // NOTE: This MUST be aliased or it will not work | |
| pushArray: arrayPush | |
| }; | |
| RegisterForm = reduxForm({ | |
| form: "register", | |
| asyncValidate, | |
| asyncBlurFields: ["username"] | |
| })(RegisterForm); | |
| export default connect( | |
| null, | |
| mapDispatchToProps | |
| )(RegisterForm); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment