Projekt

Obecné

Profil

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