model-params.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { SupportModelParams } from '@/types/model';
  2. import { Checkbox, Form, FormInstance, Input, InputNumber } from 'antd';
  3. import { useEffect } from 'react';
  4. interface ParamValues {
  5. [key: string]: string | number | boolean;
  6. }
  7. function ModelParams({ params, form }: { params: Array<SupportModelParams> | null; form: FormInstance<any> }) {
  8. useEffect(() => {
  9. if (params) {
  10. const initialValues: ParamValues = {};
  11. params.forEach(param => {
  12. initialValues[param.param_name] = param.default_value;
  13. });
  14. form.setFieldsValue(initialValues); // 设置表单字段的初始值
  15. }
  16. }, [params, form]);
  17. if (!params || params?.length < 1) {
  18. return null;
  19. }
  20. function renderItem(param: SupportModelParams) {
  21. switch (param.param_type) {
  22. case 'str':
  23. return <Input />;
  24. case 'int':
  25. return <InputNumber />;
  26. case 'bool':
  27. return <Checkbox />;
  28. }
  29. }
  30. return (
  31. <>
  32. {params?.map((param: SupportModelParams) => (
  33. <Form.Item
  34. key={param.param_name}
  35. label={
  36. <p className='whitespace-normal overflow-wrap-break-word'>
  37. {param.description?.length > 20 ? param.param_name : param.description}
  38. </p>
  39. }
  40. name={param.param_name}
  41. initialValue={param.default_value}
  42. valuePropName={param.param_type === 'bool' ? 'checked' : 'value'}
  43. tooltip={param.description}
  44. rules={[{ required: param.required, message: `Please input ${param.description}` }]}
  45. >
  46. {renderItem(param)}
  47. </Form.Item>
  48. ))}
  49. </>
  50. );
  51. }
  52. export default ModelParams;