|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useI18nBundle, useIsomorphicId } from '@ui5/webcomponents-react-base'; |
| 4 | +import { clsx } from 'clsx'; |
| 5 | +import React, { forwardRef, useEffect, useRef, useState } from 'react'; |
| 6 | +import { createPortal } from 'react-dom'; |
| 7 | +import { createUseStyles } from 'react-jss'; |
| 8 | +import { CLOSE_POPOVER, SHOW_FULL_TEXT, SHOW_LESS, SHOW_MORE } from '../../i18n/i18n-defaults.js'; |
| 9 | +import type { CommonProps } from '../../interfaces/index.js'; |
| 10 | +import { useCanRenderPortal } from '../../internal/ssr.js'; |
| 11 | +import { getUi5TagWithSuffix } from '../../internal/utils.js'; |
| 12 | +import type { LinkDomRef } from '../../webComponents/index.js'; |
| 13 | +import { Link } from '../../webComponents/index.js'; |
| 14 | +import { ResponsivePopover } from '../../webComponents/ResponsivePopover/index.js'; |
| 15 | +import type { TextPropTypes } from '../Text/index.js'; |
| 16 | +import { Text } from '../Text/index.js'; |
| 17 | +import { TextStyles } from '../Text/Text.jss.js'; |
| 18 | + |
| 19 | +export interface ExpandableTextPropTypes |
| 20 | + extends Omit<TextPropTypes, 'maxLines' | 'wrapping' | 'children'>, |
| 21 | + CommonProps { |
| 22 | + /** |
| 23 | + * Determines the text to be displayed. |
| 24 | + */ |
| 25 | + children?: string; |
| 26 | + /** |
| 27 | + * Specifies the maximum number of characters from the beginning of the text field that are shown initially. |
| 28 | + * |
| 29 | + * @default 100 |
| 30 | + */ |
| 31 | + maxCharacters?: number; |
| 32 | + /** |
| 33 | + * Determines if the full text should be displayed inside a `ResponsivePopover` or in-place. |
| 34 | + */ |
| 35 | + showOverflowInPopover?: boolean; |
| 36 | + /** |
| 37 | + * Defines where modals are rendered into via `React.createPortal`. |
| 38 | + * |
| 39 | + * You can find out more about this [here](https://sap.github.io/ui5-webcomponents-react/?path=/docs/knowledge-base-working-with-portals--page). |
| 40 | + * |
| 41 | + * @default document.body |
| 42 | + */ |
| 43 | + portalContainer?: Element; |
| 44 | +} |
| 45 | + |
| 46 | +const useStyles = createUseStyles( |
| 47 | + { |
| 48 | + expandableText: { ...TextStyles.text }, |
| 49 | + text: { display: 'inline' }, |
| 50 | + ellipsis: { wordSpacing: '0.125rem' }, |
| 51 | + popover: { maxWidth: '30rem', '&::part(content)': { padding: '1rem' } } |
| 52 | + }, |
| 53 | + { name: 'ExpandableText' } |
| 54 | +); |
| 55 | +/** |
| 56 | + * The `ExpandableText` component can be used to display long texts inside a table, list or form. |
| 57 | + * |
| 58 | + * Initially, only the first characters from the text are shown with a "Show More" link which allows the full text to be displayed. The `showOverflowInPopover` property determines if the full text will be displayed expanded in place (default) or in a popover (`showOverflowInPopover: true`). If the text is expanded a "Show Less" link is displayed, which allows collapsing the text field. |
| 59 | + * |
| 60 | + * @since 1.23.0 |
| 61 | + */ |
| 62 | +const ExpandableText = forwardRef<HTMLSpanElement, ExpandableTextPropTypes>((props, ref) => { |
| 63 | + const { |
| 64 | + children, |
| 65 | + emptyIndicator, |
| 66 | + renderWhitespace, |
| 67 | + hyphenated, |
| 68 | + showOverflowInPopover, |
| 69 | + maxCharacters = 100, |
| 70 | + portalContainer, |
| 71 | + className, |
| 72 | + ...rest |
| 73 | + } = props; |
| 74 | + const [collapsed, setCollapsed] = useState(true); |
| 75 | + const [popoverOpen, setPopoverOpen] = useState(false); |
| 76 | + const linkRef = useRef<LinkDomRef>(null); |
| 77 | + const classes = useStyles(); |
| 78 | + const uniqueId = useIsomorphicId(); |
| 79 | + const i18nBundle = useI18nBundle('@ui5/webcomponents-react'); |
| 80 | + const trimmedChildren = renderWhitespace ? children : children?.replace(/\s+/g, ' ').trim(); |
| 81 | + const isOverflow = trimmedChildren?.length >= maxCharacters; |
| 82 | + const strippedChildren = |
| 83 | + isOverflow && (collapsed || showOverflowInPopover) ? trimmedChildren?.slice(0, maxCharacters) : children; |
| 84 | + |
| 85 | + const handleClick = () => { |
| 86 | + if (showOverflowInPopover) { |
| 87 | + setPopoverOpen((prev) => !prev); |
| 88 | + } |
| 89 | + setCollapsed((prev) => !prev); |
| 90 | + }; |
| 91 | + |
| 92 | + const closePopover = () => { |
| 93 | + setCollapsed(true); |
| 94 | + setPopoverOpen(false); |
| 95 | + }; |
| 96 | + |
| 97 | + useEffect(() => { |
| 98 | + const tagName = getUi5TagWithSuffix('ui5-link'); |
| 99 | + void customElements.whenDefined(tagName).then(() => { |
| 100 | + if (linkRef.current) { |
| 101 | + if (showOverflowInPopover) { |
| 102 | + linkRef.current.accessibilityAttributes = { hasPopup: 'Dialog' }; |
| 103 | + } else { |
| 104 | + linkRef.current.accessibilityAttributes = { expanded: !collapsed }; |
| 105 | + } |
| 106 | + } |
| 107 | + }); |
| 108 | + }, [collapsed, showOverflowInPopover]); |
| 109 | + |
| 110 | + const canRenderPortal = useCanRenderPortal(); |
| 111 | + if (showOverflowInPopover && !canRenderPortal) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + return ( |
| 115 | + <span className={clsx(classes.expandableText, className)} {...rest} ref={ref}> |
| 116 | + <Text |
| 117 | + emptyIndicator={emptyIndicator} |
| 118 | + renderWhitespace={renderWhitespace} |
| 119 | + hyphenated={hyphenated} |
| 120 | + className={classes.text} |
| 121 | + > |
| 122 | + {strippedChildren} |
| 123 | + </Text> |
| 124 | + {isOverflow && ( |
| 125 | + <> |
| 126 | + <span className={classes.ellipsis}>{showOverflowInPopover || collapsed ? '... ' : ' '}</span> |
| 127 | + <Link |
| 128 | + accessibleName={ |
| 129 | + showOverflowInPopover |
| 130 | + ? collapsed |
| 131 | + ? i18nBundle.getText(CLOSE_POPOVER) |
| 132 | + : i18nBundle.getText(SHOW_FULL_TEXT) |
| 133 | + : undefined |
| 134 | + } |
| 135 | + accessibleRole="button" |
| 136 | + onClick={handleClick} |
| 137 | + ref={linkRef} |
| 138 | + id={`${uniqueId}-link`} |
| 139 | + > |
| 140 | + {collapsed ? i18nBundle.getText(SHOW_MORE) : i18nBundle.getText(SHOW_LESS)} |
| 141 | + </Link> |
| 142 | + </> |
| 143 | + )} |
| 144 | + {showOverflowInPopover && |
| 145 | + popoverOpen && |
| 146 | + createPortal( |
| 147 | + <ResponsivePopover opener={`${uniqueId}-link`} open onAfterClose={closePopover} className={classes.popover}> |
| 148 | + <Text renderWhitespace={renderWhitespace} hyphenated={hyphenated} className={classes.text}> |
| 149 | + {children} |
| 150 | + </Text> |
| 151 | + </ResponsivePopover>, |
| 152 | + portalContainer ?? document.body |
| 153 | + )} |
| 154 | + </span> |
| 155 | + ); |
| 156 | +}); |
| 157 | + |
| 158 | +ExpandableText.displayName = 'ExpandableText'; |
| 159 | + |
| 160 | +export { ExpandableText }; |
0 commit comments