Projekt

Obecné

Profil

Stáhnout (2.41 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

    
9
/**
10
 * Creates a single tag item in a tag panel.
11
 * @param props Properties should contain the tag.
12
 * @returns The item that represents the tag.
13
 */
14
export function TagItem(props: { tag: TagInfo }) {
15
    /**
16
     * Should sub tags be visible?
17
     */
18
    const [visibleSubTags, setVisibleSubTags] = useState(false);
19

    
20
    /**
21
     * Tag context.
22
     */
23
    const { selectTag } = useContext(TagCategoryContext);
24

    
25
    /**
26
     * Button icon.
27
     */
28
    const [buttonIcon, setButtonIcon] = useState<React.ReactNode>(<DownOutlined />);
29

    
30
    /**
31
     * Changes visibility of sub tags.
32
     */
33
    const changeSubTagsVisibility = () => {
34
        setButtonIcon(visibleSubTags ? <DownOutlined /> : <UpOutlined />);
35
        setVisibleSubTags(!visibleSubTags);
36
    };
37

    
38
    /**
39
     * Selects the tag.
40
     */
41
    const onSelectTag = () => {
42
        selectTag(props.tag, null);
43
    };
44

    
45
    /**
46
     * Does tag have sub tags?
47
     */
48
    const hasSubTags = (props.tag.subTags?.length ?? 0) > 0;
49

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