Projekt

Obecné

Profil

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

    
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
    /**
22
     * Tag context.
23
     */
24
    const { selectTag } = useContext(TagCategoryContext);
25

    
26
    /**
27
     * Annotation context
28
     */
29
    const { submitting } = useContext(AnnotationContext);
30

    
31
    /**
32
     * Button icon.
33
     */
34
    const [buttonIcon, setButtonIcon] = useState<React.ReactNode>(<DownOutlined />);
35

    
36
    /**
37
     * Changes visibility of sub tags.
38
     */
39
    const changeSubTagsVisibility = () => {
40
        setButtonIcon(visibleSubTags ? <DownOutlined /> : <UpOutlined />);
41
        setVisibleSubTags(!visibleSubTags);
42
    };
43

    
44
    /**
45
     * Selects the tag.
46
     */
47
    const onSelectTag = () => {
48
        selectTag(props.tag, null);
49
    };
50

    
51
    /**
52
     * Does tag have sub tags?
53
     */
54
    const hasSubTags = (props.tag.subTags?.length ?? 0) > 0;
55

    
56
    return (
57
        <Container>
58
            <Row>
59
                {hasSubTags && (
60
                    <Button
61
                        icon={buttonIcon}
62
                        onClick={changeSubTagsVisibility}
63
                        className="w-100 text-start rounded"
64
                        disabled={submitting}
65
                    >
66
                        {props.tag.name}
67
                        <TagOutlined style={{ color: props.tag.color ?? 'black' }} />
68
                    </Button>
69
                )}
70
                {!hasSubTags && (
71
                    <Button
72
                        onClick={onSelectTag}
73
                        className="w-100 text-start rounded"
74
                        disabled={submitting}
75
                    >
76
                        {props.tag.name}
77
                        <TagOutlined style={{ color: props.tag.color ?? 'black' }} />
78
                    </Button>
79
                )}
80
            </Row>
81
            {visibleSubTags && (
82
                <Stack gap={2} className="mt-2">
83
                    {props.tag.subTags?.map((subTag, index) => (
84
                        <SubTagItem tag={props.tag} subTag={subTag} key={index} />
85
                    ))}
86
                </Stack>
87
            )}
88
        </Container>
89
    );
90
}
(6-6/7)