UpdateForm.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import React, {useEffect, useState} from 'react';
  2. import {Button, Form, Input, message, Modal, Radio, Upload} from 'antd';
  3. import {TableListItem} from '../data.d';
  4. import {UploadOutlined} from '@ant-design/icons';
  5. // 表单特殊字段
  6. export interface FormValueType extends Partial<TableListItem> {
  7. }
  8. export interface UpdateFormProps {
  9. onCancel: (flag?: boolean, formVals?: FormValueType) => void;
  10. onSubmit: (values: FormValueType) => void;
  11. updateModalVisible: boolean;
  12. values: Partial<TableListItem>;
  13. }
  14. const FormItem = Form.Item;
  15. const formLayout = {
  16. labelCol: {span: 7},
  17. wrapperCol: {span: 13},
  18. };
  19. const UpdateForm: React.FC<UpdateFormProps> = (props) => {
  20. const [formVals, setFormVals] = useState<FormValueType>(props.values);
  21. const [form] = Form.useForm();
  22. const {
  23. onSubmit: handleUpdate,
  24. onCancel: handleUpdateModalVisible,
  25. updateModalVisible,
  26. values,
  27. } = props;
  28. const submit = async () => {
  29. const fieldsValue = await form.validateFields();
  30. const newValue = {...formVals, ...fieldsValue};
  31. setFormVals(newValue);
  32. handleUpdate(newValue);
  33. };
  34. const modelFileProps = {
  35. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  36. // @ts-ignore
  37. onChange(info) {
  38. if (info.file.status === 'done') {
  39. setFormVals({...formVals, ...{"modelFile": info.file.response.url}});
  40. message.success(`file uploaded successfully`);
  41. } else if (info.file.status === 'error') {
  42. message.error(`file upload failed.`);
  43. }
  44. }
  45. };
  46. const vocabFileProps = {
  47. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  48. // @ts-ignore
  49. onChange(info) {
  50. if (info.file.status === 'done') {
  51. setFormVals({...formVals, ...{"vocabFile": info.file.response.url}});
  52. message.success(`file uploaded successfully`);
  53. } else if (info.file.status === 'error') {
  54. message.error(`file upload failed.`);
  55. }
  56. }
  57. };
  58. const labelFileProps = {
  59. action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
  60. // @ts-ignore
  61. onChange(info) {
  62. if (info.file.status === 'done') {
  63. setFormVals({...formVals, ...{"labelFile": info.file.response.url}});
  64. message.success(`file uploaded successfully`);
  65. } else if (info.file.status === 'error') {
  66. message.error(`file upload failed.`);
  67. }
  68. }
  69. };
  70. const renderFooter = () => {
  71. return (
  72. <>
  73. <Button onClick={() => handleUpdateModalVisible(false, values)}>取消</Button>
  74. <Button type="primary" onClick={() => submit()}>
  75. 提交
  76. </Button>
  77. </>
  78. );
  79. };
  80. return (
  81. <Modal
  82. width={640}
  83. bodyStyle={{padding: '32px 40px 48px'}}
  84. destroyOnClose
  85. title="配置模型"
  86. visible={updateModalVisible}
  87. footer={renderFooter()}
  88. onCancel={() => handleUpdateModalVisible()}
  89. >
  90. <Form
  91. {...formLayout}
  92. form={form}
  93. initialValues={formVals}
  94. >
  95. <FormItem
  96. name="modelName"
  97. label="模型名称"
  98. rules={[{required: true, message: '请输入规则名称!'}]}
  99. >
  100. <Input placeholder="请输入"/>
  101. </FormItem>
  102. <FormItem
  103. name="modelType"
  104. label="模型类型"
  105. rules={[{required: true, message: '请选择模型类型!'}]}
  106. >
  107. <Radio.Group>
  108. <Radio.Button value="0">aubert</Radio.Button>
  109. <Radio.Button value="1">blstm</Radio.Button>
  110. </Radio.Group>
  111. </FormItem>
  112. <FormItem
  113. label="模型文件"
  114. >
  115. <Upload {...modelFileProps}>
  116. <Button>
  117. <UploadOutlined/> Upload
  118. </Button>
  119. </Upload>
  120. </FormItem>
  121. <FormItem
  122. label="词向量文件"
  123. >
  124. <Upload {...vocabFileProps}>
  125. <Button>
  126. <UploadOutlined/> Upload
  127. </Button>
  128. </Upload>
  129. </FormItem>
  130. <FormItem
  131. label="标签文件"
  132. >
  133. <Upload {...labelFileProps}>
  134. <Button>
  135. <UploadOutlined/> Upload
  136. </Button>
  137. </Upload>
  138. </FormItem>
  139. </Form>
  140. </Modal>
  141. );
  142. };
  143. export default UpdateForm;