0
仍然是新手。Redux-Form使用「客戶端」表單進行故障同時創建和編輯
有人建議我可以使用客戶端的形式來創建和編輯。我後來對Create和Edit都使用了相同的表單,但是現在遇到的問題是,當我重新加載頁面時,我的表單字段沒有離開表單字段,因爲我在表單底部添加了與initialvalues的連接。
這是我如何設置的形式彌補終極版形式:
let ClientForm = reduxForm({
form: CLIENT_FORM_NAME
})(Client);
let ClientForm2 = connect(
state => ({
initialValues: state.editClient, // pull initial values from client reducer
enableReinitialize: true
}),
{ reducer } // bind client loading action creator
)(ClientForm);
export default ClientForm2;
狀態變化editClient更新果然表單字段與狀態培訓相關數據加載。精細。
但是,如果我這次重新加載表單作爲創建或「新客戶端」形式的原始詳細信息表單編輯客戶端仍然在字段中。
當狀態中的isEditMode設置爲false時,如何構造表單以不加載editClient詳細信息(避免加載)initialValues?
這是使用表格進行決鬥或更多目的的常見問題,並且是否存在簡單/優雅的修復?
爲了增加背景下,這裏是整個窗體..其相當長所以我在最後連接吧..
import React, { PropTypes } from "react";
import { Field, reduxForm, FormSection } from "redux-form";
import { connect } from "react-redux";
import { Col, Row } from "react-bootstrap";
import { Button, Glyphicon, Panel } from "react-bootstrap";
import Moment from "moment";
import Address from "../../address/addressContainer";
import FormField from "../../formComponents/formField";
import CheckboxField from "../../formComponents/checkboxField";
import TextField from "../../formComponents/textField";
import StaticText from "../../formComponents/staticText";
import TextAreaField from "../../formComponents/textAreaField";
import DateField from "../../formComponents/datefield";
import reducer from "../edit/reducer";
export const CLIENT_FORM_NAME = "Client";
const required = value => (value ? undefined : "Required");
const maxLength = max => value =>
value && value.length > max ? `Must be ${max} characters or less` : undefined;
const number = value =>
value && isNaN(Number(value)) ? "Must be a number" : undefined;
const minValue = min => value =>
value && value < min ? `Must be at least ${min}` : undefined;
const email = value =>
value && !/^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)
? "Invalid email address"
: undefined;
const tooOld = value =>
value && value > 65 ? "You might be too old for this" : undefined;
const aol = value =>
value && /[email protected]\.com/.test(value)
? "Really? You still use AOL for your email?"
: undefined;
const normalizeMobilePhone = value => {
if (!value) {
return value;
}
const onlyNums = value.replace(/[^\d]/g, "");
if (onlyNums.length <= 4) {
return onlyNums;
}
if (onlyNums.length <= 8) {
return `${onlyNums.slice(0, 4)} ${onlyNums.slice(4)}`;
}
return `${onlyNums.slice(0, 4)} ${onlyNums.slice(4, 7)} ${onlyNums.slice(
7,
10
)}`;
};
export const Client = props => {
const {
handleSubmit,
companyValue,
isWarrantyCompanyValue,
isEditMode
} = props;
const { reset } = props;
return (
<Row>
<Col md={12}>
<h2>
<Glyphicon glyph="edit" /> {isEditMode ? "Edit" : "New"} Client
</h2>
<hr />
<form onSubmit={handleSubmit} className="form-horizontal">
{isEditMode &&
<Panel header={<h3>Client - Basic Details</h3>}>
<Row>
<Field
component={StaticText}
name="clientNo"
id="clientNo"
label="Client No."
fieldCols={4}
labelCols={4}
controlCols={8}
/>
<Field
component={StaticText}
name="dateCreated"
id="dateCreated"
label="Date Created."
fieldCols={4}
labelCols={4}
controlCols={8}
/>
<Field
component={StaticText}
name="userName"
id="userName"
label="Created By."
fieldCols={4}
labelCols={4}
controlCols={8}
/>
</Row>
<Row>
<Field
component={props => {
return (
<StaticText {...props}>
<p className="form-control-static">
<Glyphicon
glyph={props.input.value ? "ok" : "remove"}
/>{" "}
{props.input.value
? "Has jobs attached"
: "No jobs attached"}
</p>
</StaticText>
);
}}
name="activity"
id="activity"
label="Activity"
fieldCols={4}
labelCols={4}
controlCols={8}
/>
<Field
component={CheckboxField}
name="active"
id="active"
label="De-Activate"
checkboxLabel="De activate this client"
fieldCols={4}
labelCols={4}
controlCols={8}
/>
</Row>
</Panel>}
<Panel header={<h3>Client - CompanyDetails</h3>}>
<Row>
<Field
component={CheckboxField}
id="company"
name="company"
label="Company?"
checkboxLabel="Client represents a company"
fieldCols={6}
labelCols={3}
controlCols={9}
/>
</Row>
{companyValue &&
<div>
<Row>
<Field
component={TextField}
name="companyName"
id="companyName"
type="text"
label="Company Name"
placeholder="Enter company name..."
fieldCols={6}
labelCols={3}
controlCols={9}
/>
<Field
component={TextField}
name="abn"
id="abn"
type="text"
label="ABN."
fieldCols={6}
labelCols={3}
controlCols={5}
/>
</Row>
<Row>
<Field
component={CheckboxField}
id="isWarrantyCompany"
name="isWarrantyCompany"
label="Warranty Company?"
checkboxLabel="Client represents a warranty company"
fieldCols={6}
labelCols={3}
controlCols={9}
/>
{isWarrantyCompanyValue &&
<Field
component={CheckboxField}
id="requiresPartsPayment"
name="requiresPartsPayment"
label="Requires Parts Payment?"
checkboxLabel="We pay for parts"
fieldCols={6}
labelCols={3}
controlCols={9}
/>}
</Row>
<Row>
<Field
component={TextField}
name="companyEmail"
id="companyEmail"
type="email"
label="Spare Parts Email."
placeholder="Enter spare parts email..."
fieldCols={6}
labelCols={3}
controlCols={9}
/>
</Row>
</div>}
</Panel>
<Panel
header={
<h3>
Client - {companyValue ? "Company Contact" : "Personal"} Details
</h3>
}
>
<Row>
<Field
component={TextField}
name="clientFirstName"
id="clientFirstName"
type="text"
label="First Name."
placeholder="Enter first name..."
fieldCols={6}
labelCols={3}
controlCols={9}
validate={[required]}
/>
<Field
component={TextField}
name="clientLastName"
id="clientLastName"
type="text"
label="Last Name."
placeholder="Enter last name..."
fieldCols={6}
labelCols={3}
controlCols={9}
/>
</Row>
<Row>
<Field
component={TextField}
name="mobilePhone"
id="mobilePhone"
type="text"
label="Mobile No."
placeholder="Enter mobile No..."
fieldCols={6}
labelCols={3}
controlCols={5}
normalize={normalizeMobilePhone}
/>
<Field
component={TextField}
name="phone"
id="phone"
type="text"
label="Phone No."
placeholder="Enter phone No..."
fieldCols={6}
labelCols={3}
controlCols={5}
/>
</Row>
<Row>
<Field
component={TextField}
name="email"
id="email"
type="email"
label="Email."
placeholder="Enter email address..."
fieldCols={6}
labelCols={3}
controlCols={9}
/>
</Row>
</Panel>
<FormSection name="Address">
<Address />
</FormSection>
<Panel header={<h3>Notes</h3>}>
<Row>
<Field
component={TextAreaField}
id="notes"
name="notes"
label="Notes."
placeholder="Enter notes here..."
fieldCols={12}
labelCols={1}
controlCols={11}
/>
</Row>
</Panel>
<Panel header={<h3>Client - Bank Details</h3>}>
<Row>
<Field
component={TextField}
name="bankName"
id="bankName"
type="text"
label="Bank Name."
placeholder="Enter bank name..."
fieldCols={4}
labelCols={4}
controlCols={8}
/>
<Field
component={TextField}
name="bsb"
id="bsb"
type="text"
label="BSB No."
placeholder="Enter BSB No..."
fieldCols={4}
labelCols={4}
controlCols={8}
/>
<Field
component={TextField}
name="account"
id="account"
type="text"
label="Account No."
placeholder="Enter Account No..."
fieldCols={4}
labelCols={4}
controlCols={8}
/>
</Row>
</Panel>
<div className="panel-body">
<Row>
<Col xs={4}>
<Row>
<Col xs={8} xsOffset={4}>
<Button bsStyle="primary" type="submit" bsSize="small">
<Glyphicon glyph="ok" /> Submit
</Button>{" "}
<Button type="reset" bsSize="small" onClick={reset}>
<Glyphicon glyph="ban-circle" /> Clear
</Button>
</Col>
</Row>
</Col>
</Row>
</div>
</form>
</Col>
</Row>
);
};
let ClientForm = reduxForm({
form: CLIENT_FORM_NAME
})(Client);
let ClientForm2 = connect(
state => ({
initialValues: state.editClient, // pull initial values from client reducer
enableReinitialize: true
}),
{ reducer } // bind client loading action creator
)(ClientForm);
export default ClientForm2;
認爲這將工作,然而「ownProps」提出了錯誤「client.jsx:385 Uncaught ReferenceError:ownProps未定義」我如何獲取它isEditMode ... – si2030
@ si2030你怎麼實際上使用' ClientForm2'?是不是像' '? –