UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

144 lines (143 loc) 4.78 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = require("react"); var _clsx = _interopRequireDefault(require("clsx")); var _WizardContext = _interopRequireDefault(require("../Context/WizardContext.js")); var _StepContext = _interopRequireDefault(require("./StepContext.js")); var _Flex = _interopRequireDefault(require("../../../../components/flex/Flex.js")); var _componentHelper = require("../../../../shared/component-helper.js"); var _index = _interopRequireDefault(require("../../Field/Provider/index.js")); var _withComponentMarkers = _interopRequireDefault(require("../../../../shared/helpers/withComponentMarkers.js")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function Step(props) { const { id, className, title, index: indexProp, inactive, include = true, includeWhen, required, keepInDOM: keepInDOMProp, prerenderFieldProps, children, ...restProps } = props; const { check, enableMapOverChildren, activeIndex, totalStepsRef, stepElementRef, stepIndexRef, stepsRef, keepInDOM } = (0, _react.useContext)(_WizardContext.default) || {}; const wizardStepContext = (0, _react.useContext)(_StepContext.default) || {}; const indexFromContext = wizardStepContext.index; const totalSteps = totalStepsRef === null || totalStepsRef === void 0 ? void 0 : totalStepsRef.current; const ariaLabel = (0, _react.useMemo)(() => (0, _componentHelper.convertJsxToString)(title), [title]); const index = (0, _react.useMemo)(() => { if (indexProp !== undefined) { return indexProp; } if (indexFromContext !== undefined) { return indexFromContext; } if (stepsRef !== null && stepsRef !== void 0 && stepsRef.current) { const { index } = Array.from(stepsRef.current.values()).find(({ id: currentId, title: originalTitleProp, stringifiedTitle }) => { var _originalTitleProp$pr; if (id !== undefined) { return id === currentId; } const translationId = originalTitleProp === null || originalTitleProp === void 0 || (_originalTitleProp$pr = originalTitleProp['props']) === null || _originalTitleProp$pr === void 0 ? void 0 : _originalTitleProp$pr.id; if (translationId) { var _title$props; return translationId === (title === null || title === void 0 || (_title$props = title['props']) === null || _title$props === void 0 ? void 0 : _title$props.id); } if (stringifiedTitle) { return stringifiedTitle === ariaLabel; } return originalTitleProp === title; }) || {}; if (index !== undefined) { return index; } } if (totalSteps && stepIndexRef) { stepIndexRef.current += 1; } return stepIndexRef === null || stepIndexRef === void 0 ? void 0 : stepIndexRef.current; }, [indexProp, stepsRef, totalSteps, stepIndexRef, indexFromContext, title, id, ariaLabel]); if (!prerenderFieldProps && index >= totalSteps) { enableMapOverChildren(); } if (include === false) { return null; } if (includeWhen && !check({ visibleWhen: includeWhen })) { return null; } const fieldProps = typeof required === 'boolean' ? { required } : undefined; const childrenWithFieldProvider = fieldProps ? (0, _jsxRuntime.jsx)(_index.default, { ...fieldProps, children: children }) : children; if (prerenderFieldProps) { return (0, _jsxRuntime.jsx)(_StepContext.default, { value: { index }, children: childrenWithFieldProvider }); } const stepRef = activeIndex === index ? stepElementRef : null; const childrenWithFlex = (0, _jsxRuntime.jsx)(_StepContext.default, { value: { index }, children: (0, _jsxRuntime.jsx)(_Flex.default.Stack, { className: (0, _clsx.default)('dnb-forms-step', className), element: "section", "aria-label": ariaLabel, ref: stepRef, tabIndex: -1, ...restProps, children: childrenWithFieldProvider }) }); if (activeIndex !== index || includeWhen && !check({ visibleWhen: includeWhen })) { if (keepInDOMProp !== null && keepInDOMProp !== void 0 ? keepInDOMProp : keepInDOM) { return (0, _jsxRuntime.jsx)("div", { title: "Wizard Step", hidden: true, children: childrenWithFlex }); } return null; } return childrenWithFlex; } (0, _withComponentMarkers.default)(Step, { _supportsSpacingProps: true }); var _default = exports.default = Step; //# sourceMappingURL=Step.js.map