UNPKG

@craftercms/studio-ui

Version:

Services, components, models & utils to build CrafterCMS authoring extensions.

134 lines (132 loc) 4.53 kB
/* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import * as React from 'react'; import { forwardRef } from 'react'; import { makeStyles } from 'tss-react/mui'; import palette from '../../styles/palette'; import Typography from '@mui/material/Typography'; import { isPreviewable } from '../PathNavigator/utils'; import ItemStateIcon from '../ItemStateIcon'; import ItemTypeIcon from '../ItemTypeIcon'; import ItemPublishingTargetIcon from '../ItemPublishingTargetIcon'; import { isInWorkflow } from './utils'; const useStyles = makeStyles()((theme, { root, label, labelPreviewable, icon, typeIcon } = {}) => ({ root: { display: 'inline-flex', alignItems: 'center', placeContent: 'left center', maxWidth: '100%', ...root }, label: { marginLeft: 2, display: 'inline-block', ...label }, labelPreviewable: { color: theme.palette.mode === 'dark' ? palette.teal.tint : palette.teal.shade, ...labelPreviewable }, icon: { fontSize: '1.1rem', ...icon }, typeIcon: { marginRight: 3, ...typeIcon } })); const ItemDisplay = forwardRef((props, ref) => { // region const { ... } = props; const { item, styles, classes: propClasses, // @see https://github.com/craftercms/craftercms/issues/5442 // eslint-disable-next-line @typescript-eslint/no-unused-vars showPublishingTarget = true, showWorkflowState = true, showItemType = true, showNavigableAsLinks = true, isNavigableFn = isPreviewable, labelTypographyProps, labelComponent = 'span', labelDisplayProp = 'label', titleDisplayProp = 'label', stateIconProps, publishingTargetIconProps, itemTypeIconProps, ...rest } = props; // endregion const { classes, cx } = useStyles(props.styles); if (!item) { // Prevents crashing if the item is nullish return null; } const inWorkflow = isInWorkflow(item.stateMap) || item.systemType === 'folder'; return React.createElement( 'span', { ref: ref, ...rest, className: cx(classes.root, propClasses?.root, rest?.className) }, inWorkflow ? showWorkflowState && React.createElement(ItemStateIcon, { ...stateIconProps, item: item, className: cx(classes.icon, propClasses?.icon, stateIconProps?.className) }) : showPublishingTarget && React.createElement(ItemPublishingTargetIcon, { ...publishingTargetIconProps, item: item, className: cx(classes.icon, propClasses?.icon, publishingTargetIconProps?.className) }), showItemType && React.createElement(ItemTypeIcon, { ...itemTypeIconProps, item: item, className: cx(classes.icon, propClasses?.icon, itemTypeIconProps?.className) }), React.createElement(Typography, { noWrap: true, component: labelComponent, ...labelTypographyProps, className: cx( classes.label, showNavigableAsLinks && isNavigableFn(item) && classes.labelPreviewable, labelTypographyProps?.className ), title: item[titleDisplayProp], children: item[labelDisplayProp] }) ); }); export default ItemDisplay;