@octopusdeploy/design-system-components
Version:
The design systems component library.
111 lines (110 loc) • 5.89 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.RadioButtonGroupContext = void 0;
exports.RadioButtonGroup = RadioButtonGroup;
const jsx_runtime_1 = require("react/jsx-runtime");
const css_1 = require("@emotion/css");
const material_1 = require("@mui/material");
const design_system_tokens_1 = require("@octopusdeploy/design-system-tokens");
const React = __importStar(require("react"));
const FormFieldTitle_1 = require("../FormFieldTitle");
exports.RadioButtonGroupContext = React.createContext(undefined);
/* @deprecated
* Deprecated: 18/08/2025
* Migration guidelines: This component is deprecated and will be removed in a future release.
* This is part of our ongoing improvements to forms and input components to enhance usability and accessibility.
*
* Please migrate to the RadioGroupNew component where possible.
*
* We recognize RadioGroup has different styling; if that prevents an immediate swap, you may continue using this
* component until all the new form components are in place.
*/
function RadioButtonGroup({ className, value, onChange, title, children, error, horizontal, noMargin, accessibleName, autoFocus }) {
const [showExternalError, setShowExternalError] = React.useState(true);
const [previousError, setPreviousError] = React.useState(error);
if (error !== previousError) {
setPreviousError(error);
setShowExternalError(true);
}
const handleChange = React.useCallback((event, value) => {
setShowExternalError(false);
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
onChange?.(value);
}, [onChange]);
const groupContextValue = React.useMemo(() => ({
value,
autoFocus: autoFocus ?? false,
}), [value, autoFocus]);
/**
* Some usages of the RadioButtonGroup pass in value as "undefined" on first render, then switches to a proper value when loaded.
* In MUI 0, this would result in the RadioButtonGroup value defaulting to the first option's value, then switching to whatever value was loaded.
* In MUI 4, this doesn't work as it chooses between controlled and uncontrolled mode depending on whether the value is undefined, and throws an error if it changes.
* Ideally consumers would be updated to always pass in a valid value, however a lot of process editor step properties are typed as string, but can be undefined so it's difficult to identify probelmatic usages.
* Using the key is a hacky workaround to forcibly remount the RadioGroup if the controlled mode changes and the value becomes selected.
*/
const key = value === undefined ? "uncontrolled" : "controlled";
return ((0, jsx_runtime_1.jsx)(exports.RadioButtonGroupContext.Provider, { value: groupContextValue, children: (0, jsx_runtime_1.jsxs)("div", { className: (0, css_1.cx)(containerStyles, { [containerMarginStyles]: !noMargin }), children: [title && (0, jsx_runtime_1.jsx)(FormFieldTitle_1.FormFieldTitle, { title: title }), (0, jsx_runtime_1.jsx)(material_1.RadioGroup, { "aria-label": accessibleName, className: (0, css_1.cx)(radioGroupStyles, { [radioGroupHorizontalStyles]: horizontal }, className), onChange: handleChange, value: value, children: children }), showExternalError && error && (0, jsx_runtime_1.jsx)("div", { className: errorTextStyles, children: error })] }, key) }));
}
const muiFormGroupClasses = {
root: "MuiFormGroup-root",
};
const containerStyles = (0, css_1.css)({
width: "100%",
});
const containerMarginStyles = (0, css_1.css)({ marginTop: design_system_tokens_1.space[16], marginBottom: design_system_tokens_1.space[16] });
const radioGroupStyles = (0, css_1.css)({
[`&.${muiFormGroupClasses.root}`]: {
display: "flex",
flexDirection: "column",
flexWrap: "wrap",
},
// This is not ideal as it is has knowledge about how RadioGroup is used
// This adds space on the left of non-radiobutton elements (e.g. <Note />) so they line up with the radiobutton label
"> :not(.MuiFormControlLabel-root)": {
marginLeft: design_system_tokens_1.space[32],
},
});
const radioGroupHorizontalStyles = (0, css_1.css)({
[`&.${muiFormGroupClasses.root}`]: {
flexDirection: "row",
gap: design_system_tokens_1.space[48],
},
});
const errorTextStyles = (0, css_1.css)({
marginTop: design_system_tokens_1.space[4],
font: design_system_tokens_1.text.body.regular.xSmall,
color: design_system_tokens_1.themeTokens.color.text.danger,
});