Commit 610a8d03 authored by beisir's avatar beisir

feat: 增加批量设置

parent f93bb626
import React from 'react';
import { Form, Select, Button } from 'antd';
import { cleanArray } from './utils';
import { getInput } from './libs';
const columnsRest = { style: { width: 135 } };
const formItemColumns = () => {
console.log('==========');
return [
{
title: '供货价',
key: 'supplyPrice',
placeholder: '请先选择二级规格',
inputType: 'number',
rest: columnsRest,
},
{
title: '市场价',
key: 'marketPrice',
placeholder: '请选择市场价',
inputType: 'number',
rest: columnsRest,
},
{
title: '重量(kg)',
key: 'weight',
inputType: 'number',
rest: columnsRest,
role: [1, 2],
},
{
title: '库存',
key: 'productStock',
inputType: 'number',
rest: columnsRest,
},
];
};
const formItemStyle = {
style: {
display: 'inline-block',
marginRight: 5,
marginBottom: 0,
},
};
const createSpecSelect = ({ specId, list, placeholder }) => (
<Select allowClear style={{ width: 120 }} placeholder={specId?.label || placeholder}>
{list.map(item => (
<Select.Option key={item} value={item}>
{item}
</Select.Option>
))}
</Select>
);
const createFormItems = (productType = 1, { getFieldDecorator }) => {
const formItems = formItemColumns()
.filter(item => !item.role || item?.role.includes(productType))
.map(item => (
<Form.Item {...formItemStyle}>
{getFieldDecorator(`batchItem[${item.key}]`, {})(getInput(item))}
</Form.Item>
));
return formItems;
};
export const BatchSettings = ({ form, settingTable }) => {
const { getFieldDecorator, getFieldsValue, getFieldValue } = form;
const values = getFieldsValue();
const { firstSpecId, secondSpecId, firstValues = [], secondValues = [] } = values;
const cleanFirstValues = cleanArray(firstValues);
const cleanSecondValues = cleanArray(secondValues);
const renderFormItem = createFormItems(2, { getFieldDecorator });
return (
<div>
<Form.Item {...formItemStyle}>
{getFieldDecorator('batchItem[batchFirst]', {})(
createSpecSelect({
list: cleanFirstValues,
specId: firstSpecId,
placeholder: '请先选择一级规格',
}),
)}
</Form.Item>
<Form.Item {...formItemStyle}>
{getFieldDecorator('batchItem[batchSecond]', {})(
createSpecSelect({
list: cleanSecondValues,
specId: secondSpecId,
placeholder: '请先选择二级规格',
}),
)}
</Form.Item>
{renderFormItem}
<Form.Item style={{ display: 'inline-block' }}>
<Button type="primary" onClick={() => settingTable(getFieldValue('batchItem'))}>
批量设置
</Button>
</Form.Item>
</div>
);
};
import React, { useState } from 'react'; import React, { useState } from 'react';
import { Table, Form, InputNumber, Input, Popconfirm, Button, notification } from 'antd'; import { Table, Form, InputNumber, Input, Popconfirm, Button, notification } from 'antd';
import { useEditProduct, dataInit } from './utils'; import { getInput } from './libs';
import { cleanArray } from './utils';
import { BatchSettings } from './BatchSettings';
const initData = { const initData = {
weight: null, weight: null,
productStockWarning: null, productStockWarning: null,
marketPrice: 'marketPrice', marketPrice: null,
// salePrice: '', supplyPrice: null,
supplyPrice: 'supplyPrice', productStock: null,
// stock: '', thirdSkuNo: null,
productStock: 'productStock', skuLink: null,
thirdSkuNo: 'thirdSkuNo',
skuLink: 'skuLink',
imageList: [], imageList: [],
}; };
const createProductData = ( const createSecondProduct = (secondValues, initItem, secondSpecId, dataSource, callback) => {
{ firstValues, secondValues, firstSpecId, secondSpecId }, secondValues.forEach(secondItem => {
productData, const specSecond = { ...initItem };
) => { if (callback) {
callback(specSecond);
}
specSecond.secondSpecId = secondSpecId.key;
specSecond.secondSpecValue = secondItem;
dataSource.push(specSecond);
});
};
const createProductData = ({ firstValues, secondValues, firstSpecId, secondSpecId }) => {
const countRowSpan = {}; const countRowSpan = {};
const dataSource = []; const dataSource = [];
if (firstValues.length) {
firstValues.forEach((fisrtItem, index) => { firstValues.forEach((fisrtItem, index) => {
const specFirst = Object.assign({}, initData); const specFirst = { ...initData };
specFirst.firstSpecId = firstSpecId.key; specFirst.firstSpecId = firstSpecId.key;
specFirst.firstSpecValue = fisrtItem; specFirst.firstSpecValue = fisrtItem;
if (secondValues.length) { if (secondValues.length) {
secondValues.forEach(secondItem => { createSecondProduct(secondValues, specFirst, secondSpecId, dataSource, specSecond => {
const specSecond = Object.assign({}, specFirst);
if (!countRowSpan[specFirst.firstSpecValue]) { if (!countRowSpan[specFirst.firstSpecValue]) {
countRowSpan[specFirst.firstSpecValue] = true; countRowSpan[specFirst.firstSpecValue] = true;
specSecond.rowSpanCount = secondValues.length; specSecond.rowSpanCount = secondValues.length;
} }
specSecond.secondSpecId = secondSpecId.key;
specSecond.secondSpecValue = secondItem;
dataSource.push(specSecond);
}); });
return; return;
} }
dataSource.push(specFirst); dataSource.push(specFirst);
}); });
} else if (secondValues.length) {
createSecondProduct(secondValues, initData, secondSpecId, dataSource);
} else {
dataSource.push(initData);
}
return dataSource; return dataSource;
}; };
...@@ -55,12 +65,6 @@ const isRepeatProduct = (list, message) => { ...@@ -55,12 +65,6 @@ const isRepeatProduct = (list, message) => {
const EditableContext = React.createContext(); const EditableContext = React.createContext();
const EditableCell = tableProps => { const EditableCell = tableProps => {
const getInput = () => {
if (tableProps.inputType === 'number') {
return <InputNumber />;
}
return <Input />;
};
const renderCell = form => { const renderCell = form => {
const { getFieldDecorator } = form; const { getFieldDecorator } = form;
const { const {
...@@ -71,43 +75,36 @@ const EditableCell = tableProps => { ...@@ -71,43 +75,36 @@ const EditableCell = tableProps => {
record, record,
index, index,
children, children,
rules, rules = [],
required, required,
...restProps ...restProps
} = tableProps; } = tableProps;
console.log(tableProps); console.log(record[dataIndex]);
return ( return (
<td {...restProps}> <td {...restProps}>
{editable ? (
<Form.Item style={{ margin: 0 }}> <Form.Item style={{ margin: 0 }}>
{getFieldDecorator(`tableArray[${index}][${dataIndex}]`, { {getFieldDecorator(`tableArray[${index}][${dataIndex}]`, {
rules: [ rules: [
{ {
required, required,
message: `请输入 ${title}!`, message: `请输入${title}!`,
}, },
...rules, ...rules,
], ],
initialValue: record[dataIndex], initialValue: record[dataIndex],
})(getInput())} })(getInput(tableProps))}
</Form.Item>
) : (
<Form.Item style={{ margin: 0 }}>
{getFieldDecorator(`tableArray[${index}][${dataIndex}]`, {
initialValue: record[dataIndex],
})(<span className="ant-form-text">{children}</span>)}
</Form.Item> </Form.Item>
)}
</td> </td>
); );
}; };
return <EditableContext.Consumer>{renderCell}</EditableContext.Consumer>; return <EditableContext.Consumer>{renderCell}</EditableContext.Consumer>;
}; };
const ProductInfoColumns = ({ firstSpecId, secondSpecId }, productData) => { const ProductInfoColumns = (
console.log(productData); { firstSpecId, secondSpecId, firstValues, secondValues },
return productData.length productData,
? [ ) => {
const productColData = [
{ {
title: firstSpecId?.label, title: firstSpecId?.label,
dataIndex: 'firstSpecValue', dataIndex: 'firstSpecValue',
...@@ -116,15 +113,18 @@ const ProductInfoColumns = ({ firstSpecId, secondSpecId }, productData) => { ...@@ -116,15 +113,18 @@ const ProductInfoColumns = ({ firstSpecId, secondSpecId }, productData) => {
render: (val, row) => ({ render: (val, row) => ({
children: val, children: val,
props: { props: {
rowSpan: row.rowSpanCount ? row.rowSpanCount : 0, // eslint-disable-next-line no-nested-ternary
rowSpan: secondValues?.length ? (row.rowSpanCount ? row.rowSpanCount : 0) : true,
}, },
}), }),
hidden: !firstValues?.length,
}, },
{ {
title: secondSpecId?.label, title: secondSpecId?.label,
dataIndex: 'secondSpecValue', dataIndex: 'secondSpecValue',
key: 'secondSpecValue', key: 'secondSpecValue',
editable: false, editable: false,
hidden: !secondValues?.length,
}, },
{ {
title: '供货价', title: '供货价',
...@@ -140,6 +140,15 @@ const ProductInfoColumns = ({ firstSpecId, secondSpecId }, productData) => { ...@@ -140,6 +140,15 @@ const ProductInfoColumns = ({ firstSpecId, secondSpecId }, productData) => {
key: 'marketPrice', key: 'marketPrice',
dataIndex: 'marketPrice', dataIndex: 'marketPrice',
editable: true, editable: true,
inputType: 'number',
required: true,
},
{
title: '重量(kg)',
key: 'weight',
dataIndex: 'weight',
editable: true,
inputType: 'number',
required: true, required: true,
}, },
{ {
...@@ -150,75 +159,118 @@ const ProductInfoColumns = ({ firstSpecId, secondSpecId }, productData) => { ...@@ -150,75 +159,118 @@ const ProductInfoColumns = ({ firstSpecId, secondSpecId }, productData) => {
inputType: 'number', inputType: 'number',
required: true, required: true,
}, },
{
title: '库存预警',
dataIndex: 'productStockWarning',
key: 'productStockWarning',
editable: true,
inputType: 'number',
required: true,
},
{ {
title: '商品自编码', title: '商品自编码',
dataIndex: 'thirdSkuNo', dataIndex: 'thirdSkuNo',
key: 'thirdSkuNo', key: 'thirdSkuNo',
editable: true, editable: true,
inputType: 'number', inputType: 'input',
required: true, required: true,
}, },
{ {
title: '京东链接', title: '京东链接',
dataIndex: 'skuLink', dataIndex: 'skuLink',
key: 'skuLink', key: 'skuLink',
inputType: 'input',
editable: true, editable: true,
}, },
] ];
: []; return productData.length ? productColData : [];
}; };
const BatchSettings = () => {}; const createProductColumns = (values, productData) => {
const columns = ProductInfoColumns(values, productData)
export const GenerateProductInfo = ({ form }) => { .map(col => {
const [productData, setProductData] = useState([]); console.log('=================>');
const components = {
body: {
cell: EditableCell,
},
};
const columns = ProductInfoColumns(form.getFieldsValue(), productData).map(col => {
console.log('============================================================');
return { return {
...col, ...col,
onCell: (record, index) => { onCell: (record, index) => ({
console.log(index);
return {
index, index,
record, record,
inputType: col.inputType,
editable: col.editable, editable: col.editable,
dataIndex: col.dataIndex, dataIndex: col.dataIndex,
title: col.title, title: col.title,
rules: col.rules, rules: col.rules,
required: col.required, required: col.required,
}),
}; };
})
.filter(col => !col.hidden);
return columns;
};
export const GenerateProductInfo = ({ form }) => {
const [productData, setProductData] = useState([]);
const [columns, setColumns] = useState([]);
const components = {
body: {
cell: EditableCell,
}, },
}; };
});
const handleAddProduct = () => { const handleAddProduct = () => {
const values = form.getFieldsValue(); const values = form.getFieldsValue();
const { firstValues = [], secondValues = [] } = values; const { firstValues = [], secondValues = [], firstSpecId, secondSpecId } = values;
const cleanFirstValues = cleanArray(firstValues); // 清楚空属性
const cleanSecondValues = cleanArray(secondValues);
// 校验是否有重复的一级规格 // 校验是否有重复的一级规格
const fisrtIsRepeat = isRepeatProduct(firstValues, '一级规格不可重复'); const fisrtIsRepeat = isRepeatProduct(cleanFirstValues, '一级规格不可重复');
const secondIsRepeat = isRepeatProduct(secondValues, '二级规格不可重复'); const secondIsRepeat = isRepeatProduct(cleanSecondValues, '二级规格不可重复');
if ([fisrtIsRepeat, secondIsRepeat].includes(true)) { if ([fisrtIsRepeat, secondIsRepeat].includes(true)) {
setProductData([]); setProductData([]);
return; return;
} }
const newData = createProductData(values, productData); const cleanValues = {
firstValues: cleanFirstValues,
secondValues: cleanSecondValues,
firstSpecId,
secondSpecId,
};
const newData = createProductData(cleanValues);
console.log(newData);
setProductData(newData); setProductData(newData);
form.setFieldsValue({
tableArray: newData,
});
const columnsData = createProductColumns(cleanValues, newData);
setColumns(columnsData);
};
const settingTable = batchItem => {
const productKey = Object.keys(batchItem);
console.log(productKey);
const newData = productData.map(item => {
productKey.forEach(key => {
item[key] = batchItem[key] || null;
});
return item;
});
console.log(newData);
setProductData(newData);
form.setFieldsValue({
tableArray: newData,
});
}; };
return ( return (
<EditableContext.Provider value={form}> <EditableContext.Provider value={form}>
<div style={{ textAlign: 'center' }}> <div style={{ textAlign: 'center' }}>
<Button onClick={handleAddProduct} type="primary" style={{ marginBottom: 16 }}> <Button onClick={() => handleAddProduct()} type="primary" style={{ marginBottom: 16 }}>
生成商品信息 生成商品信息
</Button> </Button>
{/* <BatchSettings /> */}
</div> </div>
<BatchSettings form={form} settingTable={settingTable} />
<Table <Table
rowKey="id" rowKey="id"
pagination={false} pagination={false}
......
...@@ -17,9 +17,11 @@ export const SelectSpecifications = ({ ...@@ -17,9 +17,11 @@ export const SelectSpecifications = ({
getFieldDecorator(keys, { initialValue: [] }); getFieldDecorator(keys, { initialValue: [] });
const remove = k => { const remove = k => {
const valueKey = getFieldValue(keys); const valueKey = getFieldValue(keys);
const removeItem = valueKey.filter(i => i !== k);
setFieldsValue({ setFieldsValue({
[keys]: valueKey.filter(i => i !== k), [keys]: removeItem,
}); });
setCountNumber(removeItem.length);
}; };
const add = () => { const add = () => {
...@@ -45,8 +47,8 @@ export const SelectSpecifications = ({ ...@@ -45,8 +47,8 @@ export const SelectSpecifications = ({
const formItemList = getFieldValue(keys); const formItemList = getFieldValue(keys);
const formItems = formItemList.map((k, index) => ( const formItems = formItemList.map((k, index) => (
<Col style={{ marginRight: 20 }} key={k}> <Col style={{ marginRight: 20 }} key={k}>
<Form.Item style={{ width: 200 }} required={false} key={k}> <Form.Item style={{ width: 200 }} required={false}>
{getFieldDecorator(`${formValue}[${k}]`, { {getFieldDecorator(`${formValue}[${index}]`, {
validateTrigger: ['onChange', 'onBlur'], validateTrigger: ['onChange', 'onBlur'],
rules: [{ required: true, whitespace: true, message: 'Please input passenger' }], rules: [{ required: true, whitespace: true, message: 'Please input passenger' }],
})(<Input placeholder="passenger name" style={{ width: 150, marginRight: 8 }} />)} })(<Input placeholder="passenger name" style={{ width: 150, marginRight: 8 }} />)}
......
import React from 'react'; import React from 'react';
import { Radio, Cascader } from 'antd'; import { Radio, Cascader, InputNumber, Input } from 'antd';
export const RadioComponent = ({ productTypeList }) => ( export const RadioComponent = ({ productTypeList }) => (
<Radio.Group> <Radio.Group>
...@@ -20,3 +20,22 @@ export const CascaderComponent = ({ categoryData }) => ( ...@@ -20,3 +20,22 @@ export const CascaderComponent = ({ categoryData }) => (
options={categoryData} options={categoryData}
/> />
); );
export const getInput = ({ inputType, title, children, rest = {} }) => {
let renderElement = null;
switch (inputType) {
case 'number':
renderElement = <InputNumber {...rest} placeholder={`请输入${title}`} />;
break;
case 'input':
renderElement = <Input {...rest} placeholder={`请输入${title}`} />;
break;
default:
renderElement = (
<span {...rest} className="ant-form-text">
{children}
</span>
);
}
return renderElement;
};
...@@ -53,3 +53,14 @@ export const dataInit = list => { ...@@ -53,3 +53,14 @@ export const dataInit = list => {
return finialList; return finialList;
}); });
}; };
export const cleanArray = actual => {
const newArray = [];
// eslint-disable-next-line no-plusplus
for (let i = 0; i < actual.length; i++) {
if (actual[i]) {
newArray.push(actual[i]);
}
}
return newArray;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment