Projekt

Obecné

Profil

Stáhnout (4.11 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 CategoryModalValues {
8
    name?: string;
9
    color?: string;
10
    description?: string;
11
    disabledForAnnotators?: boolean;
12
}
13

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

    
31
    if (props.defaultValues) {
32
        values = {
33
            name: props.defaultValues.name ?? '',
34
            color: props.defaultValues.color ?? 'black',
35
            description: props.defaultValues.description ?? '',
36
            disabledForAnnotators: props.defaultValues.disabledForAnnotators ?? false,
37
        };
38
    } else {
39
        values = {
40
            name: '',
41
            color: 'black',
42
            description: '',
43
            disabledForAnnotators: false,
44
        };
45
    }
46

    
47
    /**
48
     * The form.
49
     */
50
    const [form] = Form.useForm();
51

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

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

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

    
135
export default CategoryModal;
(2-2/4)