ItemAttribute.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. import { Form, Button, Switch, Input, Radio, Space, Row } from 'antd';
  2. import React, { useMemo, useState, useEffect } from 'react';
  3. import { DeleteOutlined } from '@ant-design/icons';
  4. function ItemAttribute(props) {
  5. const { item, onChange, onRelClick } = props;
  6. if (!item) return null;
  7. const renderForm = () => {
  8. let FormContent;
  9. const formProps = {
  10. btns: (
  11. <Form.Item>
  12. <Button type="primary" htmlType="submit" style={{ marginRight: 20 }}>
  13. 保存
  14. </Button>
  15. </Form.Item>
  16. ),
  17. item,
  18. onFinish: (values) => {
  19. console.log(values);
  20. onChange?.(values);
  21. },
  22. };
  23. switch (item.componentName) {
  24. case 'InnerContactField':
  25. FormContent = <InnerContactField {...formProps} />;
  26. break;
  27. case 'DepartmentField':
  28. FormContent = <DepartmentField {...formProps} />;
  29. break;
  30. case 'ProjectField':
  31. FormContent = <ProjectField {...formProps} />;
  32. break;
  33. case 'ManufacturerField':
  34. FormContent = <ManufacturerField {...formProps} />;
  35. break;
  36. case 'TextField':
  37. FormContent = <TextField {...formProps} />;
  38. break;
  39. case 'TextareaField':
  40. FormContent = <TextareaField {...formProps} />;
  41. break;
  42. case 'DDSelectField':
  43. FormContent = <DDSelectField onRelClick={onRelClick} {...formProps} />;
  44. break;
  45. case 'DDMultiSelectField':
  46. FormContent = <DDMultiSelectField {...formProps} />;
  47. break;
  48. case 'NumberField':
  49. FormContent = <NumberField {...formProps} />;
  50. break;
  51. case 'TextNote':
  52. FormContent = <TextNote {...formProps} />;
  53. break;
  54. case 'DDAttachment':
  55. FormContent = <DDAttachment {...formProps} />;
  56. break;
  57. case 'DDDateField':
  58. FormContent = <DDDateField {...formProps} />;
  59. break;
  60. }
  61. return FormContent;
  62. };
  63. return (
  64. <div
  65. style={{
  66. // position: 'absolute',
  67. // top: 0,
  68. // right: 0,
  69. width: 500,
  70. height: 636,
  71. overflowY: 'auto',
  72. }}
  73. >
  74. {renderForm()}
  75. </div>
  76. );
  77. }
  78. export default ItemAttribute;
  79. function InnerContactField(props) {
  80. const { item, btns, onFinish } = props;
  81. const [form] = Form.useForm();
  82. const onSwitchChange = (checked) => {
  83. form.setFieldValue('choice', checked ? 1 : 0);
  84. };
  85. return (
  86. <Form
  87. form={form}
  88. labelCol={{ span: 4 }}
  89. wrapperCol={{ span: 20 }}
  90. autoComplete="off"
  91. initialValues={item.props}
  92. onFinish={onFinish}
  93. >
  94. <Form.Item label="标题" name="label">
  95. <Input />
  96. </Form.Item>
  97. <Form.Item label="提示文字" name="placeholder">
  98. <Input />
  99. </Form.Item>
  100. <Form.Item label="选项" name="choice">
  101. <Radio.Group>
  102. <Space direction="vertical">
  103. <Radio value={'0'}>只能选择一人</Radio>
  104. <Radio value={'1'}>可同时选择多人</Radio>
  105. </Space>
  106. </Radio.Group>
  107. </Form.Item>
  108. <Form.Item label="必填" name="required" valuePropName="checked">
  109. <Switch />
  110. </Form.Item>
  111. {btns}
  112. </Form>
  113. );
  114. }
  115. function DepartmentField(props) {
  116. const { item, btns, onFinish } = props;
  117. const [form] = Form.useForm();
  118. return (
  119. <Form
  120. form={form}
  121. labelCol={{ span: 4 }}
  122. wrapperCol={{ span: 20 }}
  123. autoComplete="off"
  124. initialValues={item.props}
  125. onFinish={onFinish}
  126. >
  127. <Form.Item label="标题" name="label">
  128. <Input />
  129. </Form.Item>
  130. <Form.Item label="提示文字" name="placeholder">
  131. <Input />
  132. </Form.Item>
  133. <Form.Item label="选项" name="choice">
  134. <Radio.Group>
  135. <Space direction="vertical">
  136. <Radio value={'0'}>只能选择一人</Radio>
  137. <Radio value={'1'}>可同时选择多人</Radio>
  138. </Space>
  139. </Radio.Group>
  140. </Form.Item>
  141. <Form.Item label="必填" name="required" valuePropName="checked">
  142. <Switch />
  143. </Form.Item>
  144. {btns}
  145. </Form>
  146. );
  147. }
  148. function ProjectField(props) {
  149. const { item, btns, onFinish } = props;
  150. const [form] = Form.useForm();
  151. return (
  152. <Form
  153. form={form}
  154. labelCol={{ span: 4 }}
  155. wrapperCol={{ span: 20 }}
  156. autoComplete="off"
  157. initialValues={item.props}
  158. onFinish={onFinish}
  159. >
  160. <Form.Item label="标题" name="label">
  161. <Input />
  162. </Form.Item>
  163. <Form.Item label="提示文字" name="placeholder">
  164. <Input />
  165. </Form.Item>
  166. <Form.Item label="必填" name="required" valuePropName="checked">
  167. <Switch />
  168. </Form.Item>
  169. {btns}
  170. </Form>
  171. );
  172. }
  173. function ManufacturerField(props) {
  174. const { item, btns, onFinish } = props;
  175. const [form] = Form.useForm();
  176. return (
  177. <Form
  178. form={form}
  179. labelCol={{ span: 4 }}
  180. wrapperCol={{ span: 20 }}
  181. autoComplete="off"
  182. initialValues={item.props}
  183. onFinish={onFinish}
  184. >
  185. <Form.Item label="标题" name="label">
  186. <Input />
  187. </Form.Item>
  188. <Form.Item label="提示文字" name="placeholder">
  189. <Input />
  190. </Form.Item>
  191. <Form.Item label="必填" name="required" valuePropName="checked">
  192. <Switch />
  193. </Form.Item>
  194. {btns}
  195. </Form>
  196. );
  197. }
  198. function TextField(props) {
  199. const { item, btns, onFinish } = props;
  200. const [form] = Form.useForm();
  201. const onReset = () => {
  202. form.resetFields();
  203. };
  204. return (
  205. <Form
  206. form={form}
  207. labelCol={{ span: 4 }}
  208. wrapperCol={{ span: 20 }}
  209. onFinish={onFinish}
  210. autoComplete="off"
  211. initialValues={item.props}
  212. >
  213. <Form.Item label="标题" name="label">
  214. <Input />
  215. </Form.Item>
  216. <Form.Item label="提示文字" name="placeholder">
  217. <Input />
  218. </Form.Item>
  219. <Form.Item label="必填" valuePropName="checked" name="required">
  220. <Switch />
  221. </Form.Item>
  222. {btns}
  223. </Form>
  224. );
  225. }
  226. function DDAttachment(props) {
  227. const { item, btns, onFinish } = props;
  228. const [form] = Form.useForm();
  229. return (
  230. <Form
  231. form={form}
  232. labelCol={{ span: 4 }}
  233. wrapperCol={{ span: 20 }}
  234. onFinish={onFinish}
  235. autoComplete="off"
  236. initialValues={item.props}
  237. >
  238. <Form.Item label="标题" name="label">
  239. <Input />
  240. </Form.Item>
  241. <Form.Item label="提示文字" name="placeholder">
  242. <Input />
  243. </Form.Item>
  244. <Form.Item label="必填" valuePropName="checked" name="required">
  245. <Switch />
  246. </Form.Item>
  247. {btns}
  248. </Form>
  249. );
  250. }
  251. function TextNote(props) {
  252. const { item, btns, onFinish } = props;
  253. const [form] = Form.useForm();
  254. return (
  255. <Form
  256. form={form}
  257. labelCol={{ span: 4 }}
  258. wrapperCol={{ span: 20 }}
  259. onFinish={onFinish}
  260. autoComplete="off"
  261. initialValues={item.props}
  262. >
  263. <Form.Item label="说明文字" name="placeholder">
  264. <Input />
  265. </Form.Item>
  266. {btns}
  267. </Form>
  268. );
  269. }
  270. function DDDateField(props) {
  271. const { item, btns, onFinish } = props;
  272. const [form] = Form.useForm();
  273. return (
  274. <Form
  275. form={form}
  276. labelCol={{ span: 4 }}
  277. wrapperCol={{ span: 20 }}
  278. onFinish={onFinish}
  279. autoComplete="off"
  280. initialValues={item.props}
  281. >
  282. <Form.Item label="标题" name="label">
  283. <Input />
  284. </Form.Item>
  285. <Form.Item label="提示文字" name="placeholder">
  286. <Input />
  287. </Form.Item>
  288. {btns}
  289. </Form>
  290. );
  291. }
  292. function TextareaField(props) {
  293. const { item, btns, onFinish } = props;
  294. const [form] = Form.useForm();
  295. return (
  296. <Form
  297. form={form}
  298. labelCol={{ span: 4 }}
  299. wrapperCol={{ span: 20 }}
  300. autoComplete="off"
  301. initialValues={item.props}
  302. onFinish={onFinish}
  303. >
  304. <Form.Item label="标题" name="label">
  305. <Input />
  306. </Form.Item>
  307. <Form.Item label="提示文字" name="placeholder">
  308. <Input />
  309. </Form.Item>
  310. <Form.Item label="必填" valuePropName="checked" name="required">
  311. <Switch />
  312. </Form.Item>
  313. {btns}
  314. </Form>
  315. );
  316. }
  317. function DDSelectField(props) {
  318. const { item, btns, onFinish, onRelClick } = props;
  319. const [form] = Form.useForm();
  320. const handleFinish = (value) => {
  321. let tempValue = { ...value };
  322. let arr = [];
  323. tempValue.options.map((item) => {
  324. arr.push(item.value);
  325. });
  326. if (arr) {
  327. arr = arr.filter((item) => item);
  328. arr = [...new Set(arr)];
  329. tempValue.options = arr;
  330. }
  331. onFinish?.(tempValue);
  332. };
  333. return (
  334. <Form
  335. form={form}
  336. labelCol={{ span: 4 }}
  337. wrapperCol={{ span: 20 }}
  338. autoComplete="off"
  339. initialValues={item.props}
  340. onFinish={handleFinish}
  341. >
  342. <Form.Item label="标题" name="label">
  343. <Input />
  344. </Form.Item>
  345. <Form.Item label="提示文字" name="placeholder">
  346. <Input />
  347. </Form.Item>
  348. <Form.Item label="选项" name="options" wrapperCol={{ span: 24 }}>
  349. <SelectItem onRelClick={onRelClick} />
  350. </Form.Item>
  351. <Form.Item label="必填" valuePropName="checked" name="required">
  352. <Switch />
  353. </Form.Item>
  354. {btns}
  355. </Form>
  356. );
  357. }
  358. function DDMultiSelectField(props) {
  359. const { item, btns, onFinish } = props;
  360. const [form] = Form.useForm();
  361. const handleFinish = (value) => {
  362. let tempValue = { ...value };
  363. let arr = [];
  364. tempValue.options.map((item) => {
  365. arr.push(item.value);
  366. });
  367. if (arr) {
  368. arr = arr.filter((item) => item);
  369. arr = [...new Set(arr)];
  370. tempValue.options = arr;
  371. }
  372. onFinish?.(tempValue);
  373. };
  374. return (
  375. <Form
  376. form={form}
  377. labelCol={{ span: 4 }}
  378. wrapperCol={{ span: 20 }}
  379. autoComplete="off"
  380. initialValues={item.props}
  381. onFinish={handleFinish}
  382. >
  383. <Form.Item label="标题" name="label">
  384. <Input />
  385. </Form.Item>
  386. <Form.Item label="提示文字" name="placeholder">
  387. <Input />
  388. </Form.Item>
  389. <Form.Item label="选项" name="options" wrapperCol={{ span: 24 }}>
  390. <SelectItem />
  391. </Form.Item>
  392. <Form.Item label="必填" valuePropName="checked" name="required">
  393. <Switch />
  394. </Form.Item>
  395. {btns}
  396. </Form>
  397. );
  398. }
  399. function SelectItem(props) {
  400. const { value, onChange, onRelClick } = props;
  401. const [localValue, setLocalValue] = useState([]);
  402. const pushItem = (item) => {
  403. let tempValue = [
  404. ...localValue,
  405. {
  406. id: item,
  407. value: item,
  408. },
  409. ];
  410. setLocalValue(tempValue);
  411. onChange(tempValue);
  412. };
  413. const handleDelete = (index) => {
  414. let tempValue = [...localValue];
  415. tempValue.splice(index, 1);
  416. setLocalValue(tempValue);
  417. onChange(tempValue);
  418. };
  419. const handleInputOnChange = (targetValue, index) => {
  420. let tempValue = [...localValue];
  421. tempValue[index].value = targetValue;
  422. setLocalValue(tempValue);
  423. onChange(tempValue);
  424. };
  425. useEffect(() => {
  426. let tempValue = value.map((item) => ({ id: item, value: item }));
  427. setLocalValue(tempValue);
  428. onChange(tempValue);
  429. }, []);
  430. return (
  431. <div
  432. style={{
  433. minHeight: 40,
  434. display: 'flex',
  435. flexDirection: 'column',
  436. }}
  437. >
  438. <Space>
  439. <div
  440. style={{
  441. fontSize: 4,
  442. color: '#40a9ff',
  443. cursor: 'pointer',
  444. lineHeight: '32px',
  445. }}
  446. onClick={() => {
  447. pushItem('');
  448. }}
  449. >
  450. 添加选项
  451. </div>
  452. {onRelClick && (
  453. <div
  454. style={{
  455. fontSize: 4,
  456. color: '#40a9ff',
  457. cursor: 'pointer',
  458. lineHeight: '32px',
  459. }}
  460. onClick={onRelClick}
  461. >
  462. 选项关联
  463. </div>
  464. )}
  465. </Space>
  466. <div style={{ minHeight: 20 }}>
  467. {localValue.map((item, index) => (
  468. <div
  469. style={{
  470. display: 'flex',
  471. justifyContent: 'center',
  472. alignItems: 'center',
  473. marginBottom: 5,
  474. }}
  475. key={item.value}
  476. >
  477. <Input
  478. style={{ marginRight: 10 }}
  479. value={item.value}
  480. onChange={(e) => handleInputOnChange(e.target.value, index)}
  481. />
  482. <DeleteOutlined onClick={() => handleDelete(index)} />
  483. </div>
  484. ))}
  485. </div>
  486. </div>
  487. );
  488. }
  489. function NumberField(props) {
  490. const { item, btns, onFinish } = props;
  491. const [form] = Form.useForm();
  492. return (
  493. <Form
  494. form={form}
  495. labelCol={{ span: 4 }}
  496. wrapperCol={{ span: 20 }}
  497. autoComplete="off"
  498. initialValues={item.props}
  499. onFinish={onFinish}
  500. >
  501. <Form.Item label="标题" name="label">
  502. <Input />
  503. </Form.Item>
  504. <Form.Item label="提示文字" name="placeholder">
  505. <Input />
  506. </Form.Item>
  507. <Form.Item label="单位" name="unit">
  508. <Input />
  509. </Form.Item>
  510. <Form.Item label="必填" valuePropName="checked" name="required">
  511. <Switch />
  512. </Form.Item>
  513. {btns}
  514. </Form>
  515. );
  516. }