TableForm.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import React, { PureComponent, Fragment } from 'react';
  2. import {
  3. Table,
  4. Button,
  5. Select,
  6. InputNumber,
  7. Popconfirm,
  8. Divider,
  9. Modal,
  10. message,
  11. } from 'antd';
  12. import Selector from '../../components/AXTableSelector/Selector';
  13. import { chargeUnitMap, durationMap, genAbsolutePicUrl, sortMap } from '../../utils/utils';
  14. import shopqr from '../../assets/shopqr.png';
  15. import styles from './TableForm.less';
  16. export default class TableForm extends PureComponent {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. data: props.value,
  21. loading: props.loading,
  22. currentKey: null,
  23. };
  24. }
  25. componentWillReceiveProps(nextProps) {
  26. if ('value' in nextProps && (!this.props.value || !this.props.value.length) ) {
  27. this.setState({
  28. data: nextProps.value,
  29. });
  30. }
  31. }
  32. getRowByKey(key, newData) {
  33. return (newData || this.state.data).filter(item => item.key === key)[0];
  34. }
  35. index = 0;
  36. cacheOriginData = {};
  37. toggleEditable=(e, key) => {
  38. e.preventDefault();
  39. const newData = this.state.data.map(item => ({ ...item }));
  40. const target = this.getRowByKey(key, newData);
  41. if (target) {
  42. // 进入编辑状态时保存原始数据
  43. if (!target.editable) {
  44. this.cacheOriginData[key] = { ...target };
  45. }
  46. target.editable = !target.editable;
  47. this.setState({ data: newData });
  48. }
  49. };
  50. chargeUnitSelectable=(value) => {
  51. const tmp = this.state.data.find(item => item.chargeUnit === value);
  52. return tmp !== undefined;
  53. };
  54. remove(record) {
  55. const { key, isNew } = record;
  56. const newData = this.state.data.filter(item => item.key !== key);
  57. this.setState({ data: newData });
  58. if (!isNew) {
  59. this.props.onDelete({ goodsId: key });
  60. }
  61. }
  62. newPrice = () => {
  63. const newData = this.state.data.map(item => ({ ...item }));
  64. newData.push({
  65. key: `NEW_TEMP_ID_${this.index}`,
  66. cpPrice: '',
  67. merchantPrice: '',
  68. terminalPrice: '',
  69. editable: true,
  70. isNew: true,
  71. });
  72. this.index += 1;
  73. this.setState({ data: newData });
  74. };
  75. handleFieldChange(value, fieldName, key) {
  76. const newData = this.state.data.map(item => ({ ...item }));
  77. const target = this.getRowByKey(key, newData);
  78. if (target) {
  79. target[fieldName] = value;
  80. this.setState({ data: newData });
  81. }
  82. }
  83. handleSelectChange(value, key) {
  84. const newData = this.state.data.map(item => ({ ...item }));
  85. const target = this.getRowByKey(key, newData);
  86. if (target) {
  87. target.chargeUnit = value;
  88. target.duration = durationMap[value];
  89. this.setState({ data: newData });
  90. }
  91. }
  92. handleQRSelectShow(key) {
  93. this.setState({
  94. currentKey: key,
  95. }, () => this.props.onQRShow());
  96. }
  97. handleQRSelectFinish(rows) {
  98. const newData = this.state.data.map(item => ({ ...item }));
  99. const target = this.getRowByKey(this.state.currentKey, newData);
  100. if (target) {
  101. target.shopQR = rows[0].path;
  102. this.setState({ data: newData });
  103. }
  104. this.props.onQRHide();
  105. }
  106. saveRow(e, key) {
  107. e.persist();
  108. if (this.clickedCancel) {
  109. this.clickedCancel = false;
  110. return;
  111. }
  112. const target = this.getRowByKey(key) || {};
  113. if (!target.chargeUnit || !target.cpPrice || !target.merchantPrice || !target.terminalPrice) {
  114. message.error('请填写完整价格信息');
  115. e.target.focus();
  116. return;
  117. }
  118. const { id, chargeUnit, cpPrice, merchantPrice, terminalPrice, duration, shopQR } = target;
  119. const sort = sortMap[chargeUnit];
  120. if (target.isNew) {
  121. this.props.onCreate({
  122. sort,
  123. cpPrice,
  124. shopQR,
  125. duration,
  126. chargeUnit,
  127. merchantPrice,
  128. terminalPrice,
  129. });
  130. } else {
  131. this.props.onUpdate({
  132. id,
  133. sort,
  134. shopQR,
  135. cpPrice,
  136. duration,
  137. chargeUnit,
  138. merchantPrice,
  139. terminalPrice,
  140. });
  141. }
  142. delete target.isNew;
  143. this.toggleEditable(e, key);
  144. }
  145. cancel(e, key) {
  146. this.clickedCancel = true;
  147. e.preventDefault();
  148. const newData = this.state.data.map(item => ({ ...item }));
  149. const target = this.getRowByKey(key, newData);
  150. if (this.cacheOriginData[key]) {
  151. Object.assign(target, this.cacheOriginData[key]);
  152. target.editable = false;
  153. delete this.cacheOriginData[key];
  154. }
  155. this.setState({ data: newData });
  156. this.clickedCancel = false;
  157. }
  158. render() {
  159. const columns = [{
  160. title: '计价单位',
  161. dataIndex: 'chargeUnit',
  162. key: 'chargeUnit',
  163. width: '13%',
  164. align: 'center',
  165. render: (text, record) => {
  166. if (record.editable) {
  167. return (
  168. <Select
  169. placeholder="请选择"
  170. style={{ width: '100%' }}
  171. value={text}
  172. onChange={value => this.handleSelectChange(value, record.key)}
  173. className={styles.select}
  174. >
  175. {
  176. Object.keys(chargeUnitMap).map(key => (
  177. <Select.Option
  178. key={key}
  179. value={key}
  180. disabled={this.chargeUnitSelectable(key)}
  181. >
  182. {chargeUnitMap[key]}
  183. </Select.Option>
  184. ))
  185. }
  186. </Select>
  187. );
  188. }
  189. return text;
  190. },
  191. }, {
  192. title: '时长(天)',
  193. dataIndex: 'duration',
  194. key: 'duration',
  195. width: '10%',
  196. align: 'center',
  197. }, {
  198. title: '二维码',
  199. dataIndex: 'shopQR',
  200. key: 'shopQR',
  201. width: '10%',
  202. align: 'center',
  203. render: (text, record) => {
  204. const { editable, key } = record;
  205. return (
  206. <div className={styles.qrWrapper}>
  207. {
  208. editable && (
  209. <div className={styles.qrBtn}>
  210. <a onClick={() => this.handleQRSelectShow(key)}>{!text ? '选择' : '更换'}</a>
  211. </div>
  212. )
  213. }
  214. <img src={!text ? shopqr : genAbsolutePicUrl(text)} alt="二维码" />
  215. </div>
  216. );
  217. },
  218. }, {
  219. title: '供应商售价(¥)',
  220. dataIndex: 'cpPrice',
  221. key: 'cpPrice',
  222. width: '18%',
  223. align: 'center',
  224. render: (text, record) => {
  225. if (record.editable) {
  226. return (
  227. <InputNumber
  228. min={1}
  229. value={text}
  230. onChange={e => this.handleFieldChange(e, 'cpPrice', record.key)}
  231. style={{ width: '100%' }}
  232. placeholder="请输入"
  233. />
  234. );
  235. }
  236. return text;
  237. },
  238. }, {
  239. title: '平台方售价(¥)',
  240. dataIndex: 'merchantPrice',
  241. key: 'merchantPrice',
  242. width: '18%',
  243. align: 'center',
  244. render: (text, record) => {
  245. if (record.editable) {
  246. return (
  247. <InputNumber
  248. min={1}
  249. value={text}
  250. onChange={value => this.handleFieldChange(value, 'merchantPrice', record.key)}
  251. style={{ width: '100%' }}
  252. placeholder="请输入"
  253. />
  254. );
  255. }
  256. return text;
  257. },
  258. }, {
  259. title: '终端显示价格(¥)',
  260. dataIndex: 'terminalPrice',
  261. key: 'terminalPrice',
  262. width: '18%',
  263. align: 'center',
  264. render: (text, record) => {
  265. if (record.editable) {
  266. return (
  267. <InputNumber
  268. min={1}
  269. value={text}
  270. onChange={value => this.handleFieldChange(value, 'terminalPrice', record.key)}
  271. style={{ width: '100%' }}
  272. placeholder="请输入"
  273. />
  274. );
  275. }
  276. return text;
  277. },
  278. }, {
  279. title: '操作',
  280. key: 'action',
  281. align: 'center',
  282. render: (text, record) => {
  283. if (!!record.editable && this.state.loading) {
  284. return null;
  285. }
  286. if (record.editable) {
  287. if (record.isNew) {
  288. return (
  289. <span>
  290. <a onClick={e => this.saveRow(e, record.key)}>添加</a>
  291. <Divider type="vertical" />
  292. <Popconfirm title="是否要删除此价格?" onConfirm={() => this.remove(record)}>
  293. <a>删除</a>
  294. </Popconfirm>
  295. </span>
  296. );
  297. }
  298. return (
  299. <span>
  300. <a onClick={e => this.saveRow(e, record.key)}>保存</a>
  301. <Divider type="vertical" />
  302. <a onClick={e => this.cancel(e, record.key)}>取消</a>
  303. </span>
  304. );
  305. }
  306. return (
  307. <span>
  308. <a onClick={e => this.toggleEditable(e, record.key)}>编辑</a>
  309. <Divider type="vertical" />
  310. <Popconfirm title="是否要删除此价格?" onConfirm={() => this.remove(record)}>
  311. <a>删除</a>
  312. </Popconfirm>
  313. </span>
  314. );
  315. },
  316. }];
  317. /* 二维码选择模态框 */
  318. const getResourceModal = () => {
  319. return (
  320. <Modal
  321. width={1100}
  322. footer={null}
  323. visible
  324. title="图片资源"
  325. maskClosable={false}
  326. onCancel={this.props.onQRHide}
  327. >
  328. <Selector
  329. multiple={false}
  330. loading={this.props.qrLoading}
  331. selectorName="PictureSingle"
  332. list={this.props.qrData.list}
  333. pageNo={this.props.qrData.pageNo}
  334. pageSize={this.props.qrData.pageSize}
  335. totalSize={this.props.qrData.totalSize}
  336. onCancel={this.props.onQRHide}
  337. onChange={this.props.onQRChange}
  338. onFinish={rows => this.handleQRSelectFinish(rows)}
  339. />
  340. </Modal>
  341. );
  342. };
  343. return (
  344. <Fragment>
  345. <Table
  346. bordered
  347. pagination={false}
  348. columns={columns}
  349. loading={this.state.loading}
  350. dataSource={this.state.data}
  351. rowClassName={() => {
  352. return styles.editable;
  353. }}
  354. />
  355. {!this.props.qrDestroy && getResourceModal()}
  356. <Button
  357. style={{ width: '100%', marginTop: 16, marginBottom: 8 }}
  358. type="dashed"
  359. onClick={this.newPrice}
  360. icon="plus"
  361. >
  362. 新增价格
  363. </Button>
  364. </Fragment>
  365. );
  366. }
  367. }