1 |
b49b834e
|
Dominik Poch
|
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 |
67683ef3
|
Dominik Poch
|
name?: string;
|
9 |
|
|
color?: string;
|
10 |
|
|
description?: string;
|
11 |
b49b834e
|
Dominik Poch
|
}
|
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 |
67683ef3
|
Dominik Poch
|
let values: TagModalValues;
|
29 |
b49b834e
|
Dominik Poch
|
|
30 |
67683ef3
|
Dominik Poch
|
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 |
b49b834e
|
Dominik Poch
|
/**
|
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 |
|
|
onCancel={handleCancel}
|
74 |
|
|
onOk={() => {
|
75 |
|
|
// Custom form validation when the modal is submitted.
|
76 |
|
|
// Validates form and if all fields are valid it calls handleOk method.
|
77 |
|
|
form.validateFields()
|
78 |
|
|
.then((values) => {
|
79 |
|
|
form.resetFields();
|
80 |
|
|
handleOk(values);
|
81 |
|
|
})
|
82 |
|
|
.catch((info) => {
|
83 |
|
|
console.log('Validate Failed:', info);
|
84 |
|
|
});
|
85 |
|
|
}}
|
86 |
|
|
>
|
87 |
|
|
<Form form={form} labelCol={{ span: 4 }} initialValues={values}>
|
88 |
|
|
<Form.Item
|
89 |
|
|
label="Název"
|
90 |
|
|
name="name"
|
91 |
|
|
rules={[{ required: true, message: 'Prosím zadejte název' }]}
|
92 |
|
|
>
|
93 |
|
|
<Input />
|
94 |
|
|
</Form.Item>
|
95 |
|
|
<Form.Item
|
96 |
|
|
label="Barva"
|
97 |
|
|
name="color"
|
98 |
|
|
rules={[{ required: true, message: 'Prosím zadejte barvu' }]}
|
99 |
|
|
>
|
100 |
|
|
<Input type="color" />
|
101 |
|
|
</Form.Item>
|
102 |
|
|
<Form.Item label="Popis" name="description">
|
103 |
|
|
<Input.TextArea />
|
104 |
|
|
</Form.Item>
|
105 |
|
|
</Form>
|
106 |
|
|
</Modal>
|
107 |
|
|
);
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
export default TagModal;
|