summaryrefslogtreecommitdiff
path: root/src/containers/WaybillForm.tsx
blob: 2167d178c6ef40ef96e1855a1da4ce1fc614d47e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React, { useState } from 'react';
import { Form, FormikProps } from 'formik';
import _ from 'lodash';
import Input from '../components/Input';
import Button from '../components/Button';
import Select from '../components/Select';
import Paper from '../components/Paper';
import useOptions from '../hooks/useOptions';

const WaybillForm: React.FC<FormikProps> = ({ setFieldValue, values }) => {
  const [recordsNumber, setRecordsNumber] = useState<number>(values.records.length);

  const handleAddRecord = () => setRecordsNumber(v => v + 1);

  const contractorOptions = useOptions('contractors', 'contractorId', values, setFieldValue);
  const productOptions = useOptions('products', 'productId', values, setFieldValue);

  return (
    <Form id="form">
      <Select
        name="contractorId"
        label="Контрагент"
        options={contractorOptions}
      />
      <Select
        name="operation"
        label="Операция"
        options={[
          { key: 'in', label: 'Приход' },
          { key: 'out', label: 'Расход' },
        ]}
      />
      <Button onClick={handleAddRecord}>+</Button>
      {_.times(recordsNumber).map(index => (
        <Paper>
          <Select
            name={`records.${index}.productId`}
            label="Товар"
            options={productOptions}
          />
          <Input
            name={`records.${index}.price`}
            type="number"
            label="Цена, $"
          />
          <Input
            name={`records.${index}.quantity`}
            type="number"
            label="Количество"
          />
          {values.records[index]?.price * values.records[index]?.quantity || 0}
        </Paper>
      ))}
    </Form>
  );
};

export default WaybillForm;