/* * The MIT License (MIT) * * Copyright (c) 2015 - present Instructure, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import { Component, ContextType } from 'react' import { Img } from '@instructure/ui-img/latest' import { withStyleNew } from '@instructure/emotion' import { renderIconWithProps } from '@instructure/ui-icons' import generateStyles from '../TreeButton/styles.js' import type { TreeBrowserNodeProps } from './props' import { allowedProps } from './props.js' import TreeBrowserContext from '../TreeBrowserContext.js' // Map TreeBrowser size tokens to icon size tokens const treeBrowserSizeToIconSize = { small: 'md', medium: 'lg', large: 'xl' } as const // Todo: merge TreeButton and TreeNode: TreeButton should be a special type of TreeNode /** --- parent: TreeBrowser id: TreeBrowser.Node --- A helper class used to render the :renderBeforeItems and :renderAfterItems in the TreeBrowser. **/ @withStyleNew(generateStyles, 'TreeBrowserTreeButton') class TreeNode extends Component { static displayName = 'TreeNode' static readonly componentId = 'TreeBrowser.Node' static readonly themeId = 'TreeBrowserTreeNode' static allowedProps = allowedProps static contextType = TreeBrowserContext declare context: ContextType static defaultProps = { size: 'medium', variant: 'folderTree', selected: false, focused: false, hoverable: true } ref: Element | null = null state = { isHovered: false } componentDidMount() { this.props.makeStyles?.({ animation: this.context?.animation, hoverable: this.props.hoverable }) } componentDidUpdate() { this.props.makeStyles?.({ animation: this.context?.animation, hoverable: this.props.hoverable }) } handleRef = (el: HTMLDivElement) => { if (el && typeof this.props.containerRef === 'function') { this.props.containerRef(el.parentElement) } this.ref = el } handleMouseEnter = () => { if (this.props.hoverable) { this.setState({ isHovered: true }) } } handleMouseLeave = () => { if (this.props.hoverable) { this.setState({ isHovered: false }) } } getIconColor(): 'baseColor' | 'onColor' | 'inverseColor' { const { selected } = this.props const { isHovered } = this.state // Priority: selected > hover > default if (selected) return 'onColor' if (isHovered) return 'inverseColor' return 'baseColor' } renderItemImage() { const { thumbnail, itemIcon, styles, size } = this.props if (thumbnail) { return (
) } if (itemIcon) { const iconSize = treeBrowserSizeToIconSize[size!] const iconColor = this.getIconColor() const iconElement = renderIconWithProps(itemIcon, iconSize, iconColor) return
{iconElement}
} return undefined } render() { const { children, styles } = this.props return (
{this.renderItemImage()} {children}
) } } export default TreeNode export { TreeNode }