Projekt

Obecné

Profil

Stáhnout (2.6 KB) Statistiky
| Větev: | Tag: | Revize:
1
import React from 'react';
2
import { Form, Input, Modal } from 'antd';
3

    
4
/**
5
 * All values that the modal window can show.
6
 */
7
export interface SubTagModalValues {
8
    name: string;
9
    description: string;
10
}
11

    
12
/**
13
 * Creates a modal window that works with properties of a sub tag.
14
 * @param props Contains arguments of the modal window.
15
 * @returns The modal window.
16
 */
17
function SubTagModal(props: {
18
    title: string; // Title of the modal
19
    submitText: string; // Text of submit button
20
    onCancel: () => void; // Method called when the modal is closed without submit
21
    onSubmit: (val: SubTagModalValues) => void; // Method called when the modal is submitted
22
    defaultValues?: SubTagModalValues; // Default values of a form that contains properties of the sub tag.
23
}) {
24
    /**
25
     * Initialize default values if they were not set in props.
26
     */
27
    const values = props.defaultValues ?? {
28
        name: '',
29
        description: '',
30
    };
31

    
32
    /**
33
     * The form.
34
     */
35
    const [form] = Form.useForm();
36

    
37
    /**
38
     * Handles cancelling of the model window.
39
     */
40
    const handleCancel = () => {
41
        props.onCancel();
42
    };
43

    
44
    /**
45
     * Handles submitting of the model window.
46
     * @param values Form values.
47
     */
48
    const handleOk = (values: any) => {
49
        props.onSubmit({
50
            name: values.name,
51
            description: values.description,
52
        });
53
    };
54

    
55
    return (
56
        <Modal
57
            title={props.title}
58
            visible={true}
59
            okText={props.submitText}
60
            cancelText="Zrušit"
61
            onCancel={handleCancel}
62
            onOk={() => {
63
                // Custom form validation when the modal is submitted.
64
                // Validates form and if all fields are valid it calls handleOk method.
65
                form.validateFields()
66
                    .then((values) => {
67
                        form.resetFields();
68
                        handleOk(values);
69
                    })
70
                    .catch((info) => {
71
                        console.log('Validate Failed:', info);
72
                    });
73
            }}
74
        >
75
            <Form form={form} labelCol={{ span: 4 }} initialValues={values}>
76
                <Form.Item
77
                    label="Název"
78
                    name="name"
79
                    rules={[{ required: true, message: 'Prosím zadejte název' }]}
80
                >
81
                    <Input />
82
                </Form.Item>
83
                <Form.Item label="Popis" name="description">
84
                    <Input.TextArea />
85
                </Form.Item>
86
            </Form>
87
        </Modal>
88
    );
89
}
90

    
91
export default SubTagModal;
(3-3/4)