Projekt

Obecné

Profil

Stáhnout (3.56 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Container, Row, Stack } from 'react-bootstrap';
2
import { TagInfo } from '../../api';
3
import { Button, Tooltip } from 'antd';
4
import {
5
    DownOutlined,
6
    QuestionCircleOutlined,
7
    TagOutlined,
8
    UpOutlined,
9
} from '@ant-design/icons';
10
import React, { useContext, useState } from 'react';
11
import { SubTagItem } from './SubTagItem';
12
import { TagCategoryContext } from '../../contexts/TagCategoryContext';
13
import { AnnotationContext } from '../../contexts/AnnotationContext';
14

    
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
    /**
27
     * Tag context.
28
     */
29
    const { selectTag } = useContext(TagCategoryContext);
30

    
31
    /**
32
     * Annotation context
33
     */
34
    const { submitting } = useContext(AnnotationContext);
35

    
36
    /**
37
     * Button icon.
38
     */
39
    const [buttonIcon, setButtonIcon] = useState<React.ReactNode>(<DownOutlined />);
40

    
41
    /**
42
     * Changes visibility of sub tags.
43
     */
44
    const changeSubTagsVisibility = () => {
45
        setButtonIcon(visibleSubTags ? <DownOutlined /> : <UpOutlined />);
46
        setVisibleSubTags(!visibleSubTags);
47
    };
48

    
49
    /**
50
     * Selects the tag.
51
     */
52
    const onSelectTag = () => {
53
        selectTag(props.tag, null);
54
    };
55

    
56
    /**
57
     * Does tag have sub tags?
58
     */
59
    const hasSubTags = (props.tag.subTags?.length ?? 0) > 0;
60

    
61
    return (
62
        <Container>
63
            <Row>
64
                {hasSubTags && (
65
                    <Button
66
                        icon={buttonIcon}
67
                        onClick={changeSubTagsVisibility}
68
                        className="w-100 text-start rounded"
69
                        disabled={submitting}
70
                    >
71
                        <TagOutlined style={{ color: props.tag.color ?? 'black' }} />
72
                        {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
                    </Button>
82
                )}
83
                {!hasSubTags && (
84
                    <Button
85
                        onClick={onSelectTag}
86
                        className="w-100 text-start rounded"
87
                        disabled={submitting}
88
                    >
89
                        <TagOutlined style={{ color: props.tag.color ?? 'black' }} />
90
                        {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
                    </Button>
100
                )}
101
            </Row>
102
            {visibleSubTags && (
103
                <Stack gap={2} className="mt-2">
104
                    {props.tag.subTags?.map((subTag, index) => (
105
                        <SubTagItem tag={props.tag} subTag={subTag} key={index} />
106
                    ))}
107
                </Stack>
108
            )}
109
        </Container>
110
    );
111
}
(6-6/7)