@carbon/react
Version:
React components for the Carbon Design System
118 lines (114 loc) • 3.57 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React, { useRef, useEffect } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { ErrorFilled, CheckmarkFilled } from '@carbon/icons-react';
import Loading from '../Loading/Loading.js';
import { usePrefix } from '../../internal/usePrefix.js';
const InlineLoading = ({
className,
status = 'active',
iconDescription,
description,
onSuccess,
successDelay = 1500,
...rest
}) => {
const prefix = usePrefix();
const loadingClasses = cx(`${prefix}--inline-loading`, className);
const timerRef = useRef(null);
useEffect(() => {
if (status === 'finished') {
timerRef.current = setTimeout(() => {
if (onSuccess) {
onSuccess();
}
}, successDelay);
}
return () => {
if (timerRef.current) {
clearTimeout(timerRef.current);
timerRef.current = null;
}
};
}, [status, onSuccess, successDelay]);
const getLoading = () => {
let iconLabel = iconDescription ? iconDescription : status;
if (status === 'error') {
return /*#__PURE__*/React.createElement(ErrorFilled, {
className: `${prefix}--inline-loading--error`
}, /*#__PURE__*/React.createElement("title", null, iconLabel));
}
if (status === 'finished') {
return /*#__PURE__*/React.createElement(CheckmarkFilled, {
className: `${prefix}--inline-loading__checkmark-container`
}, /*#__PURE__*/React.createElement("title", null, iconLabel));
}
if (status === 'active') {
if (!iconDescription) {
iconLabel = 'loading';
}
return /*#__PURE__*/React.createElement(Loading, {
small: true,
description: iconLabel,
withOverlay: false,
active: status === 'active'
});
}
if (status === 'inactive') {
if (!iconDescription) {
iconLabel = 'not loading';
}
return /*#__PURE__*/React.createElement("title", {
className: `${prefix}--inline-loading__inactive-status`
}, iconLabel);
}
return undefined;
};
const loadingText = description && /*#__PURE__*/React.createElement("div", {
className: `${prefix}--inline-loading__text`
}, description);
const loading = getLoading();
const loadingAnimation = loading && /*#__PURE__*/React.createElement("div", {
className: `${prefix}--inline-loading__animation`
}, loading);
return /*#__PURE__*/React.createElement("div", _extends({
className: loadingClasses
}, rest, {
"aria-live": rest['aria-live'] ?? 'assertive'
}), loadingAnimation, loadingText);
};
InlineLoading.propTypes = {
/**
* Specify a custom className to be applied to the container node
*/
className: PropTypes.string,
/**
* Specify the description for the inline loading text
*/
description: PropTypes.node,
/**
* Specify the description for the inline loading text
*/
iconDescription: PropTypes.string,
/**
* Provide an optional handler to be invoked when <InlineLoading> is
* successful
*/
onSuccess: PropTypes.func,
/**
* Specify the loading status
*/
status: PropTypes.oneOf(['inactive', 'active', 'finished', 'error']),
/**
* Provide a delay for the `setTimeout` for success
*/
successDelay: PropTypes.number
};
export { InlineLoading as default };