ItemAttribute.js 13 KB

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