Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Wizard/utils.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.isStepEnabled = exports.normalizeStepProps = exports.isWizardStep = exports.buildSteps = void 0;
4 const tslib_1 = require("tslib");
5 const react_1 = require("react");
6 const types_1 = require("./types");
7 const WizardStep_1 = require("./WizardStep");
8 /**
9 * Accumulate list of step & sub-step props pulled from child components
10 * @param children
11 * @returns WizardStepType[]
12 */
13 const buildSteps = (children) => react_1.Children.toArray(children).reduce((acc, child, index) => {
14 if (isWizardStep(child)) {
15 const { props: childProps } = child;
16 const { steps: childStepComponents, id } = childProps;
17 const stepIndex = acc.length + 1;
18 const subSteps = [];
19 acc.push(Object.assign(Object.assign(Object.assign({ index: stepIndex, component: child }, (index === 0 && !(childStepComponents === null || childStepComponents === void 0 ? void 0 : childStepComponents.length) && { isVisited: true })), (childStepComponents && {
20 subStepIds: childStepComponents === null || childStepComponents === void 0 ? void 0 : childStepComponents.map((childStepComponent, subStepIndex) => {
21 subSteps.push(Object.assign(Object.assign({ index: stepIndex + subStepIndex + 1, component: childStepComponent, parentId: id }, (index === 0 && subStepIndex === 0 && { isVisited: true })), (0, exports.normalizeStepProps)(childStepComponent.props)));
22 return childStepComponent.props.id;
23 })
24 })), (0, exports.normalizeStepProps)(childProps)), ...subSteps);
25 }
26 else {
27 throw new Error('Wizard only accepts children of type WizardStep.');
28 }
29 return acc;
30 }, []);
31 exports.buildSteps = buildSteps;
32 function isWizardStep(child) {
33 return (0, react_1.isValidElement)(child) && child.type === WizardStep_1.WizardStep;
34 }
35 exports.isWizardStep = isWizardStep;
36 // Omit "children" and "steps" when building steps for the Wizard's context
37 const normalizeStepProps = (_a) => {
38 var { children: _children, steps: _steps } = _a, controlStep = tslib_1.__rest(_a, ["children", "steps"]);
39 return controlStep;
40 };
41 exports.normalizeStepProps = normalizeStepProps;
42 /**
43 * Determines whether a step is navigable based on disabled/hidden properties
44 * @param steps All steps
45 * @param step
46 * @returns boolean
47 */
48 const isStepEnabled = (steps, step) => {
49 // Skip over parent steps since they are merely containers of sub-steps
50 if (!(0, types_1.isWizardParentStep)(step) && !step.isHidden && !step.isDisabled) {
51 if ((0, types_1.isWizardSubStep)(step)) {
52 const parentStep = steps.find((otherStep) => otherStep.id === step.parentId);
53 if (!parentStep.isHidden && !parentStep.isDisabled) {
54 return true;
55 }
56 }
57 else {
58 return true;
59 }
60 }
61 return false;
62 };
63 exports.isStepEnabled = isStepEnabled;
64 //# sourceMappingURL=utils.js.map