Skip to content

Instantly share code, notes, and snippets.

@calendee
Last active September 22, 2018 12:59
Show Gist options
  • Select an option

  • Save calendee/05f3d708f534f348cceab68f25b72ce9 to your computer and use it in GitHub Desktop.

Select an option

Save calendee/05f3d708f534f348cceab68f25b72ce9 to your computer and use it in GitHub Desktop.
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