Projekt

Obecné

Profil

Stáhnout (2.28 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Col, Container, Row, Stack } from 'react-bootstrap';
2
import { TagInfo } from '../../api';
3
import { Button } from 'antd';
4
import { DownOutlined, TagOutlined } from '@ant-design/icons';
5
import { 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
    const { selectTag } = useContext(TagCategoryContext);
21

    
22
    /**
23
     * Changes visibility of sub tags.
24
     */
25
    const changeSubTagsVisibility = () => {
26
        setVisibleSubTags(!visibleSubTags);
27
    };
28

    
29
    /**
30
     * Selects the tag.
31
     */
32
    const onSelectTag = () => {
33
        selectTag(props.tag, null);
34
    };
35

    
36
    return (
37
        <Container>
38
            <Row>
39
                <Col>
40
                    {props.tag.subTags && (
41
                        <Button
42
                            type="text"
43
                            icon={<DownOutlined />}
44
                            onClick={changeSubTagsVisibility}
45
                            className="w-100 text-start"
46
                        >
47
                            {props.tag.name}
48
                        </Button>
49
                    )}
50
                    {!props.tag.subTags && (
51
                        <Button
52
                            type="text"
53
                            onClick={onSelectTag}
54
                            className="w-100 text-start"
55
                        >
56
                            {props.tag.name}
57
                        </Button>
58
                    )}
59
                </Col>
60
                <Col sm="auto">
61
                    <TagOutlined style={{ color: props.tag.color ?? 'black' }} />
62
                </Col>
63
            </Row>
64
            {props.tag.subTags && visibleSubTags && (
65
                <Stack>
66
                    {props.tag.subTags?.map((subTag, index) => (
67
                        <SubTagItem tag={props.tag} subTag={subTag} key={index} />
68
                    ))}
69
                </Stack>
70
            )}
71
        </Container>
72
    );
73
}
(6-6/7)