1 |
2c9bdf18
|
Dominik Poch
|
import { Container, Row, Stack } from 'react-bootstrap';
|
2 |
ed01528f
|
Dominik Poch
|
import { TagInfo } from '../../api';
|
3 |
|
|
import { Button } from 'antd';
|
4 |
2c9bdf18
|
Dominik Poch
|
import { DownOutlined, TagOutlined, UpOutlined } from '@ant-design/icons';
|
5 |
|
|
import React, { useContext, useState } from 'react';
|
6 |
ed01528f
|
Dominik Poch
|
import { SubTagItem } from './SubTagItem';
|
7 |
5593c10b
|
Dominik Poch
|
import { TagCategoryContext } from '../../contexts/TagCategoryContext';
|
8 |
dee53692
|
Jaroslav Hrubý
|
import { AnnotationContext } from '../../contexts/AnnotationContext';
|
9 |
ed01528f
|
Dominik Poch
|
|
10 |
|
|
/**
|
11 |
|
|
* Creates a single tag item in a tag panel.
|
12 |
|
|
* @param props Properties should contain the tag.
|
13 |
|
|
* @returns The item that represents the tag.
|
14 |
|
|
*/
|
15 |
|
|
export function TagItem(props: { tag: TagInfo }) {
|
16 |
|
|
/**
|
17 |
|
|
* Should sub tags be visible?
|
18 |
|
|
*/
|
19 |
|
|
const [visibleSubTags, setVisibleSubTags] = useState(false);
|
20 |
|
|
|
21 |
0441c314
|
Dominik Poch
|
/**
|
22 |
|
|
* Tag context.
|
23 |
|
|
*/
|
24 |
5593c10b
|
Dominik Poch
|
const { selectTag } = useContext(TagCategoryContext);
|
25 |
|
|
|
26 |
dee53692
|
Jaroslav Hrubý
|
/**
|
27 |
|
|
* Annotation context
|
28 |
|
|
*/
|
29 |
|
|
const { submitting } = useContext(AnnotationContext);
|
30 |
|
|
|
31 |
2c9bdf18
|
Dominik Poch
|
/**
|
32 |
|
|
* Button icon.
|
33 |
|
|
*/
|
34 |
|
|
const [buttonIcon, setButtonIcon] = useState<React.ReactNode>(<DownOutlined />);
|
35 |
|
|
|
36 |
ed01528f
|
Dominik Poch
|
/**
|
37 |
|
|
* Changes visibility of sub tags.
|
38 |
|
|
*/
|
39 |
|
|
const changeSubTagsVisibility = () => {
|
40 |
2c9bdf18
|
Dominik Poch
|
setButtonIcon(visibleSubTags ? <DownOutlined /> : <UpOutlined />);
|
41 |
ed01528f
|
Dominik Poch
|
setVisibleSubTags(!visibleSubTags);
|
42 |
|
|
};
|
43 |
|
|
|
44 |
|
|
/**
|
45 |
|
|
* Selects the tag.
|
46 |
|
|
*/
|
47 |
5593c10b
|
Dominik Poch
|
const onSelectTag = () => {
|
48 |
|
|
selectTag(props.tag, null);
|
49 |
|
|
};
|
50 |
ed01528f
|
Dominik Poch
|
|
51 |
2c9bdf18
|
Dominik Poch
|
/**
|
52 |
|
|
* Does tag have sub tags?
|
53 |
|
|
*/
|
54 |
|
|
const hasSubTags = (props.tag.subTags?.length ?? 0) > 0;
|
55 |
|
|
|
56 |
ed01528f
|
Dominik Poch
|
return (
|
57 |
|
|
<Container>
|
58 |
|
|
<Row>
|
59 |
2c9bdf18
|
Dominik Poch
|
{hasSubTags && (
|
60 |
|
|
<Button
|
61 |
|
|
icon={buttonIcon}
|
62 |
|
|
onClick={changeSubTagsVisibility}
|
63 |
|
|
className="w-100 text-start rounded"
|
64 |
dee53692
|
Jaroslav Hrubý
|
disabled={submitting}
|
65 |
2c9bdf18
|
Dominik Poch
|
>
|
66 |
|
|
{props.tag.name}
|
67 |
|
|
<TagOutlined style={{ color: props.tag.color ?? 'black' }} />
|
68 |
|
|
</Button>
|
69 |
|
|
)}
|
70 |
|
|
{!hasSubTags && (
|
71 |
dee53692
|
Jaroslav Hrubý
|
<Button
|
72 |
|
|
onClick={onSelectTag}
|
73 |
|
|
className="w-100 text-start rounded"
|
74 |
|
|
disabled={submitting}
|
75 |
|
|
>
|
76 |
2c9bdf18
|
Dominik Poch
|
{props.tag.name}
|
77 |
|
|
<TagOutlined style={{ color: props.tag.color ?? 'black' }} />
|
78 |
|
|
</Button>
|
79 |
|
|
)}
|
80 |
ed01528f
|
Dominik Poch
|
</Row>
|
81 |
2c9bdf18
|
Dominik Poch
|
{visibleSubTags && (
|
82 |
|
|
<Stack gap={2} className="mt-2">
|
83 |
ed01528f
|
Dominik Poch
|
{props.tag.subTags?.map((subTag, index) => (
|
84 |
5593c10b
|
Dominik Poch
|
<SubTagItem tag={props.tag} subTag={subTag} key={index} />
|
85 |
ed01528f
|
Dominik Poch
|
))}
|
86 |
|
|
</Stack>
|
87 |
|
|
)}
|
88 |
|
|
</Container>
|
89 |
|
|
);
|
90 |
|
|
}
|