UpdateForm.tsx 4.2 KB

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