@carbon/ibm-products
Version:
Carbon for IBM Products
291 lines (289 loc) • 11.5 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { __toESM } from "../../_virtual/_rolldown/runtime.js";
import { require_classnames } from "../../node_modules/classnames/index.js";
import { pkg } from "../../settings.js";
import { useCreateComponentFocus } from "../../global/js/hooks/useCreateComponentFocus.js";
import { useCreateComponentStepChange } from "../../global/js/hooks/useCreateComponentStepChange.js";
import { usePreviousValue } from "../../global/js/hooks/usePreviousValue.js";
import { getNumberOfHiddenSteps } from "../../global/js/utils/getNumberOfHiddenSteps.js";
import { useValidCreateStepCount } from "../../global/js/hooks/useValidCreateStepCount.js";
import { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import { SimpleHeader } from "../SimpleHeader/SimpleHeader.js";
import { ActionSet } from "../ActionSet/ActionSet.js";
import { CreateInfluencer } from "../CreateInfluencer/CreateInfluencer.js";
import { lastIndexInArray } from "../../global/js/utils/lastIndexInArray.js";
import React, { createContext, isValidElement, useEffect, useState } from "react";
import PropTypes from "prop-types";
import { Button, ComposedModal, Form, ModalBody, ModalFooter, ModalHeader, Tooltip } from "@carbon/react";
//#region src/components/CreateFullPage/CreateFullPage.tsx
/**
* Copyright IBM Corp. 2021, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const blockClass = `${pkg.prefix}--create-full-page`;
const componentName = "CreateFullPage";
const StepsContext = createContext(null);
const StepNumberContext = createContext(-1);
/**
* Use with creations that must be completed in order for a service to be usable.
*
* ### Grid
*
* The `CreateFullPage` component utilizes Carbons' grid system in the inner
content of the main section inside of the component. You can read more guidance
on the Carbon's grid system
[here](https://www.carbondesignsystem.com/guidelines/2x-grid/overview). You can
include `<Row>` and `<Column>` components inside of each `CreateFullPageStep`
component to get the desired affect.
*/
const CreateFullPage = React.forwardRef(({ breadcrumbsOverflowAriaLabel, breadcrumbs, backButtonText, cancelButtonText, children, className, initialStep, maxVisibleBreadcrumbs, modalDangerButtonText, modalDescription, modalSecondaryButtonText, modalTitle, nextButtonText, onClickInfluencerStep, onClose, onRequestSubmit, firstFocusElement, submitButtonText, noTrailingSlash, title, secondaryTitle, breadcrumbOverflowTooltipAlign = "right", ...rest }, ref) => {
const [createFullPageActions, setCreateFullPageActions] = useState([]);
const [shouldViewAll, setShouldViewAll] = useState(false);
const [currentStep, setCurrentStep] = useState(1);
const [isSubmitting, setIsSubmitting] = useState(false);
const [modalIsOpen, setModalIsOpen] = useState(false);
const previousState = usePreviousValue({ currentStep });
const [isDisabled, setIsDisabled] = useState(false);
const [onPrevious, setOnPrevious] = useState();
const [onNext, setOnNext] = useState();
const [onMount, setOnMount] = useState();
const [stepData, setStepData] = useState([]);
const [firstIncludedStep, setFirstIncludedStep] = useState(1);
const [lastIncludedStep, setLastIncludedStep] = useState();
const stepLength = React.Children.toArray(children).filter((item) => isValidElement(item) && item.props.includeStep !== false).length;
useEffect(() => {
const firstItem = stepData.findIndex((item) => item?.shouldIncludeStep) + 1;
const lastItem = lastIndexInArray(stepData, "shouldIncludeStep", true);
if (firstItem !== firstIncludedStep) setFirstIncludedStep(firstItem);
if (lastItem !== lastIncludedStep) setLastIncludedStep(lastItem);
if (Number(initialStep) > stepLength || Number(initialStep) <= 0) setCurrentStep(1);
else if (initialStep) {
const numberOfHiddenSteps = getNumberOfHiddenSteps(stepData, initialStep);
setCurrentStep(Number(initialStep + numberOfHiddenSteps));
}
}, [
stepData,
firstIncludedStep,
lastIncludedStep,
initialStep,
modalIsOpen,
stepLength
]);
useEffect(() => {
checkForValidInitialStep();
}, [initialStep]);
useCreateComponentFocus({
previousState,
currentStep,
blockClass: `.${blockClass} .${pkg.prefix}--create-full-page__step`,
onMount,
firstFocusElement
});
useValidCreateStepCount(stepData.length, componentName);
useCreateComponentStepChange({
firstIncludedStep,
lastIncludedStep,
stepData,
onPrevious,
onNext,
isSubmitDisabled: isDisabled,
setCurrentStep,
setIsSubmitting,
/**@ts-ignore */
setShouldViewAll,
onClose,
onRequestSubmit,
componentName,
currentStep,
shouldViewAll,
backButtonText,
cancelButtonText,
submitButtonText,
nextButtonText,
isSubmitting,
componentBlockClass: blockClass,
setCreateComponentActions: setCreateFullPageActions,
setModalIsOpen
});
const checkForValidInitialStep = () => {
if (initialStep && stepLength && Number(initialStep) > stepLength || Number(initialStep) <= 0) console.warn(`${componentName}: An invalid \`initialStep\` prop was supplied. The \`initialStep\` prop should be a number that is greater than 0 or less than or equal to the number of steps your ${componentName} has.`);
};
/* istanbul ignore next */
return /* @__PURE__ */ React.createElement("div", {
...rest,
ref,
className: (0, import_classnames.default)(blockClass, className),
...getDevtoolsProps(componentName)
}, (title || breadcrumbs) && /* @__PURE__ */ React.createElement(SimpleHeader, {
title,
breadcrumbs,
noTrailingSlash,
overflowAriaLabel: breadcrumbsOverflowAriaLabel,
maxVisible: maxVisibleBreadcrumbs,
className: `${blockClass}__header`,
overflowTooltipAlign: breadcrumbOverflowTooltipAlign
}), /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__influencer-and-body-container` }, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__influencer` }, /* @__PURE__ */ React.createElement(CreateInfluencer, {
stepData,
currentStep,
title: secondaryTitle,
onClickStep: onClickInfluencerStep
})), /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__body` }, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__main` }, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__content` }, /* @__PURE__ */ React.createElement(Form, {
className: `${blockClass}__form`,
"aria-label": title,
onSubmit: (e) => e.preventDefault()
}, /* @__PURE__ */ React.createElement(StepsContext.Provider, { value: {
currentStep,
setIsDisabled,
setOnPrevious: (fn) => setOnPrevious(() => fn),
setOnNext: (fn) => setOnNext(() => fn),
setOnMount: (fn) => setOnMount(() => fn),
setStepData,
stepData
} }, React.Children.toArray(children).filter(Boolean).map((child, index) => /* @__PURE__ */ React.createElement(StepNumberContext.Provider, {
value: index + 1,
key: index
}, child))))), /* @__PURE__ */ React.createElement(ActionSet, {
className: `${blockClass}__buttons`,
actions: createFullPageActions,
buttonSize: "2xl",
size: "2xl"
}))), /* @__PURE__ */ React.createElement(ComposedModal, {
className: `${blockClass}__modal`,
size: "sm",
open: modalIsOpen,
"aria-label": modalTitle,
onClose: () => {
setModalIsOpen(false);
}
}, /* @__PURE__ */ React.createElement(ModalHeader, { title: modalTitle }), /* @__PURE__ */ React.createElement(ModalBody, null, /* @__PURE__ */ React.createElement("p", null, modalDescription)), /* @__PURE__ */ React.createElement(ModalFooter, null, /* @__PURE__ */ React.createElement(Button, {
type: "button",
kind: "secondary",
onClick: () => {
setModalIsOpen(!modalIsOpen);
},
"data-modal-primary-focus": true
}, modalSecondaryButtonText), /* @__PURE__ */ React.createElement(Button, {
type: "button",
kind: "danger",
onClick: onClose
}, modalDangerButtonText)))));
});
CreateFullPage.displayName = componentName;
CreateFullPage.propTypes = {
/**
* The back button text
*/
backButtonText: PropTypes.string.isRequired,
/**
* align breadcrumb overflow tooltip
*/
breadcrumbOverflowTooltipAlign: Tooltip.propTypes.align,
/** The header breadcrumbs */
/**@ts-ignore */
breadcrumbs: PropTypes.arrayOf(PropTypes.shape({
/** breadcrumb item key */
key: PropTypes.string.isRequired,
/** breadcrumb item label */
label: PropTypes.string.isRequired,
/** breadcrumb item link */
href: PropTypes.string,
/** breadcrumb item title tooltip */
title: PropTypes.string,
/** Provide if this breadcrumb item represents the current page */
isCurrentPage: PropTypes.bool
})),
/**
* Label for open/close overflow button used for breadcrumb items that do not fit
*/
breadcrumbsOverflowAriaLabel: PropTypes.string,
/**
* The cancel button text
*/
cancelButtonText: PropTypes.string.isRequired,
/**
* The main content of the full page
*/
children: PropTypes.node,
/**
* Provide an optional class to be applied to the containing node.
*/
className: PropTypes.string,
/**
* Specifies elements to focus on first on render.
*/
firstFocusElement: PropTypes.string,
/**
* This can be used to open the component to a step other than the first step.
* For example, a create flow was previously in progress, data was saved, and
* is now being completed.
*/
initialStep: PropTypes.number,
/** Maximum visible breadcrumb-items before overflow is used (values less than 1 are treated as 1) */
maxVisibleBreadcrumbs: PropTypes.number,
/**
* The primary 'danger' button text in the modal
*/
modalDangerButtonText: PropTypes.string.isRequired,
/**
* The description located below the title in the modal
*/
modalDescription: PropTypes.string,
/**
* The secondary button text in the modal
*/
modalSecondaryButtonText: PropTypes.string.isRequired,
/**
* The title located in the header of the modal
*/
modalTitle: PropTypes.string.isRequired,
/**
* The next button text
*/
nextButtonText: PropTypes.string.isRequired,
/**
* A prop to omit the trailing slash for the breadcrumbs
*/
noTrailingSlash: PropTypes.bool,
/**
* onChange event for Progress Indicator in the Influencer
*/
onClickInfluencerStep: PropTypes.func,
/**
* An optional handler that is called when the user closes the full page (by
* clicking the secondary button, located in the modal, which triggers after
* clicking the ghost button in the modal
*/
onClose: PropTypes.func,
/**
* Specify a handler for submitting the multi step full page (final step).
* This function can _optionally_ return a promise that is either resolved or rejected and the CreateFullPage will handle the submitting state of the create button.
*/
onRequestSubmit: PropTypes.func.isRequired,
/**
* A secondary title of the full page, displayed in the influencer area
*/
secondaryTitle: PropTypes.string,
/**
* @ignore
* The aria label to be used for the UI Shell SideNav Carbon component
*/
sideNavAriaLabel: PropTypes.string,
/**
* The submit button text
*/
submitButtonText: PropTypes.string.isRequired,
/**
* The main title of the full page, displayed in the header area
*/
title: PropTypes.string
};
//#endregion
export { CreateFullPage, StepNumberContext, StepsContext };