Projekt

Obecné

Profil

Stáhnout (2.41 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
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 ed01528f Dominik Poch
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 0441c314 Dominik Poch
    /**
21
     * Tag context.
22
     */
23 5593c10b Dominik Poch
    const { selectTag } = useContext(TagCategoryContext);
24
25 2c9bdf18 Dominik Poch
    /**
26
     * Button icon.
27
     */
28
    const [buttonIcon, setButtonIcon] = useState<React.ReactNode>(<DownOutlined />);
29
30 ed01528f Dominik Poch
    /**
31
     * Changes visibility of sub tags.
32
     */
33
    const changeSubTagsVisibility = () => {
34 2c9bdf18 Dominik Poch
        setButtonIcon(visibleSubTags ? <DownOutlined /> : <UpOutlined />);
35 ed01528f Dominik Poch
        setVisibleSubTags(!visibleSubTags);
36
    };
37
38
    /**
39
     * Selects the tag.
40
     */
41 5593c10b Dominik Poch
    const onSelectTag = () => {
42
        selectTag(props.tag, null);
43
    };
44 ed01528f Dominik Poch
45 2c9bdf18 Dominik Poch
    /**
46
     * Does tag have sub tags?
47
     */
48
    const hasSubTags = (props.tag.subTags?.length ?? 0) > 0;
49
50 ed01528f Dominik Poch
    return (
51
        <Container>
52
            <Row>
53 2c9bdf18 Dominik Poch
                {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 ed01528f Dominik Poch
            </Row>
70 2c9bdf18 Dominik Poch
            {visibleSubTags && (
71
                <Stack gap={2} className="mt-2">
72 ed01528f Dominik Poch
                    {props.tag.subTags?.map((subTag, index) => (
73 5593c10b Dominik Poch
                        <SubTagItem tag={props.tag} subTag={subTag} key={index} />
74 ed01528f Dominik Poch
                    ))}
75
                </Stack>
76
            )}
77
        </Container>
78
    );
79
}