Projekt

Obecné

Profil

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

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

    
32
    /**
33
     * Selects the tag.
34
     */
35
    const onSelectTag = () => {
36
        selectTag(props.tag, null);
37
    };
38

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