Revize 29867307
Přidáno uživatelem Dominik Poch před asi 2 roky(ů)
webapp/components/modals/CategoryModal.tsx | ||
---|---|---|
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 |
const values = props.defaultValues ?? { |
|
30 |
name: '', |
|
31 |
color: 'black', |
|
32 |
description: '', |
|
33 |
disabledForAnnotators: false, |
|
34 |
}; |
|
35 |
|
|
36 |
/** |
|
37 |
* The form. |
|
38 |
*/ |
|
39 |
const [form] = Form.useForm(); |
|
40 |
|
|
41 |
/** |
|
42 |
* Handles cancelling of the model window. |
|
43 |
*/ |
|
44 |
const handleCancel = () => { |
|
45 |
props.onCancel(); |
|
46 |
}; |
|
47 |
|
|
48 |
/** |
|
49 |
* Handles submitting of the model window. |
|
50 |
* @param values Form values. |
|
51 |
*/ |
|
52 |
const handleOk = (values: any) => { |
|
53 |
props.onSubmit({ |
|
54 |
name: values.name, |
|
55 |
color: values.color, |
|
56 |
description: values.description, |
|
57 |
disabledForAnnotators: values.disabledForAnnotators, |
|
58 |
}); |
|
59 |
}; |
|
60 |
|
|
61 |
return ( |
|
62 |
<Modal |
|
63 |
title={props.title} |
|
64 |
visible={true} |
|
65 |
okText={props.submitText} |
|
66 |
cancelText="Zrušit" |
|
67 |
onCancel={handleCancel} |
|
68 |
onOk={() => { |
|
69 |
// Custom form validation when the modal is submitted. |
|
70 |
// Validates form and if all fields are valid it calls handleOk method. |
|
71 |
form.validateFields() |
|
72 |
.then((values) => { |
|
73 |
form.resetFields(); |
|
74 |
handleOk(values); |
|
75 |
}) |
|
76 |
.catch((info) => { |
|
77 |
console.log('Validate Failed:', info); |
|
78 |
}); |
|
79 |
}} |
|
80 |
> |
|
81 |
<Form |
|
82 |
form={form} |
|
83 |
labelCol={{ span: 10 }} |
|
84 |
wrapperCol={{ span: 14 }} |
|
85 |
initialValues={values} |
|
86 |
> |
|
87 |
<Form.Item |
|
88 |
label="Název" |
|
89 |
name="name" |
|
90 |
rules={[{ required: true, message: 'Prosím zadejte název' }]} |
|
91 |
> |
|
92 |
<Input /> |
|
93 |
</Form.Item> |
|
94 |
<Form.Item |
|
95 |
label="Barva" |
|
96 |
name="color" |
|
97 |
rules={[{ required: true, message: 'Prosím zadejte barvu' }]} |
|
98 |
> |
|
99 |
<Input type="color" /> |
|
100 |
</Form.Item> |
|
101 |
<Form.Item label="Popis" name="description"> |
|
102 |
<Input.TextArea /> |
|
103 |
</Form.Item> |
|
104 |
<Form.Item |
|
105 |
label="Skrýt pro anotátory" |
|
106 |
name="disabledForAnnotators" |
|
107 |
valuePropName="checked" |
|
108 |
rules={[ |
|
109 |
{ |
|
110 |
required: true, |
|
111 |
message: |
|
112 |
'Prosím zadejte zda má být kategorie viditelná pro anotátory', |
|
113 |
}, |
|
114 |
]} |
|
115 |
> |
|
116 |
<Switch /> |
|
117 |
</Form.Item> |
|
118 |
</Form> |
|
119 |
</Modal> |
|
120 |
); |
|
121 |
} |
|
122 |
|
|
123 |
export default CategoryModal; |
Také k dispozici: Unified diff
Added category modal
Modal with properties of a category