@ui5/webcomponents-react
Version:
React Wrapper for UI5 Web Components and additional components
176 lines (174 loc) • 7.29 kB
JavaScript
'use client';
import { VALUE_STATE_ERROR, VALUE_STATE_INFORMATION, VALUE_STATE_SUCCESS, VALUE_STATE_WARNING } from '@ui5/webcomponents/dist/generated/i18n/i18n-defaults.js';
import IconMode from '@ui5/webcomponents/dist/types/IconMode.js';
import ValueState from '@ui5/webcomponents-base/dist/types/ValueState.js';
import alertIcon from '@ui5/webcomponents-icons/dist/alert.js';
import errorIcon from '@ui5/webcomponents-icons/dist/error.js';
import informationIcon from '@ui5/webcomponents-icons/dist/information.js';
import successIcon from '@ui5/webcomponents-icons/dist/sys-enter-2.js';
import { useI18nBundle, useStylesheet } from '@ui5/webcomponents-react-base';
import { clsx } from 'clsx';
import { forwardRef } from 'react';
import { ARIA_OBJ_STATUS_DESC, ARIA_OBJ_STATUS_DESC_INACTIVE, EMPTY_VALUE, INDICATION_COLOR } from '../../i18n/i18n-defaults.js';
import { Icon } from '../../webComponents/Icon/index.js';
import { classNames, styleData } from './ObjectStatus.module.css.js';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const getStateSpecifics = (state, showDefaultIcon, userIcon, stateAnnouncementText, i18nTexts) => {
const {
indicationColorText,
errorStateText,
warningStateText,
informationStateText,
successStateText
} = i18nTexts;
let icon = userIcon;
const renderDefaultIcon = showDefaultIcon && !icon;
let invisibleText = stateAnnouncementText;
if (!invisibleText && state.startsWith('Indication')) {
const indicationNumber = parseInt(state.replace('Indication', ''), 10);
invisibleText = `${indicationColorText} ${indicationNumber}`;
}
if (!invisibleText || renderDefaultIcon) {
switch (state) {
case ValueState.Negative:
if (renderDefaultIcon) {
icon = /*#__PURE__*/_jsx(Icon, {
name: errorIcon,
"data-component-name": "ObjectStatusDefaultIcon",
mode: IconMode.Decorative
});
}
if (!invisibleText) {
invisibleText = errorStateText;
}
break;
case ValueState.Positive:
if (renderDefaultIcon) {
icon = /*#__PURE__*/_jsx(Icon, {
name: successIcon,
"data-component-name": "ObjectStatusDefaultIcon",
mode: IconMode.Decorative
});
}
if (!invisibleText) {
invisibleText = successStateText;
}
break;
case ValueState.Critical:
if (renderDefaultIcon) {
icon = /*#__PURE__*/_jsx(Icon, {
name: alertIcon,
"data-component-name": "ObjectStatusDefaultIcon",
mode: IconMode.Decorative
});
}
if (!invisibleText) {
invisibleText = warningStateText;
}
break;
case ValueState.Information:
if (renderDefaultIcon) {
icon = /*#__PURE__*/_jsx(Icon, {
name: informationIcon,
"data-component-name": "ObjectStatusDefaultIcon",
mode: IconMode.Decorative
});
}
if (!invisibleText) {
invisibleText = informationStateText;
}
break;
}
}
return [icon, invisibleText];
};
/**
* Status information that can be either text with a value state, or an icon.
*
* __Note:__ When used in interactive lists or tables, consider overriding the active state styling. Learn more [here](https://ui5.github.io/webcomponents-react/v2/?path=/docs/data-display-objectstatus--docs#objectstatus-in-interactive-lists-or-tables).
*/
const ObjectStatus = /*#__PURE__*/forwardRef((props, ref) => {
const {
state = ValueState.None,
showDefaultIcon,
children,
icon,
className,
style,
interactive,
inverted,
onClick,
emptyIndicator,
stateAnnouncementText,
large,
'aria-roledescription': ariaRoleDescription,
...rest
} = props;
const i18nBundle = useI18nBundle('@ui5/webcomponents-react');
const i18nBundleWc = useI18nBundle('@ui5/webcomponents');
useStylesheet(styleData, ObjectStatus.displayName);
const indicationColorText = i18nBundle.getText(INDICATION_COLOR);
const errorStateText = i18nBundleWc.getText(VALUE_STATE_ERROR);
const warningStateText = i18nBundleWc.getText(VALUE_STATE_WARNING);
const informationStateText = i18nBundleWc.getText(VALUE_STATE_INFORMATION);
const successStateText = i18nBundleWc.getText(VALUE_STATE_SUCCESS);
const [iconToRender, invisibleText] = getStateSpecifics(state, showDefaultIcon, icon, stateAnnouncementText, {
indicationColorText,
errorStateText,
warningStateText,
informationStateText,
successStateText
});
const showEmptyIndicator = emptyIndicator && !children;
const computedChildren = showEmptyIndicator ? /*#__PURE__*/_jsx("span", {
"aria-hidden": showEmptyIndicator ? 'true' : undefined,
"data-component-name": "ObjectStatusEmptyIndicator",
className: classNames.emptyIndicator,
children: "\u2013"
}) : children;
const objStatusClasses = clsx(classNames.normalizeCSS, classNames.objectStatus, classNames[`${state}`.toLowerCase()], interactive && classNames.active, inverted && !showEmptyIndicator && classNames.inverted, large && classNames.large, className);
const TagName = interactive ? 'button' : 'div';
const roleDesc = `${ariaRoleDescription ? `${ariaRoleDescription} ` : ''}${interactive ? i18nBundle.getText(ARIA_OBJ_STATUS_DESC) : ''}`;
return /*#__PURE__*/_jsxs(TagName
// @ts-expect-error: both refs are allowed (attributes, etc. of HTMLButtonElement should only be used if `interactive` is `true`)
, {
ref: ref,
className: objStatusClasses,
style: style
// @ts-expect-error: onClick is only registered if the event target is a HTMLButtonElement
,
onClick: interactive ? onClick : undefined,
tabIndex: interactive ? 0 : undefined,
"data-icon-only": !children,
role: interactive ? undefined : 'group',
"aria-roledescription": roleDesc || undefined,
...rest,
"data-component-name": "ObjectStatus",
"data-state": state,
"data-inverted": `${!!inverted}`,
children: [!interactive && /*#__PURE__*/_jsx("span", {
className: classNames.pseudoInvisibleText,
"data-component-name": "ObjectStatusInvisibleDescriptionContainer",
children: i18nBundle.getText(ARIA_OBJ_STATUS_DESC_INACTIVE)
}), iconToRender && /*#__PURE__*/_jsx("span", {
className: classNames.icon,
"data-icon-only": !children,
"data-component-name": "ObjectStatusIconContainer",
children: iconToRender
}), computedChildren && /*#__PURE__*/_jsxs("span", {
className: classNames.text,
"data-component-name": "ObjectStatusTextContainer",
children: [computedChildren, showEmptyIndicator && /*#__PURE__*/_jsx("span", {
className: classNames.pseudoInvisibleText,
"data-component-name": "ObjectStatusInvisibleEmptyTextContainer",
children: i18nBundle.getText(EMPTY_VALUE)
})]
}), !!invisibleText && (computedChildren || iconToRender) && /*#__PURE__*/_jsx("span", {
className: classNames.pseudoInvisibleText,
"data-component-name": "ObjectStatusInvisibleTextContainer",
children: invisibleText
})]
});
});
ObjectStatus.displayName = 'ObjectStatus';
export { ObjectStatus };