DocSpace-buildtools/packages/asc-web-components/catalog-item/index.js

151 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-09-23 08:08:08 +00:00
import React from "react";
import PropTypes from "prop-types";
import { ReactSVG } from "react-svg";
import Badge from "../badge/";
import {
StyledCatalogItemContainer,
StyledCatalogItemImg,
StyledCatalogItemSibling,
StyledCatalogItemBadgeWrapper,
StyledCatalogItemText,
StyledCatalogItemInitialText,
} from "./styled-catalog-item";
2021-09-30 13:17:10 +00:00
const getInitial = (text) => text.substring(0, 1).toUpperCase();
2021-09-23 08:08:08 +00:00
const CatalogItem = (props) => {
const {
className,
id,
style,
icon,
text,
showText,
onClick,
2022-02-07 08:07:26 +00:00
onDrop,
2021-09-23 08:08:08 +00:00
isEndOfBlock,
isActive,
2022-02-07 08:07:26 +00:00
isDragging,
2021-09-23 08:08:08 +00:00
showInitial,
showBadge,
labelBadge,
iconBadge,
onClickBadge,
} = props;
const onClickAction = () => {
onClick && onClick(id);
2021-09-23 08:08:08 +00:00
};
const onClickBadgeAction = (e) => {
e.stopPropagation();
onClickBadge && onClickBadge(id);
2021-09-23 08:08:08 +00:00
};
2022-02-07 08:07:26 +00:00
const onMouseUpAction = () => {
onDrop && isDragging && onDrop(id, text);
};
2021-09-23 08:08:08 +00:00
return (
<StyledCatalogItemContainer
className={className}
id={id}
style={style}
showText={showText}
isEndOfBlock={isEndOfBlock}
>
<StyledCatalogItemSibling
isActive={isActive}
2022-02-07 08:07:26 +00:00
isDragging={isDragging}
2021-09-23 08:08:08 +00:00
onClick={onClickAction}
2022-02-07 08:07:26 +00:00
onMouseUp={onMouseUpAction}
2021-09-23 08:08:08 +00:00
></StyledCatalogItemSibling>
<StyledCatalogItemImg>
<ReactSVG className="icon" src={icon} />
2021-09-23 08:08:08 +00:00
{!showText && (
<>
{showInitial && (
<StyledCatalogItemInitialText>
{getInitial(text)}
</StyledCatalogItemInitialText>
)}
{showBadge && !iconBadge && (
<StyledCatalogItemBadgeWrapper
onClick={onClickBadgeAction}
showText={showText}
/>
2021-09-23 08:08:08 +00:00
)}
</>
)}
</StyledCatalogItemImg>
{showText && (
<StyledCatalogItemText noSelect={true}>{text}</StyledCatalogItemText>
)}
2021-09-23 08:08:08 +00:00
{showBadge && showText && (
<StyledCatalogItemBadgeWrapper
showText={showText}
onClick={onClickBadgeAction}
>
{!iconBadge ? (
<Badge className="catalog-item__badge" label={labelBadge} />
2021-09-23 08:08:08 +00:00
) : (
<ReactSVG src={iconBadge} />
)}
</StyledCatalogItemBadgeWrapper>
)}
</StyledCatalogItemContainer>
);
};
CatalogItem.propTypes = {
/** Accepts className */
className: PropTypes.string,
/** Accepts id */
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
2021-09-23 08:08:08 +00:00
/** Accepts css style */
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
/** Catalog item icon */
icon: PropTypes.string,
/** Catalog item text */
text: PropTypes.string,
/** Tells when the catalog item should display text */
showText: PropTypes.bool,
/** Call function when user clicked on catalog item */
onClick: PropTypes.func,
2022-02-07 08:07:26 +00:00
/** Call function when user mouse up on catalog item with dragging */
onDrop: PropTypes.func,
2021-09-23 08:08:08 +00:00
/** Tells when the catalog item should display initial on icon, text should be hidden */
showInitial: PropTypes.bool,
/** Tells when the catalog item should be end of block */
isEndOfBlock: PropTypes.bool,
/** Tells when the catalog item should be active */
isActive: PropTypes.bool,
2022-02-07 08:07:26 +00:00
/** Tells when the catalog item available for drag`n`drop */
isDragging: PropTypes.bool,
2021-09-23 08:08:08 +00:00
/** Tells when the catalog item should display badge */
showBadge: PropTypes.bool,
/** Label in catalog item badge */
labelBadge: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/** Special icon for badge, change default badge */
iconBadge: PropTypes.string,
/** Call function when user clicked on catalog item badge */
onClickBadge: PropTypes.func,
};
CatalogItem.defaultProps = {
showText: false,
showBadge: false,
isActive: false,
2021-09-23 08:08:08 +00:00
showInitial: false,
isEndOfBlock: false,
2022-02-07 08:07:26 +00:00
isDragging: false,
2021-09-23 08:08:08 +00:00
};
export default React.memo(CatalogItem);