Projekt

Obecné

Profil

Stáhnout (3.22 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 TagModalValues {
8
    name?: string;
9
    color?: string;
10
    description?: string;
11
}
12

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

    
30
    if (props.defaultValues) {
31
        values = {
32
            name: props.defaultValues.name ?? '',
33
            color: props.defaultValues.color ?? 'black',
34
            description: props.defaultValues.description ?? '',
35
        };
36
    } else {
37
        values = {
38
            name: '',
39
            color: 'black',
40
            description: '',
41
        };
42
    }
43
    /**
44
     * The form.
45
     */
46
    const [form] = Form.useForm();
47

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

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

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

    
111
export default TagModal;
(4-4/4)