Projekt

Obecné

Profil

Stáhnout (3.89 KB) Statistiky
| Větev: | Tag: | Revize:
1 b49b834e Dominik Poch
import React from 'react';
2 0e4d7bd0 Dominik Poch
import { Form, Input, Modal, Switch } from 'antd';
3 b49b834e Dominik Poch
4
/**
5
 * All values that the modal window can show.
6
 */
7
export interface TagModalValues {
8 67683ef3 Dominik Poch
    name?: string;
9
    color?: string;
10
    description?: string;
11 0e4d7bd0 Dominik Poch
    sentimentEnabled?: boolean;
12 b49b834e Dominik Poch
}
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 67683ef3 Dominik Poch
    let values: TagModalValues;
30 b49b834e Dominik Poch
31 67683ef3 Dominik Poch
    if (props.defaultValues) {
32
        values = {
33
            name: props.defaultValues.name ?? '',
34
            color: props.defaultValues.color ?? 'black',
35
            description: props.defaultValues.description ?? '',
36 0e4d7bd0 Dominik Poch
            sentimentEnabled: props.defaultValues.sentimentEnabled ?? false,
37 67683ef3 Dominik Poch
        };
38
    } else {
39
        values = {
40
            name: '',
41
            color: 'black',
42
            description: '',
43 0e4d7bd0 Dominik Poch
            sentimentEnabled: false,
44 67683ef3 Dominik Poch
        };
45
    }
46 b49b834e Dominik Poch
    /**
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 0e4d7bd0 Dominik Poch
            sentimentEnabled: values.sentimentEnabled,
68 b49b834e Dominik Poch
        });
69
    };
70
71
    return (
72
        <Modal
73
            title={props.title}
74
            visible={true}
75
            okText={props.submitText}
76
            cancelText="Zrušit"
77 d1bca09c Dominik Poch
            maskClosable={false}
78 b49b834e Dominik Poch
            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 0e4d7bd0 Dominik Poch
            <Form form={form} labelCol={{ span: 8 }} initialValues={values}>
93 b49b834e Dominik Poch
                <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 0e4d7bd0 Dominik Poch
                <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 b49b834e Dominik Poch
                <Form.Item label="Popis" name="description">
121
                    <Input.TextArea />
122
                </Form.Item>
123
            </Form>
124
        </Modal>
125
    );
126
}
127
128
export default TagModal;