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
|
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
|
93
|
form={form}
|
94
|
labelCol={{ span: 10 }}
|
95
|
wrapperCol={{ span: 14 }}
|
96
|
initialValues={values}
|
97
|
>
|
98
|
<Form.Item
|
99
|
label="Název"
|
100
|
name="name"
|
101
|
rules={[{ required: true, message: 'Prosím zadejte název' }]}
|
102
|
>
|
103
|
<Input />
|
104
|
</Form.Item>
|
105
|
<Form.Item
|
106
|
label="Barva"
|
107
|
name="color"
|
108
|
rules={[{ required: true, message: 'Prosím zadejte barvu' }]}
|
109
|
>
|
110
|
<Input type="color" />
|
111
|
</Form.Item>
|
112
|
<Form.Item label="Popis" name="description">
|
113
|
<Input.TextArea />
|
114
|
</Form.Item>
|
115
|
<Form.Item
|
116
|
label="Skrýt pro anotátory"
|
117
|
name="disabledForAnnotators"
|
118
|
valuePropName="checked"
|
119
|
rules={[
|
120
|
{
|
121
|
required: true,
|
122
|
message:
|
123
|
'Prosím zadejte zda má být kategorie viditelná pro anotátory',
|
124
|
},
|
125
|
]}
|
126
|
>
|
127
|
<Switch />
|
128
|
</Form.Item>
|
129
|
</Form>
|
130
|
</Modal>
|
131
|
);
|
132
|
}
|
133
|
|
134
|
export default CategoryModal;
|