Debian Codegraph @

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

raw
1 import { useEffect, useState } from 'react';
2 import { css } from '@patternfly/react-styles';
3 import styles from '@patternfly/react-styles/css/components/Wizard/wizard';
4 import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon';
5 import { useOUIAProps, OUIAProps } from '../../../helpers';
6
7 export interface WizardNavItemProps extends OUIAProps {
8 /** Can nest a WizardNav component for substeps */
9 children?: React.ReactNode;
10 /** The content to display in the nav item */
11 content?: React.ReactNode;
12 /** Whether the nav item is the currently active item */
13 isCurrent?: boolean;
14 /** Whether the nav item is disabled */
15 isDisabled?: boolean;
16 /** The step passed into the onNavItemClick callback */
17 step: number;
18 /** Callback for when the nav item is clicked */
19 onNavItemClick?: (step: number) => any;
20 /** Component used to render WizardNavItem */
21 navItemComponent?: 'button' | 'a';
22 /** An optional url to use for when using an anchor component */
23 href?: string;
24 /** Flag indicating that this NavItem has child steps and is expandable */
25 isExpandable?: boolean;
26 /** The id for the nav item */
27 id?: string | number;
28 }
29
30 export const WizardNavItem: React.FunctionComponent<WizardNavItemProps> = ({
31 children = null,
32 content = '',
33 isCurrent = false,
34 isDisabled = false,
35 step,
36 onNavItemClick = () => undefined,
37 navItemComponent = 'button',
38 href = null,
39 isExpandable = false,
40 id,
41 ouiaId,
42 ouiaSafe = true,
43 ...rest
44 }: WizardNavItemProps) => {
45 const ouiaProps = useOUIAProps(WizardNavItem.displayName, ouiaId, ouiaSafe);
46 const NavItemComponent = navItemComponent;
47
48 const [isExpanded, setIsExpanded] = useState(false);
49
50 useEffect(() => {
51 setIsExpanded(isCurrent);
52 }, [isCurrent]);
53
54 if (navItemComponent === 'a' && !href && process.env.NODE_ENV !== 'production') {
55 // eslint-disable-next-line no-console
56 console.error('WizardNavItem: When using an anchor, please provide an href');
57 }
58
59 const btnProps = {
60 disabled: isDisabled,
61 type: 'button' as const
62 };
63
64 const linkProps = {
65 tabIndex: isDisabled ? -1 : undefined,
66 href
67 };
68
69 return (
70 <li
71 className={css(
72 styles.wizardNavItem,
73 isExpandable && styles.modifiers.expandable,
74 isExpandable && isExpanded && styles.modifiers.expanded
75 )}
76 >
77 <NavItemComponent
78 {...rest}
79 {...(navItemComponent === 'a' ? { ...linkProps } : { ...btnProps })}
80 {...(id && { id: id.toString() })}
81 onClick={() => (isExpandable ? setIsExpanded(!isExpanded || isCurrent) : onNavItemClick(step))}
82 className={css(
83 styles.wizardNavLink,
84 isCurrent && styles.modifiers.current,
85 isDisabled && styles.modifiers.disabled
86 )}
87 aria-disabled={isDisabled ? true : null}
88 aria-current={isCurrent && !children ? 'step' : false}
89 {...(isExpandable && { 'aria-expanded': isExpanded })}
90 {...ouiaProps}
91 >
92 <span className={css(styles.wizardNavLinkMain)}>
93 {isExpandable ? (
94 <>
95 <span className="pf-v6-c-wizard__nav-link-text">{content}</span>
96 <span className={css(styles.wizardNavLinkToggle)}>
97 <span className={css(styles.wizardNavLinkToggleIcon)}>
98 <RhMicronsCaretDownIcon />
99 </span>
100 </span>
101 </>
102 ) : (
103 content
104 )}
105 </span>
106 </NavItemComponent>
107 {children}
108 </li>
109 );
110 };
111 WizardNavItem.displayName = 'WizardNavItem';