@carbon/ibm-products
Version:
Carbon for IBM Products
348 lines (335 loc) • 13 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 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.
*/
'use strict';
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var react = require('@carbon/react');
var React = require('react');
var SimpleHeader = require('../SimpleHeader/SimpleHeader.js');
var ActionSet = require('../ActionSet/ActionSet.js');
var index = require('../../_virtual/index.js');
var cx = require('classnames');
var devtools = require('../../global/js/utils/devtools.js');
var getNumberOfHiddenSteps = require('../../global/js/utils/getNumberOfHiddenSteps.js');
var lastIndexInArray = require('../../global/js/utils/lastIndexInArray.js');
var settings = require('../../settings.js');
var usePreviousValue = require('../../global/js/hooks/usePreviousValue.js');
var useCreateComponentFocus = require('../../global/js/hooks/useCreateComponentFocus.js');
var useValidCreateStepCount = require('../../global/js/hooks/useValidCreateStepCount.js');
var useCreateComponentStepChange = require('../../global/js/hooks/useCreateComponentStepChange.js');
var CreateInfluencer = require('../CreateInfluencer/CreateInfluencer.js');
const blockClass = `${settings.pkg.prefix}--create-full-page`;
const componentName = 'CreateFullPage';
// This is a general context for the steps container
// containing information about the state of the container
// and providing some callback methods for steps to use
const StepsContext = /*#__PURE__*/React.createContext(null);
// This is a context supplied separately to each step in the container
// to let it know what number it is in the sequence of steps
const StepNumberContext = /*#__PURE__*/React.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.
*/
exports.CreateFullPage = /*#__PURE__*/React.forwardRef((_ref, ref) => {
let {
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] = React.useState([]);
const [shouldViewAll, setShouldViewAll] = React.useState(false);
const [currentStep, setCurrentStep] = React.useState(1);
const [isSubmitting, setIsSubmitting] = React.useState(false);
const [modalIsOpen, setModalIsOpen] = React.useState(false);
const previousState = usePreviousValue.usePreviousValue({
currentStep
});
const [isDisabled, setIsDisabled] = React.useState(false);
const [onPrevious, setOnPrevious] = React.useState();
const [onNext, setOnNext] = React.useState();
const [onMount, setOnMount] = React.useState();
const [stepData, setStepData] = React.useState([]);
const [firstIncludedStep, setFirstIncludedStep] = React.useState(1);
const [lastIncludedStep, setLastIncludedStep] = React.useState();
const invalidInitialStepWarned = React.useRef(false);
React.useEffect(() => {
const firstItem = stepData.findIndex(item => item?.shouldIncludeStep) + 1;
const lastItem = lastIndexInArray.lastIndexInArray(stepData, 'shouldIncludeStep', true);
if (firstItem !== firstIncludedStep) {
setFirstIncludedStep(firstItem);
}
if (lastItem !== lastIncludedStep) {
setLastIncludedStep(lastItem);
}
/**@ts-ignore */
if (initialStep) {
const numberOfHiddenSteps = getNumberOfHiddenSteps.getNumberOfHiddenSteps(stepData, initialStep);
setCurrentStep(Number(initialStep + numberOfHiddenSteps));
}
}, [stepData, firstIncludedStep, lastIncludedStep, initialStep, modalIsOpen]);
React.useEffect(() => {
if (!invalidInitialStepWarned?.current) {
checkForValidInitialStep();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [initialStep, stepData]);
useCreateComponentFocus.useCreateComponentFocus({
previousState,
currentStep,
blockClass,
onMount,
firstFocusElement
});
useValidCreateStepCount.useValidCreateStepCount(stepData.length, componentName);
useCreateComponentStepChange.useCreateComponentStepChange({
firstIncludedStep,
lastIncludedStep,
stepData,
onPrevious,
onNext,
isSubmitDisabled: isDisabled,
setCurrentStep,
setIsSubmitting,
onClose,
onRequestSubmit,
componentName,
currentStep,
backButtonText,
cancelButtonText,
submitButtonText,
nextButtonText,
isSubmitting,
componentBlockClass: blockClass,
setCreateComponentActions: setCreateFullPageActions,
setModalIsOpen
});
const checkForValidInitialStep = () => {
// An invalid initialStep value was provided, we'll default to rendering the first step or last step
if (initialStep && stepData?.length && Number(initialStep) > Number(stepData?.length) || Number(initialStep) <= 0) {
invalidInitialStepWarned.current = true;
setCurrentStep(1);
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.`);
}
};
// currently, we are not supporting the use of 'view all' toggle state
/* istanbul ignore next */
return /*#__PURE__*/React.createElement("div", _rollupPluginBabelHelpers.extends({}, rest, {
ref: ref,
className: cx(blockClass, className)
}, devtools.getDevtoolsProps(componentName)), (title || breadcrumbs) && /*#__PURE__*/React.createElement(SimpleHeader.SimpleHeader, {
title: title,
breadcrumbs: breadcrumbs,
noTrailingSlash: 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.CreateInfluencer, {
stepData: stepData,
currentStep: 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(react.Form, {
className: `${blockClass}__form`,
"aria-label": title
}, /*#__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.ActionSet, {
className: `${blockClass}__buttons`,
actions: createFullPageActions,
buttonSize: "2xl",
size: "2xl"
}))), /*#__PURE__*/React.createElement(react.ComposedModal, {
className: `${blockClass}__modal`,
size: "sm",
open: modalIsOpen,
"aria-label": modalTitle,
onClose: () => {
setModalIsOpen(false);
}
}, /*#__PURE__*/React.createElement(react.ModalHeader, {
title: modalTitle
}), /*#__PURE__*/React.createElement(react.ModalBody, null, /*#__PURE__*/React.createElement("p", null, modalDescription)), /*#__PURE__*/React.createElement(react.ModalFooter, null, /*#__PURE__*/React.createElement(react.Button, {
type: "button",
kind: "secondary",
onClick: () => {
setModalIsOpen(!modalIsOpen);
},
"data-modal-primary-focus": true
}, modalSecondaryButtonText), /*#__PURE__*/React.createElement(react.Button, {
type: "button",
kind: "danger",
onClick: onClose
}, modalDangerButtonText)))));
});
// Return a placeholder if not released and not enabled by feature flag.
exports.CreateFullPage = settings.pkg.checkComponentEnabled(exports.CreateFullPage, componentName);
// The display name of the component, used by React. Note that displayName
// is used in preference to relying on function.name.
exports.CreateFullPage.displayName = componentName;
// The types and DocGen commentary for the component props,
// in alphabetical order (for consistency).
// See https://www.npmjs.com/package/prop-types#usage.
exports.CreateFullPage.propTypes = {
/**
* The back button text
*/
backButtonText: index.default.string.isRequired,
/**
* align breadcrumb overflow tooltip
*/
breadcrumbOverflowTooltipAlign: react.Tooltip.propTypes.align,
/** The header breadcrumbs */
/**@ts-ignore */
breadcrumbs: index.default.arrayOf(index.default.shape({
/** breadcrumb item key */
key: index.default.string.isRequired,
/** breadcrumb item label */
label: index.default.string.isRequired,
/** breadcrumb item link */
href: index.default.string,
/** breadcrumb item title tooltip */
title: index.default.string,
/** Provide if this breadcrumb item represents the current page */
isCurrentPage: index.default.bool
})),
/**
* Label for open/close overflow button used for breadcrumb items that do not fit
*/
breadcrumbsOverflowAriaLabel: SimpleHeader.overflowAriaLabel_required_if_breadcrumbs_exist,
/**
* The cancel button text
*/
cancelButtonText: index.default.string.isRequired,
/**
* The main content of the full page
*/
children: index.default.node,
/**
* Provide an optional class to be applied to the containing node.
*/
className: index.default.string,
/**
* Specifies elements to focus on first on render.
*/
firstFocusElement: index.default.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: index.default.number,
/** Maximum visible breadcrumb-items before overflow is used (values less than 1 are treated as 1) */
maxVisibleBreadcrumbs: index.default.number,
/**
* The primary 'danger' button text in the modal
*/
modalDangerButtonText: index.default.string.isRequired,
/**
* The description located below the title in the modal
*/
modalDescription: index.default.string,
/**
* The secondary button text in the modal
*/
modalSecondaryButtonText: index.default.string.isRequired,
/**
* The title located in the header of the modal
*/
modalTitle: index.default.string.isRequired,
/**
* The next button text
*/
nextButtonText: index.default.string.isRequired,
/**
* A prop to omit the trailing slash for the breadcrumbs
*/
noTrailingSlash: index.default.bool,
/**
* onChange event for Progress Indicator in the Influencer
*/
onClickInfluencerStep: index.default.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: index.default.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: index.default.func.isRequired,
/**
* A secondary title of the full page, displayed in the influencer area
*/
secondaryTitle: index.default.string,
/**
* @ignore
* The aria label to be used for the UI Shell SideNav Carbon component
*/
sideNavAriaLabel: index.default.string,
/**
* The submit button text
*/
submitButtonText: index.default.string.isRequired,
/**
* The main title of the full page, displayed in the header area
*/
title: index.default.string
};
exports.StepNumberContext = StepNumberContext;
exports.StepsContext = StepsContext;