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 |
|
|
name: string;
|
9 |
|
|
color: string;
|
10 |
|
|
description: string;
|
11 |
|
|
}
|
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 |
|
|
const values = props.defaultValues ?? {
|
29 |
|
|
name: '',
|
30 |
|
|
color: 'black',
|
31 |
|
|
description: '',
|
32 |
|
|
};
|
33 |
|
|
|
34 |
|
|
/**
|
35 |
|
|
* The form.
|
36 |
|
|
*/
|
37 |
|
|
const [form] = Form.useForm();
|
38 |
|
|
|
39 |
|
|
/**
|
40 |
|
|
* Handles cancelling of the model window.
|
41 |
|
|
*/
|
42 |
|
|
const handleCancel = () => {
|
43 |
|
|
props.onCancel();
|
44 |
|
|
};
|
45 |
|
|
|
46 |
|
|
/**
|
47 |
|
|
* Handles submitting of the model window.
|
48 |
|
|
* @param values Form values.
|
49 |
|
|
*/
|
50 |
|
|
const handleOk = (values: any) => {
|
51 |
|
|
props.onSubmit({
|
52 |
|
|
name: values.name,
|
53 |
|
|
color: values.color,
|
54 |
|
|
description: values.description,
|
55 |
|
|
});
|
56 |
|
|
};
|
57 |
|
|
|
58 |
|
|
return (
|
59 |
|
|
<Modal
|
60 |
|
|
title={props.title}
|
61 |
|
|
visible={true}
|
62 |
|
|
okText={props.submitText}
|
63 |
|
|
cancelText="Zrušit"
|
64 |
|
|
onCancel={handleCancel}
|
65 |
|
|
onOk={() => {
|
66 |
|
|
// Custom form validation when the modal is submitted.
|
67 |
|
|
// Validates form and if all fields are valid it calls handleOk method.
|
68 |
|
|
form.validateFields()
|
69 |
|
|
.then((values) => {
|
70 |
|
|
form.resetFields();
|
71 |
|
|
handleOk(values);
|
72 |
|
|
})
|
73 |
|
|
.catch((info) => {
|
74 |
|
|
console.log('Validate Failed:', info);
|
75 |
|
|
});
|
76 |
|
|
}}
|
77 |
|
|
>
|
78 |
|
|
<Form form={form} labelCol={{ span: 4 }} initialValues={values}>
|
79 |
|
|
<Form.Item
|
80 |
|
|
label="Název"
|
81 |
|
|
name="name"
|
82 |
|
|
rules={[{ required: true, message: 'Prosím zadejte název' }]}
|
83 |
|
|
>
|
84 |
|
|
<Input />
|
85 |
|
|
</Form.Item>
|
86 |
|
|
<Form.Item
|
87 |
|
|
label="Barva"
|
88 |
|
|
name="color"
|
89 |
|
|
rules={[{ required: true, message: 'Prosím zadejte barvu' }]}
|
90 |
|
|
>
|
91 |
|
|
<Input type="color" />
|
92 |
|
|
</Form.Item>
|
93 |
|
|
<Form.Item label="Popis" name="description">
|
94 |
|
|
<Input.TextArea />
|
95 |
|
|
</Form.Item>
|
96 |
|
|
</Form>
|
97 |
|
|
</Modal>
|
98 |
|
|
);
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
export default TagModal;
|