ItemAttribute.js 14 KB

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