TableRender.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { useMemo, useState } from 'react';
  2. import styles from './index.less';
  3. import { Input, Select } from 'antd';
  4. import { DeleteOutlined } from '@ant-design/icons';
  5. const TableRender = ({ value, onChange, onlyShow = false }) => {
  6. const defaultItem = { type: '', scale: '' };
  7. const options = ['UF', 'RO', 'NF', 'MF', 'MBR'];
  8. const list = useMemo(() => {
  9. return value ? JSON.parse(value) : [defaultItem];
  10. }, [value]);
  11. const handleChange = (idx, item) => {
  12. const newList = JSON.parse(JSON.stringify(list));
  13. newList[idx] = item;
  14. const result = JSON.stringify(newList);
  15. onChange(result);
  16. };
  17. const handleAddClick = () => {
  18. const result = JSON.stringify([...list, defaultItem]);
  19. onChange(result);
  20. };
  21. const handleDelClick = idx => {
  22. const newList = JSON.parse(JSON.stringify(list));
  23. newList.splice(idx, 1);
  24. const result = JSON.stringify(newList);
  25. onChange(result);
  26. };
  27. const renderItems = (item, idx) => {
  28. const { type, scale } = item;
  29. return (
  30. <div className={styles.item} key={`${idx}`}>
  31. <Select
  32. value={type}
  33. className={styles.itemLeft}
  34. style={{ width: '100%' }}
  35. onChange={e => handleChange(idx, { ...item, type: e })}
  36. >
  37. {options.map(value => (
  38. <Option key={value}>{value}</Option>
  39. ))}
  40. </Select>
  41. <Input
  42. value={scale}
  43. onChange={e => handleChange(idx, { ...item, scale: e.target.value })}
  44. />
  45. <DeleteOutlined className={styles.deleteIcon} onClick={() => handleDelClick(idx)} />
  46. </div>
  47. );
  48. };
  49. const renderOnlyShowItems = (item, idx) => {
  50. const { type, scale } = item;
  51. return (
  52. <div className={styles.item} key={`${type}_${scale}_${idx}`}>
  53. <span>{type}</span>
  54. <div className={styles.line}></div>
  55. <span>{scale}</span>
  56. </div>
  57. );
  58. };
  59. return (
  60. <div>
  61. {onlyShow ? (
  62. <div className={styles.content} style={{ width: '60%' }}>
  63. {list.map((item, idx) => renderOnlyShowItems(item, idx))}
  64. </div>
  65. ) : (
  66. <>
  67. <div className={styles.titleContent}>
  68. <div>工艺技术</div>
  69. <div>项目规模</div>
  70. </div>
  71. <div className={styles.content}>
  72. {list.map((item, idx) => renderItems(item, idx))}
  73. <div className={styles.addBtn} onClick={handleAddClick}>
  74. + 添加
  75. </div>
  76. </div>
  77. </>
  78. )}
  79. </div>
  80. );
  81. };
  82. export default TableRender;