Projekt

Obecné

Profil

Stáhnout (2.85 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
    let values: SubTagModalValues;
28

    
29
    if (props.defaultValues) {
30
        values = {
31
            name: props.defaultValues.name ?? '',
32
            description: props.defaultValues.description ?? '',
33
        };
34
    } else {
35
        values = {
36
            name: '',
37
            description: '',
38
        };
39
    }
40

    
41
    /**
42
     * The form.
43
     */
44
    const [form] = Form.useForm();
45

    
46
    /**
47
     * Handles cancelling of the model window.
48
     */
49
    const handleCancel = () => {
50
        props.onCancel();
51
    };
52

    
53
    /**
54
     * Handles submitting of the model window.
55
     * @param values Form values.
56
     */
57
    const handleOk = (values: any) => {
58
        props.onSubmit({
59
            name: values.name,
60
            description: values.description,
61
        });
62
    };
63

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

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