UNPKG

@spaced-out/ui-design-system

Version:
77 lines (72 loc) 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Stepper = void 0; var React = _interopRequireWildcard(require("react")); var _classify = require("../../utils/classify"); var _qa = require("../../utils/qa"); var _StepperModule = _interopRequireDefault(require("./Stepper.module.css")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } // Define the expected props for step elements // Type for a step element that can be cloned const Stepper = exports.Stepper = /*#__PURE__*/React.forwardRef((_ref, ref) => { let { activeStep = 0, children, orientation = 'horizontal', classNames, disabled = false, testId } = _ref; const childrenArray = React.Children.toArray(children).filter(Boolean); const steps = childrenArray.map((step, index) => { let isLastStepCompleted = false; // Type guard to ensure step is a ReactElement if (! /*#__PURE__*/React.isValidElement(step)) { return step; } // Cast to our typed step element const stepElement = step; const stepProps = stepElement.props; const { disabled: disabledChild, onClick } = stepProps; if (index === 0) { isLastStepCompleted = true; } else { // Type guard for previous step const previousStep = childrenArray[index - 1]; if (/*#__PURE__*/React.isValidElement(previousStep)) { const previousStepElement = previousStep; const previousStepProps = previousStepElement?.props; isLastStepCompleted = previousStepProps?.completed || false; } } return /*#__PURE__*/React.cloneElement(stepElement, { ...stepProps, index, last: index + 1 === childrenArray.length, active: index === activeStep, allowClick: onClick && index !== activeStep && isLastStepCompleted, disabled: disabledChild || disabled, testId: (0, _qa.generateTestId)({ base: testId, slot: 'step', index: index.toString() }) }); }); return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: (0, _classify.classify)(_StepperModule.default.stepperWrapper, _StepperModule.default[orientation], classNames?.wrapper), ref: ref, "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'root' }), children: steps }); });