@Form.create({
mapPropsToFields: (props) => {
const { formData } = props;
const keys = Object.keys(formData);
const newFormData = {};
for (let i = 0; i < keys.length; i++) {
newFormData[keys[i]] = Form.createFormField({
...formData[keys[i]],
value: formData[keys[i]] && formData[keys[i]].value,
});
}
return newFormData;
},
onFieldsChange(props, fields) {
let baseKey = ['deptcode', 'reportYear', 'reportMonth', 'deptname', 'deptPeopleNow', 'deptPeoplePre', 'certificatePeopleNow', 'certificatePeoplePre', 'premiumTotal', 'premiumPre', 'turnoverTotal', 'turnoverPre', 'profitNow', 'profitPre'];
const keys = Object.keys(fields);
let key = keys[0]; // 基本信息中的key / property / life
let newFields = {};
if (baseKey.includes(key)) { // 對於普通的formItem
props.onFormChange(fields);
} else { // 成組增長的formItem
let subFields = fields[key] || {};
let subFieldsKey = Object.keys(subFields)[0];
if (subFieldsKey) {
let arr = subFields[subFieldsKey] || [];
let realyFieldsKey = Object.keys(arr)[0];
if (!realyFieldsKey) return;
let value = arr[realyFieldsKey];
newFields = {
[value.name]: value,
};
props.onFormChange(newFields);
}
}
},
})
// 改變表單數據時觸發 onFormChange
onFormChange = (changedFields) => {
this.setState(({ formData }) => ({
formData: { ...formData, ...changedFields },
}));
}
// 經過mapPropsToFields將數據顯示在頁面上 接個返回的表單數據須要進行格式化
export function getFormatFormData(obj, options) {
const { dateFields, arrayFormItemFields, selectFields, objectFields, imgFields } = options;
if (!obj) {
return {};
}
let result = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (dateFields && dateFields.length > 0 && inArray(key, dateFields) !== -1) {
if (isArray(obj[key])) {
result[key] = {
value: obj[key].map(item => item && moment(item)),
};
} else {
result[key] = {
value: obj[key] && moment(obj[key]),
};
}
} else if (arrayFormItemFields && arrayFormItemFields.length > 0 && inArray(key, arrayFormItemFields) !== -1) {
(obj[key] || []).forEach((item, index) => {
for (let curKey in item) {
if (item.hasOwnProperty(curKey)) {
let newKey = key + curKey.substring(0, 1).toUpperCase() + curKey.substring(1, curKey.length) + 's[' + index + ']';
result[newKey] = {
value: item[curKey],
};
}
}
});
} else if (selectFields && selectFields.length > 0 && inArray(key, selectFields) !== -1) {
result[key] = {
value: (obj[key] !== null && obj[key] !== undefined && obj[key].toString()) || undefined,
};
} else if (objectFields && objectFields.length > 0 && inArray(key, objectFields) !== -1) {
for (let objKey in obj[key]) {
if (obj[key].hasOwnProperty(objKey)) {
result[objKey] = {
value: obj[key][objKey],
};
}
}
} else if (imgFields && imgFields.length > 0 && inArray(key, imgFields) !== -1) {
let fieldList = [];
(obj[key] || []).forEach((item, index) => {
if (item) {
let current = item.split('/');
let name = current[current.length - 1];
fieldList.push({
key: -1 - index,
uid: -1 - index,
name: name,
status: 'done',
url: item,
});
}
});
result[key] = { value: fieldList };
} else {
result[key] = { value: obj[key] };
}
}
}
return result;
}
複製代碼
經過modalVisible控制modal是否可見,以及modal的加載和銷燬.對於modal中的數據,能夠經過initialValue進行初始化bash
{modalVisible ? (<EditModal
formData={formData}
closeModal={this.closeModal}
modalType={modalType}
modalVisible={modalVisible}
handlePageSearch={this.handlePageSearch}
insuranceCityData={insuredCityList}
issuingTypeList={issuingTypeList}
licensedCityData={cities}
/>) : null}
<FormItem {...modalFormItemLayout} label="城市">
{
getFieldDecorator('cityId', {
initialValue: cityId && cityId + '',
rules: [{ required: true, message: '請選擇城市' }],
onChange: (value, options) => {
setFieldsValue({
'cityName': options.props.children,
});
},
})(<Select
allowClear
showSearch
placeholder="請選擇"
onChange={this.handleCityChange}
defaultActiveFirstOption={false}
optionFilterProp="children"
>
{
getSelectOptions([{ cityId: -1 , cityName: '所有' }], cityData, ['cityId', 'cityName'])
}
</Select>)
}
</FormItem>
複製代碼
/**
* 獲取Select中選擇項
* @param defOption
* @param otherOptions
* @param attributesRules
* @returns {Array|*|{inline, annotation}}
*/
function getSelectOptions(defOption = [], otherOptions = [], fields = ['id', 'name'], attributesRules = []) {
const options = defOption.concat(otherOptions);
return options.map((option) => {
const attributes = {};
if (attributesRules && attributesRules.length > 0) {
attributesRules.forEach((attribute) => {
const result = attribute.split('|');
if (result.length > 1) {
const valueFields = result[1].split('&');
attributes[result[0]] = valueFields.map(valueField => option[valueField]).join('');
}
});
}
return (
<Option key={option[fields[0]].toString()} {...attributes}>
{option[fields[1]]}
</Option>
);
});
}
複製代碼
經過getFieldDecorator進行裝飾時, 賦值爲number[${k}]
或number{k}
,他們對應的值分別爲number[k]和fields[number{k}
];ui
<FormItem>
{
getFieldDecorator(`number[${k}]`)(<Select
placeholder="請選擇"
defaultActiveFirstOption={false}
style={config.selectStyle}
disabled={!canEdit}
>
<Option value='0'>0-0</Option>
<Option value='1'>0-1</Option>
</Select>)
}
</FormItem>
複製代碼