Debian Codegraph @

node/@patternfly/react-core/src/components/ProgressStepper/ProgressStep.tsx

raw
1 import { useRef } from 'react';
2 import styles from '@patternfly/react-styles/css/components/ProgressStepper/progress-stepper';
3 import { css } from '@patternfly/react-styles';
4 import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-check-circle-fill-icon';
5 import ResourcesFullIcon from '@patternfly/react-icons/dist/esm/icons/resources-full-icon';
6 import RhUiWarningFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-warning-fill-icon';
7 import RhUiErrorFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-error-fill-icon';
8
9 export enum ProgressStepVariant {
10 default = 'default',
11 pending = 'pending',
12 success = 'success',
13 info = 'info',
14 danger = 'danger',
15 warning = 'warning'
16 }
17
18 export interface ProgressStepProps extends React.DetailedHTMLProps<
19 React.LiHTMLAttributes<HTMLLIElement>,
20 HTMLLIElement
21 > {
22 /** Content rendered inside the progress step. */
23 children?: React.ReactNode;
24 /** Additional classes applied to the progress step container. */
25 className?: string;
26 /** Variant of the progress step. Each variant has a default icon. */
27 variant?: 'default' | 'success' | 'info' | 'pending' | 'warning' | 'danger';
28 /** Flag indicating the progress step is the current step. */
29 isCurrent?: boolean;
30 /** Custom icon of a progress step. Will override default icons provided by the variant. */
31 icon?: React.ReactNode;
32 /** Description text of a progress step. */
33 description?: React.ReactNode;
34 /** ID of the title of the progress step. */
35 titleId?: string;
36 /** Accessible label for the progress step. Should communicate all information being communicated by the progress
37 * step's icon, including the variant and the completed status. */
38 'aria-label'?: string;
39 /** Forwards the step ref to rendered function. Use this prop to add a popover to the step.*/
40 popoverRender?: (stepRef: React.RefObject<any>) => React.ReactNode;
41 }
42
43 const variantIcons = {
44 default: undefined as any,
45 pending: undefined as any,
46 success: <RhUiCheckCircleFillIcon />,
47 info: <ResourcesFullIcon />,
48 warning: <RhUiWarningFillIcon />,
49 danger: <RhUiErrorFillIcon />
50 };
51
52 const variantStyle = {
53 default: '',
54 info: styles.modifiers.info,
55 success: styles.modifiers.success,
56 pending: styles.modifiers.pending,
57 warning: styles.modifiers.warning,
58 danger: styles.modifiers.danger
59 };
60
61 export const ProgressStep: React.FunctionComponent<ProgressStepProps> = ({
62 children,
63 className,
64 variant,
65 isCurrent,
66 description,
67 icon,
68 titleId,
69 'aria-label': ariaLabel,
70 popoverRender,
71 ...props
72 }: ProgressStepProps) => {
73 const _icon = icon !== undefined ? icon : variantIcons[variant];
74 const Component = popoverRender !== undefined ? 'button' : 'div';
75 const stepRef = useRef(undefined);
76
77 if (props.id === undefined || titleId === undefined) {
78 /* eslint-disable no-console */
79 console.warn(
80 'ProgressStep: The titleId and id properties are required to make this component accessible, and one or both of these properties are missing.'
81 );
82 }
83
84 return (
85 <li
86 className={css(
87 styles.progressStepperStep,
88 variantStyle[variant],
89 isCurrent && styles.modifiers.current,
90 className
91 )}
92 aria-label={ariaLabel}
93 // CSS style `display: contents` gives this li a generic role, we need to override that
94 role="listitem"
95 {...(isCurrent && { 'aria-current': 'step' })}
96 {...props}
97 >
98 <div className={css(styles.progressStepperStepConnector)}>
99 <span className={css(styles.progressStepperStepIcon)}>{_icon && _icon}</span>
100 </div>
101 <div className={css(styles.progressStepperStepMain)}>
102 <Component
103 className={css(styles.progressStepperStepTitle, popoverRender && styles.modifiers.helpText)}
104 id={titleId}
105 ref={stepRef}
106 {...(popoverRender && { type: 'button' })}
107 {...(props.id !== undefined &&
108 titleId !== undefined &&
109 popoverRender && { 'aria-labelledby': `${props.id} ${titleId}` })}
110 >
111 {children}
112 {popoverRender && popoverRender(stepRef)}
113 </Component>
114 {description && <div className={css(styles.progressStepperStepDescription)}>{description}</div>}
115 </div>
116 </li>
117 );
118 };
119
120 ProgressStep.displayName = 'ProgressStep';