FormContent.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { Form } from 'antd';
  2. import React, { useState } from 'react';
  3. import { ArrowUpOutlined, ArrowDownOutlined, DeleteOutlined } from '@ant-design/icons';
  4. function FormContent(props) {
  5. const { list, onChange, onSelect } = props;
  6. const [currentItem, setCurrentItem] = useState(null);
  7. const handleDelete = index => {
  8. let _list = [...list];
  9. _list.splice(index, 1);
  10. onChange(_list);
  11. };
  12. const handleUp = index => {
  13. let _list = [...list];
  14. // 跟上一位置换位置
  15. const temp = _list[index - 1];
  16. _list[index - 1] = _list[index];
  17. _list[index] = temp;
  18. onChange(_list);
  19. };
  20. const handleDown = index => {
  21. let _list = [...list];
  22. const temp = _list[index + 1];
  23. _list[index + 1] = _list[index];
  24. _list[index] = temp;
  25. onChange(_list);
  26. };
  27. const handleSelect = index => {
  28. setCurrentItem(index);
  29. onSelect(index);
  30. };
  31. return (
  32. <div style={{ width: 300 }}>
  33. {list.map((item, index) => {
  34. const btns = (
  35. <>
  36. {index != 0 && (
  37. <ArrowUpOutlined
  38. style={{ marginLeft: 5, cursor: 'pointer' }}
  39. onClick={() => handleUp(index)}
  40. />
  41. )}
  42. {index != list.length - 1 && (
  43. <ArrowDownOutlined
  44. style={{ marginLeft: 5, cursor: 'pointer' }}
  45. onClick={() => handleDown(index)}
  46. />
  47. )}
  48. <DeleteOutlined
  49. style={{ marginLeft: 5, cursor: 'pointer' }}
  50. onClick={() => handleDelete(index)}
  51. />
  52. </>
  53. );
  54. return (
  55. <FormItem
  56. key={item.props?.id}
  57. active={index == currentItem}
  58. onClick={() => handleSelect(index)}
  59. item={item}
  60. btns={btns}
  61. />
  62. );
  63. })}
  64. </div>
  65. );
  66. }
  67. function FormItem(props) {
  68. const { item, btns, active, onClick } = props;
  69. const { label, placeholder, required } = item.props;
  70. return (
  71. <div
  72. style={{
  73. marginBottom: 20,
  74. padding: '4px 12px',
  75. border: '1px solid #666',
  76. borderLeft: active ? '10px solid #1890FF' : '1px solid #666',
  77. }}
  78. onClick={onClick}
  79. >
  80. <div style={{ fontSzie: 24, color: '#000', fontWeight: 'bold', position: 'relative' }}>
  81. {required && <i style={{ color: 'red' }}>*</i>}
  82. {label}
  83. <div style={{ position: 'absolute', right: 0, top: 0, padding: '5px 10px' }}>{btns}</div>
  84. </div>
  85. <div style={{ color: '#999', fontSize: 16 }}>{placeholder}</div>
  86. </div>
  87. );
  88. }
  89. export default FormContent;