You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

addExperimentModal.tsx 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Form, Input, Modal, Select } from 'antd';
  2. import { useState } from 'react';
  3. import styles from './addExperimentModal.less';
  4. type FormData = {
  5. name?: string;
  6. description?: string;
  7. workflow_id?: string | number;
  8. };
  9. type AddExperimentModalProps = {
  10. isAdd: boolean;
  11. open: boolean;
  12. onCancel: () => void;
  13. onFinish: () => void;
  14. workflowList: Workflow[];
  15. initialValues: FormData;
  16. };
  17. interface GlobalParam {
  18. param_name: string;
  19. param_value: string;
  20. }
  21. interface Workflow {
  22. id: string | number;
  23. name: string;
  24. global_param?: GlobalParam[] | null;
  25. }
  26. function AddExperimentModal({
  27. isAdd,
  28. open,
  29. onCancel,
  30. onFinish,
  31. workflowList = [],
  32. initialValues = {},
  33. }: AddExperimentModalProps) {
  34. const dialogTitle = isAdd ? '新建实验' : '编辑实验';
  35. const workflowDisabled = isAdd ? false : true;
  36. const [globalParam, setGlobalParam] = useState<GlobalParam[]>([]);
  37. const [form] = Form.useForm();
  38. // 除了流水线选择发生变化
  39. const handleWorkflowChange = (id: string) => {
  40. const pipeline: Workflow | undefined = workflowList.find((v) => v.id === id);
  41. if (pipeline && pipeline.global_param) {
  42. setGlobalParam(pipeline.global_param);
  43. const fields = pipeline.global_param.reduce((acc, item) => {
  44. acc[item.param_name] = item.param_value;
  45. return acc;
  46. }, {} as Record<string, string>);
  47. form.setFieldsValue(fields);
  48. } else {
  49. setGlobalParam([]);
  50. }
  51. };
  52. return (
  53. <Modal
  54. className={styles.modal}
  55. title={
  56. <div className={styles.title}>
  57. <img className={styles.image} src={`/assets/images/pipeline-edit-icon.png`} alt="" />
  58. {dialogTitle}
  59. </div>
  60. }
  61. open={open}
  62. okButtonProps={{
  63. htmlType: 'submit',
  64. form: 'form',
  65. }}
  66. onCancel={onCancel}
  67. destroyOnClose={true}
  68. >
  69. <Form
  70. name="form"
  71. layout="vertical"
  72. initialValues={initialValues}
  73. onFinish={onFinish}
  74. autoComplete="off"
  75. form={form}
  76. >
  77. <Form.Item
  78. label="实验名称"
  79. name="name"
  80. rules={[{ required: true, message: '请输入实验名称' }]}
  81. >
  82. <Input placeholder="请输入实验名称" maxLength={64} showCount allowClear />
  83. </Form.Item>
  84. <Form.Item
  85. label="实验描述"
  86. name="description"
  87. rules={[{ required: true, message: '请输入实验描述' }]}
  88. >
  89. <Input placeholder="请输入实验描述" maxLength={128} showCount allowClear />
  90. </Form.Item>
  91. <Form.Item
  92. label="选择流水线"
  93. name="workflow_id"
  94. rules={[{ required: true, message: '请选择流水线' }]}
  95. >
  96. <Select
  97. disabled={workflowDisabled}
  98. placeholder="请选择流水线"
  99. onChange={handleWorkflowChange}
  100. >
  101. {Array.isArray(workflowList)
  102. ? workflowList.map((item) => {
  103. return (
  104. <Select.Option key={item.id} value={item.id}>
  105. {item.name}
  106. </Select.Option>
  107. );
  108. })
  109. : null}
  110. </Select>
  111. </Form.Item>
  112. {globalParam.map((item) => (
  113. <Form.Item label={item.param_name} name={item.param_name} key={item.param_name}>
  114. <Input />
  115. </Form.Item>
  116. ))}
  117. </Form>
  118. </Modal>
  119. );
  120. }
  121. export default AddExperimentModal;