Debian Codegraph @

node/@patternfly/react-core/src/components/Wizard/WizardToggle.tsx

raw
1 import { Fragment, useCallback, useEffect } from 'react';
2 import { css } from '@patternfly/react-styles';
3 import styles from '@patternfly/react-styles/css/components/Wizard/wizard';
4 import RhMicronsCaretRightIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-right-icon';
5 import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon';
6 import RhUiErrorFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-error-fill-icon';
7 import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-check-circle-fill-icon';
8
9 import { KeyTypes } from '../../helpers/constants';
10 import { WizardStepType, isWizardSubStep } from './types';
11 import { WizardNavProps } from './WizardNav';
12 import { WizardStep, WizardStepProps } from './WizardStep';
13 import { WizardBody } from './WizardBody';
14
15 /**
16 * Used to toggle between step content, including the body and footer. This is also where the navigation and its expandability is controlled.
17 */
18
19 export interface WizardToggleProps {
20 /** List of steps and/or sub-steps */
21 steps: WizardStepType[];
22 /** The current step */
23 activeStep: WizardStepType;
24 /** Wizard footer */
25 footer: React.ReactElement<any>;
26 /** Wizard navigation */
27 nav: React.ReactElement<WizardNavProps>;
28 /** The expandable dropdown button's aria-label */
29 'aria-label'?: string;
30 /** Flag to determine whether the dropdown navigation is expanded */
31 isNavExpanded?: boolean;
32 /** Callback to expand or collapse the dropdown navigation */
33 toggleNavExpanded?: (event: React.MouseEvent<HTMLButtonElement> | KeyboardEvent) => void;
34 }
35
36 export const WizardToggle = ({
37 steps,
38 activeStep,
39 footer,
40 nav,
41 isNavExpanded,
42 toggleNavExpanded,
43 'aria-label': ariaLabel = 'Wizard toggle'
44 }: WizardToggleProps) => {
45 const isActiveSubStep = isWizardSubStep(activeStep);
46 const parentStep = isActiveSubStep && steps.find((step) => step.id === activeStep.parentId);
47 const nonSubSteps = steps.filter((step) => !isWizardSubStep(step));
48 const wizardToggleIndex = nonSubSteps.indexOf(parentStep || activeStep) + 1;
49 const isActiveStepStatus = activeStep.status;
50
51 const handleKeyClicks = useCallback(
52 (event: KeyboardEvent): void => {
53 if (isNavExpanded && event.key === KeyTypes.Escape) {
54 toggleNavExpanded?.(event);
55 }
56 },
57 [isNavExpanded, toggleNavExpanded]
58 );
59
60 // Open/close collapsable navigation on keydown event
61 useEffect(() => {
62 const target = typeof document !== 'undefined' ? document.body : null;
63 target?.addEventListener('keydown', handleKeyClicks, false);
64
65 return () => {
66 target?.removeEventListener('keydown', handleKeyClicks, false);
67 };
68 }, [handleKeyClicks]);
69
70 const bodyContent = steps.map((step) => {
71 const props: WizardStepProps = step.component?.props || {};
72 const { children, body, ...propsWithoutChildren } = props;
73
74 return (
75 <Fragment key={step.id}>
76 {activeStep?.id === step.id && <WizardBody {...body}>{children}</WizardBody>}
77
78 <div key={step.id} style={{ display: 'none' }}>
79 <WizardStep {...propsWithoutChildren} />
80 </div>
81 </Fragment>
82 );
83 });
84
85 return (
86 <>
87 <button
88 onClick={toggleNavExpanded}
89 className={css(styles.wizardToggle, isNavExpanded && 'pf-m-expanded')}
90 aria-label={ariaLabel}
91 aria-expanded={isNavExpanded}
92 >
93 <span className={css(styles.wizardToggleList)}>
94 <span
95 className={css(
96 styles.wizardToggleListItem,
97 isActiveStepStatus === 'error' && styles.modifiers.danger,
98 isActiveStepStatus === 'success' && styles.modifiers.success
99 )}
100 >
101 {isActiveStepStatus === 'error' && (
102 <span className={css(styles.wizardToggleStatusIcon)}>
103 <RhUiErrorFillIcon />
104 </span>
105 )}
106 {isActiveStepStatus === 'success' && (
107 <span className={css(styles.wizardToggleStatusIcon)}>
108 <RhUiCheckCircleFillIcon />
109 </span>
110 )}
111 {isActiveStepStatus !== 'success' && isActiveStepStatus !== 'error' && (
112 <span className={css(styles.wizardToggleNum)}>{wizardToggleIndex}</span>
113 )}{' '}
114 {parentStep?.name || activeStep?.name}
115 {isActiveSubStep && (
116 <span className={css(styles.wizardToggleSeparator)}>
117 <RhMicronsCaretRightIcon />
118 </span>
119 )}
120 </span>
121 {isActiveSubStep && <span className={css(styles.wizardToggleListItem)}>{activeStep?.name}</span>}
122 </span>
123
124 <span className={css(styles.wizardToggleIcon)}>
125 <RhMicronsCaretDownIcon />
126 </span>
127 </button>
128 <div className={css(styles.wizardOuterWrap)}>
129 <div className={css(styles.wizardInnerWrap)}>
130 {nav}
131 {bodyContent}
132 </div>
133
134 {footer}
135 </div>
136 </>
137 );
138 };
139
140 WizardToggle.displayName = 'WizardToggle';