Projekt

Obecné

Profil

Stáhnout (3.89 KB) Statistiky
| Větev: | Tag: | Revize:
1
import React from 'react';
2
import { Form, Input, Modal, Switch } 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
    sentimentEnabled?: boolean;
12
}
13

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

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

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

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

    
71
    return (
72
        <Modal
73
            title={props.title}
74
            visible={true}
75
            okText={props.submitText}
76
            cancelText="Zrušit"
77
            maskClosable={false}
78
            onCancel={handleCancel}
79
            onOk={() => {
80
                // Custom form validation when the modal is submitted.
81
                // Validates form and if all fields are valid it calls handleOk method.
82
                form.validateFields()
83
                    .then((values) => {
84
                        form.resetFields();
85
                        handleOk(values);
86
                    })
87
                    .catch((info) => {
88
                        console.log('Validate Failed:', info);
89
                    });
90
            }}
91
        >
92
            <Form form={form} labelCol={{ span: 8 }} initialValues={values}>
93
                <Form.Item
94
                    label="Název"
95
                    name="name"
96
                    rules={[{ required: true, message: 'Prosím zadejte název' }]}
97
                >
98
                    <Input />
99
                </Form.Item>
100
                <Form.Item
101
                    label="Barva"
102
                    name="color"
103
                    rules={[{ required: true, message: 'Prosím zadejte barvu' }]}
104
                >
105
                    <Input type="color" />
106
                </Form.Item>
107
                <Form.Item
108
                    label="Povolit sentiment"
109
                    name="sentimentEnabled"
110
                    valuePropName="checked"
111
                    rules={[
112
                        {
113
                            required: true,
114
                            message: 'Prosím zadejte zda má být povolený sentiment',
115
                        },
116
                    ]}
117
                >
118
                    <Switch />
119
                </Form.Item>
120
                <Form.Item label="Popis" name="description">
121
                    <Input.TextArea />
122
                </Form.Item>
123
            </Form>
124
        </Modal>
125
    );
126
}
127

    
128
export default TagModal;
(10-10/11)