Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(InlineLoading): add TypeScript types #15247

Merged
merged 10 commits into from
Dec 5, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cx from 'classnames';
import { usePrefix } from '../../internal/usePrefix';
import { noopFn } from '../../internal/noopFn';
import InlineLoading from '../InlineLoading';
import { InlineLoadingStatus } from '../InlineLoading/InlineLoading';

interface SecondaryButtonProps {
buttonText: ReactNode;
Expand Down Expand Up @@ -166,7 +167,7 @@ export interface ModalFooterProps {
/**
* loading status
*/
loadingStatus?: string;
loadingStatus?: InlineLoadingStatus;

/**
* Specify the description for the loading text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,64 @@ import { CheckmarkFilled, ErrorFilled } from '@carbon/icons-react';
import Loading from '../Loading';
import { usePrefix } from '../../internal/usePrefix';

export default function InlineLoading({
export const InlineLoadingStatuses = [
'inactive',
'active',
'finished',
'error',
] as const;

export type InlineLoadingStatus = (typeof InlineLoadingStatuses)[number];

export interface InlineLoadingProps
extends Omit<
React.DetailedHTMLProps<
React.HTMLAttributes<HTMLDivElement>,
HTMLDivElement
>,
'children'
> {
/**
* Specify a custom className to be applied to the container node
*/
className?: string;

/**
* Specify the description for the inline loading text
*/
description?: React.ReactNode;

/**
* Specify the description for the inline loading text
*/
iconDescription?: string;

/**
* Provide an optional handler to be invoked when <InlineLoading> is
* successful
*/
onSuccess?: () => void;

/**
* Specify the loading status
*/
status?: InlineLoadingStatus;

/**
* Provide a delay for the `setTimeout` for success
*/
successDelay?: number;
}

const InlineLoading = ({
className,
status = 'active',
iconDescription,
description,
onSuccess,
successDelay = 1500,
...other
}) {
...rest
}: InlineLoadingProps) => {
const prefix = usePrefix();
const loadingClasses = classNames(`${prefix}--inline-loading`, className);
const getLoading = () => {
Expand Down Expand Up @@ -70,13 +119,13 @@ export default function InlineLoading({
return (
<div
className={loadingClasses}
{...other}
aria-live={'assertive' || other['aria-live']}>
{...rest}
aria-live={'assertive' || rest['aria-live']}>
{loadingAnimation}
{description && loadingText}
</div>
);
}
};

InlineLoading.propTypes = {
/**
Expand Down Expand Up @@ -110,3 +159,5 @@ InlineLoading.propTypes = {
*/
successDelay: PropTypes.number,
};

export default InlineLoading;
5 changes: 3 additions & 2 deletions packages/react/src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { keys, match } from '../../internal/keyboard';
import { noopFn } from '../../internal/noopFn';
import { Text } from '../Text';
import { ReactAttr } from '../../types/common';
import { InlineLoadingStatus } from '../InlineLoading/InlineLoading';

const getInstanceId = setupGetInstanceId();

Expand Down Expand Up @@ -101,7 +102,7 @@ export interface ModalProps extends ReactAttr<HTMLDivElement> {
/**
* Specify loading status
*/
loadingStatus?: string;
loadingStatus?: InlineLoadingStatus;

/**
* Specify a label to be read by screen readers on the modal root node
Expand All @@ -128,7 +129,7 @@ export interface ModalProps extends ReactAttr<HTMLDivElement> {
* Specify an optional handler to be invoked when loading is
* successful
*/
onLoadingSuccess?: React.ReactEventHandler<HTMLElement>;
onLoadingSuccess?: () => void;

/**
* Specify a handler for closing modal.
Expand Down