|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2015 - present Instructure, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +import { ComponentElement, Component } from 'react' |
| 26 | + |
| 27 | +import { logError as error } from '@instructure/console' |
| 28 | +import { |
| 29 | + callRenderProp, |
| 30 | + getElementType, |
| 31 | + matchComponentTypes, |
| 32 | + passthroughProps |
| 33 | +} from '@instructure/ui-react-utils' |
| 34 | + |
| 35 | +import { View } from '@instructure/ui-view/latest' |
| 36 | +import { ScreenReaderContent } from '@instructure/ui-a11y-content' |
| 37 | +import type { ScreenReaderContentProps } from '@instructure/ui-a11y-content' |
| 38 | + |
| 39 | +import { withStyle } from '@instructure/emotion' |
| 40 | + |
| 41 | +import generateStyle from './styles' |
| 42 | +import type { AppNavItemProps } from './props' |
| 43 | +import { allowedProps } from './props' |
| 44 | + |
| 45 | +/** |
| 46 | +--- |
| 47 | +parent: AppNav |
| 48 | +id: AppNav.Item |
| 49 | +--- |
| 50 | +@module Item |
| 51 | +**/ |
| 52 | +@withStyle(generateStyle) |
| 53 | +class Item extends Component<AppNavItemProps> { |
| 54 | + static readonly componentId = 'AppNav.Item' |
| 55 | + |
| 56 | + static allowedProps = allowedProps |
| 57 | + |
| 58 | + static defaultProps = { |
| 59 | + children: null, |
| 60 | + isSelected: false, |
| 61 | + cursor: 'pointer', |
| 62 | + isDisabled: false |
| 63 | + } as const |
| 64 | + |
| 65 | + ref: Element | null = null |
| 66 | + |
| 67 | + componentDidMount() { |
| 68 | + this.props.makeStyles?.() |
| 69 | + } |
| 70 | + |
| 71 | + componentDidUpdate() { |
| 72 | + this.props.makeStyles?.() |
| 73 | + } |
| 74 | + |
| 75 | + handleRef = (el: Element | null) => { |
| 76 | + const { elementRef } = this.props |
| 77 | + |
| 78 | + this.ref = el |
| 79 | + |
| 80 | + if (typeof elementRef === 'function') { |
| 81 | + elementRef(el) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + handleClick = (e: React.MouseEvent) => { |
| 86 | + const { isDisabled, onClick } = this.props |
| 87 | + |
| 88 | + if (isDisabled) { |
| 89 | + e.preventDefault() |
| 90 | + e.stopPropagation() |
| 91 | + } else if (typeof onClick === 'function') { |
| 92 | + onClick(e) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + render() { |
| 97 | + const ElementType = getElementType(Item, this.props) |
| 98 | + |
| 99 | + const { renderIcon, renderLabel, href, renderAfter, cursor, isDisabled } = |
| 100 | + this.props |
| 101 | + |
| 102 | + const icon = callRenderProp(renderIcon) |
| 103 | + const label = callRenderProp(renderLabel) |
| 104 | + |
| 105 | + const labelIsForScreenReaders = matchComponentTypes< |
| 106 | + ComponentElement<ScreenReaderContentProps, ScreenReaderContent> |
| 107 | + >(label, [ScreenReaderContent]) |
| 108 | + |
| 109 | + if (icon) { |
| 110 | + error( |
| 111 | + labelIsForScreenReaders, |
| 112 | + '[AppNav] If an icon is used, the label text should be wrapped in <ScreenReaderContent />.' |
| 113 | + ) |
| 114 | + } |
| 115 | + |
| 116 | + return ( |
| 117 | + <View |
| 118 | + {...passthroughProps(this.props)} |
| 119 | + as={ElementType} |
| 120 | + href={href} |
| 121 | + onClick={this.handleClick} |
| 122 | + disabled={isDisabled} |
| 123 | + elementRef={this.handleRef} |
| 124 | + display="flex" |
| 125 | + position="relative" |
| 126 | + borderRadius="medium" |
| 127 | + cursor={isDisabled ? 'not-allowed' : cursor} |
| 128 | + css={this.props.styles?.item} |
| 129 | + data-cid="AppNav.Item" |
| 130 | + > |
| 131 | + {icon} |
| 132 | + {labelIsForScreenReaders ? ( |
| 133 | + label |
| 134 | + ) : ( |
| 135 | + <span css={this.props.styles?.label}>{label}</span> |
| 136 | + )} |
| 137 | + {renderAfter && callRenderProp(renderAfter)} |
| 138 | + </View> |
| 139 | + ) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +export default Item |
| 144 | +export { Item } |
0 commit comments