BasicForm.jsx 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React, { useState, useEffect } from 'react';
  2. import { Button, Checkbox, Card, Form, Input, notification } from 'antd';
  3. import { addPackage, updataPackage } from '@/services/package';
  4. import Money from '@/components/Money';
  5. export default (props) => {
  6. const { current, onChange } = props;
  7. const [form] = Form.useForm();
  8. const [loading, setLoading] = useState(false);
  9. const onFinish = (values) => {
  10. setLoading(true);
  11. const data = current.id ? { ...current, ...values } : values;
  12. const func = current.id ? updataPackage : addPackage;
  13. func(data, current.id).then(res => {
  14. setLoading(false);
  15. onChange(res);
  16. // notification.success({ message: '操作成功' })
  17. }).catch(() => {
  18. setLoading(false);
  19. });
  20. }
  21. useEffect(() => {
  22. form.setFieldsValue({
  23. name: current.name,
  24. unit: current.unit,
  25. price: current.price,
  26. });
  27. }, [form, current])
  28. return (
  29. <Card>
  30. <Form
  31. form={form}
  32. labelCol={{ span: 6 }}
  33. wrapperCol={{ span: 16 }}
  34. onFinish={onFinish}
  35. autoComplete="off"
  36. >
  37. <Form.Item
  38. label="套餐名称"
  39. name="name"
  40. rules={[{ required: true, message: '请输入套餐名称' }]}
  41. >
  42. <Input placeholder='请输入套餐名称' />
  43. </Form.Item>
  44. <Form.Item
  45. label="套餐单位"
  46. name="unit"
  47. >
  48. <Input placeholder='请输入套餐单位' />
  49. </Form.Item>
  50. <Form.Item
  51. label="价格"
  52. name="price"
  53. >
  54. <Money style={{ width: '100%' }} />
  55. </Form.Item>
  56. <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
  57. <Button type="primary" htmlType="submit" loading={loading}>
  58. 保存
  59. </Button>
  60. </Form.Item>
  61. </Form>
  62. </Card>
  63. )
  64. }