index.jsx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { addStoreType, getStoreTypeById, updataStoreType } from '@/services/stock';
  2. import { PageContainer, ProForm, ProFormSelect, ProFormText } from '@ant-design/pro-components';
  3. import { useNavigate, useSearchParams } from 'react-router-dom';
  4. import { Card, Col, message, Row, Space } from 'antd';
  5. import { useEffect, useRef } from 'react';
  6. export default (props) => {
  7. const [searchParams, setSearchParams] = useSearchParams();
  8. const id = searchParams.get('id');
  9. const navigate = useNavigate();
  10. const formRef = useRef();
  11. useEffect(() => {
  12. if (id) {
  13. getStoreTypeById(id).then((res) => {
  14. formRef.current.setFieldsValue(res);
  15. });
  16. }
  17. }, [id]);
  18. const onFinish = async (values) => {
  19. console.log(values);
  20. if (id) {
  21. // 修改
  22. updataStoreType(id, { ...values }).then((res) => {
  23. // message.success('修改成功');
  24. navigate(-1);
  25. });
  26. } else {
  27. // 新增;
  28. addStoreType({ ...values }).then((res) => {
  29. // message.success('添加成功');
  30. navigate(-1);
  31. });
  32. }
  33. return false;
  34. };
  35. return (
  36. <PageContainer>
  37. <Card>
  38. <ProForm
  39. formRef={formRef}
  40. layout={'horizontal'}
  41. labelCol={{ span: 8 }}
  42. wrapperCol={{ span: 16 }}
  43. onFinish={onFinish}
  44. submitter={{
  45. searchConfig: {
  46. resetText: '返回',
  47. },
  48. onReset: () => navigate(-1),
  49. render: (props, doms) => {
  50. return (
  51. <Row>
  52. <Col span={8} offset={8}>
  53. <Space>{doms}</Space>
  54. </Col>
  55. </Row>
  56. );
  57. },
  58. }}
  59. >
  60. <ProFormText
  61. name="name"
  62. label="分类名称"
  63. placeholder="请输入名称"
  64. width={460}
  65. allowClear={false}
  66. rules={[{ required: true, message: '请输入名称' }]}
  67. />
  68. <ProFormSelect
  69. name="isFood"
  70. label="是否食材"
  71. options={[
  72. { label: '是', value: 1 },
  73. { label: '否', value: 0 },
  74. ]}
  75. placeholder="请选择"
  76. width={460}
  77. allowClear={false}
  78. rules={[{ required: true, message: '请选择是否食材' }]}
  79. />
  80. <ProFormSelect
  81. name="isDevice"
  82. label="是否设备"
  83. options={[
  84. { label: '是', value: 1 },
  85. { label: '否', value: 0 },
  86. ]}
  87. placeholder="请选择"
  88. width={460}
  89. allowClear={false}
  90. rules={[{ required: true, message: '请选择是否设备' }]}
  91. />
  92. </ProForm>
  93. </Card>
  94. </PageContainer>
  95. );
  96. };