addDict.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React, {
  2. useState, useEffect, useContext
  3. } from 'react';
  4. import { Form, Input, Select, Button, Switch, TreeSelect, message, Space } from 'antd';
  5. import apiObj from '@api/index';
  6. import utils from '@utils/index'
  7. import DictContext from './Dict-context';
  8. import { useSelector } from 'react-redux'
  9. import Item from 'antd/lib/list/Item';
  10. const { post, api } = apiObj;
  11. const { TextArea } = Input;
  12. function AddDict(props) {
  13. const [form] = Form.useForm();
  14. const { type, formData} = useContext(DictContext);
  15. const staticInfo = useSelector(state => {
  16. return state.staticInfo;
  17. });
  18. const initialValues = formData;
  19. const onFinish = values => {
  20. values.status ? values.status=1:values.status=0
  21. let params = values
  22. if (type == 3) {
  23. params.id=initialValues.id
  24. editDict(params)
  25. } else {
  26. addDict(params)
  27. }
  28. };
  29. function addDict(param) {
  30. post(api.addDict, param).then((res) => {
  31. if (res.data.code === 200) {
  32. props.DictChange()
  33. message.success(res.data.message);
  34. } else {
  35. message.error(res.data.message);
  36. }
  37. })
  38. }
  39. function editDict(param) {
  40. post(api.updateDict, param).then((res) => {
  41. if (res.data.code === 200) {
  42. props.DictChange()
  43. message.success(res.data.message);
  44. } else {
  45. message.error(res.data.message);
  46. }
  47. })
  48. }
  49. function cancel() {
  50. props.cancel()
  51. }
  52. function onValuesChange() {
  53. props.isChange(form.isFieldsTouched())
  54. }
  55. return (
  56. <>
  57. <Form
  58. labelCol={{ span: 6 }}
  59. wrapperCol={{ span: 16 }}
  60. form={form}
  61. name="register"
  62. onFinish={onFinish}
  63. initialValues={initialValues}
  64. onValuesChange={onValuesChange}
  65. >
  66. <Form.Item
  67. name="groupType"
  68. label="代码类别"
  69. rules={[
  70. {
  71. required: true,
  72. message: '请输入代码类别',
  73. },
  74. ]}
  75. >
  76. {type == 3 ?
  77. <Input autoComplete='off'/>
  78. :
  79. <Input placeholder="请输入代码类别" autoComplete='off'/>
  80. }
  81. </Form.Item>
  82. <Form.Item
  83. name="val"
  84. label="字典编码"
  85. rules={[
  86. {
  87. required: true,
  88. message: '请输入字典编码',
  89. },
  90. ]}
  91. >
  92. {type == 3 ?
  93. <Input autoComplete='off'/>
  94. :
  95. <Input placeholder="请输入字典编码" autoComplete='off'/>
  96. }
  97. </Form.Item>
  98. <Form.Item
  99. name="name"
  100. label="代码名称"
  101. rules={[{ required: true, message: '请输入代码名称', whitespace: true }]}
  102. >
  103. {type == 3 ?
  104. <Input autoComplete='off'/>
  105. :
  106. <Input placeholder="请输入代码名称" autoComplete='off'/>
  107. }
  108. </Form.Item>
  109. <Form.Item
  110. name="remark"
  111. label="字典说明"
  112. rules={[
  113. {
  114. required: true,
  115. message: '请输入字典说明',
  116. },
  117. {
  118. max: 50,
  119. message: '密码不能大于50个字符',
  120. },
  121. ]}
  122. >
  123. {type == 3 ?
  124. <TextArea rows={4} />
  125. :
  126. <TextArea placeholder="50个字以内" rows={4} />
  127. }
  128. </Form.Item>
  129. <Form.Item
  130. name="status"
  131. valuePropName={initialValues.status=="禁用"?"":"checked"}
  132. label="当前状态"
  133. rules={[{ required: true, message: '请选择状态' }]}
  134. >
  135. <Switch/>
  136. </Form.Item>
  137. <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
  138. <Space size="middle">
  139. <Button htmlType="button" onClick={e => cancel()}>
  140. 取消
  141. </Button>
  142. <Button type="primary" htmlType="submit">
  143. 保存
  144. </Button>
  145. </Space>
  146. </Form.Item>
  147. </Form>
  148. </>
  149. );
  150. }
  151. export default AddDict;