@octopusdeploy/design-system-components
Version:
The design systems component library.
187 lines (186 loc) • 8.77 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.CheckboxNew = CheckboxNew;
const jsx_runtime_1 = require("react/jsx-runtime");
const css_1 = require("@emotion/css");
const design_system_icons_1 = require("@octopusdeploy/design-system-icons");
const design_system_tokens_1 = require("@octopusdeploy/design-system-tokens");
const React = __importStar(require("react"));
const FormDescription_1 = require("../Primitives/FormDescription");
const InputLabel_1 = require("../Primitives/InputLabel");
const CheckboxGroupNew_1 = require("./CheckboxGroupNew");
/**
* CheckboxNew component
*/
function CheckboxNew(props) {
const { description, disabled, isIndeterminate, value, name, autoFocus, hasDefaultMarker, popover, onChange } = props;
const label = "label" in props && props.label !== undefined ? props.label : undefined;
const hasLabel = label !== undefined;
const ariaLabel = hasLabel ? undefined : "ariaLabel" in props ? props.ariaLabel : undefined;
const context = React.useContext(CheckboxGroupNew_1.CheckboxGroupContext);
const inputRef = React.useRef(null);
/* eslint-disable @typescript-eslint/consistent-type-assertions */
const handleChange = React.useCallback((event) => {
const isChecked = event.target.checked;
if (onChange) {
const newValue = isChecked ? (typeof value === "string" ? "enabled" : true) : typeof value === "string" ? "" : false;
onChange(newValue);
return;
}
if (!context?.onChange)
return;
const currentValues = context.value;
const newValues = isChecked ? [...currentValues, value] : currentValues.filter((v) => v !== value);
context.onChange(newValues);
}, [context, value, onChange]);
/* eslint-enable @typescript-eslint/consistent-type-assertions */
const checked = React.useMemo(() => {
if (onChange) {
return value;
}
if (!context?.value)
return false;
return Array.isArray(context.value) && context.value.includes(value);
}, [context?.value, value, onChange]);
const isDisabled = disabled || context?.disabled;
const baseId = React.useId();
const inputId = `checkbox-${name}-${baseId}`;
const getAriaDescribedBy = () => {
const ids = [description && `${inputId}-description`, context?.error && `${context.groupId}-error`].filter(Boolean);
return ids.length > 0 ? ids.join(" ") : undefined;
};
React.useEffect(() => {
if (inputRef.current) {
inputRef.current.indeterminate = Boolean(isIndeterminate);
}
}, [isIndeterminate]);
return ((0, jsx_runtime_1.jsxs)("div", { className: checkboxFieldStyles, children: [(0, jsx_runtime_1.jsxs)("div", { className: checkboxWrapperStyles, children: [(0, jsx_runtime_1.jsx)("input", { ref: inputRef, type: "checkbox", name: name || context?.name, value: String(value), disabled: isDisabled, className: checkboxInputStyles, checked: Boolean(checked), onChange: handleChange, id: inputId, autoFocus: autoFocus, "aria-checked": isIndeterminate ? "mixed" : checked ? "true" : "false", "aria-label": ariaLabel, "aria-describedby": !hasLabel ? getAriaDescribedBy() : undefined }), (0, jsx_runtime_1.jsx)("div", { className: checkboxIconStyles, children: isIndeterminate ? (0, jsx_runtime_1.jsx)(design_system_icons_1.MinusIcon, { size: 16, color: "currentColor" }) : (0, jsx_runtime_1.jsx)(design_system_icons_1.CheckIcon, { size: 16, color: "currentColor" }) })] }), hasLabel && (0, jsx_runtime_1.jsx)(InputLabel_1.InputLabel, { label: label, htmlFor: inputId, isRadioOrCheckbox: true, isDisabled: isDisabled, hasDefaultMarker: hasDefaultMarker, popover: popover, "aria-describedby": getAriaDescribedBy() }), description && (0, jsx_runtime_1.jsx)(FormDescription_1.FormDescription, { id: `${inputId}-description`, description: description, isDisabled: isDisabled, isRadioOrCheckbox: true })] }));
}
const checkboxButtonSize = "1.25rem";
const checkboxFieldStyles = (0, css_1.css)({
display: "grid",
gridTemplateColumns: `${checkboxButtonSize} auto`,
columnGap: design_system_tokens_1.space[8],
alignItems: "center",
margin: 0,
// Add margin bottom between checkboxes
"&:has(+ &), & ~ &": {
marginBottom: design_system_tokens_1.space[8],
},
});
const checkboxWrapperStyles = (0, css_1.css)({
position: "relative",
});
const borderWidthValue = design_system_tokens_1.borderWidth[1];
const checkboxInputStyles = (0, css_1.css)({
display: "grid",
placeContent: "center",
margin: 0,
appearance: "none",
width: checkboxButtonSize,
height: checkboxButtonSize,
backgroundColor: design_system_tokens_1.themeTokens.color.background.primary.default,
border: `${borderWidthValue} solid ${design_system_tokens_1.themeTokens.color.border.bold}`,
borderRadius: design_system_tokens_1.borderRadius.extraSmall,
cursor: "pointer",
font: "inherit",
color: "currentcolor",
"&:before": {
content: `""`,
position: "absolute",
top: 0,
left: 0,
width: `calc(100% - (${borderWidthValue} * 2))`,
height: `calc(100% - (${borderWidthValue} * 2))`,
backgroundColor: design_system_tokens_1.themeTokens.color.background.selected,
// It needs to be 1px as its nested inside a 2px radius. Have flagged for design token.
borderRadius: "1px",
scale: 0,
translate: "1px 1px",
transformOrigin: "center center",
opacity: 0,
transition: "scale 100ms ease-in, opacity 100ms ease-in",
"@media (prefers-reduced-motion: reduce)": {
transition: "none",
},
},
"&:checked:before, &:indeterminate:before": {
scale: "1",
opacity: 1,
transition: "scale 100ms ease-out, opacity 100ms ease-out",
"@media (prefers-reduced-motion: reduce)": {
transition: "none",
},
},
"&:checked, &:indeterminate": {
borderColor: design_system_tokens_1.themeTokens.color.border.selected,
},
"&:disabled": {
backgroundColor: design_system_tokens_1.themeTokens.color.background.disabled,
cursor: "not-allowed",
},
"&:checked:disabled, &:indeterminate:disabled": {
borderColor: design_system_tokens_1.themeTokens.color.border.bold,
},
"&:checked:disabled:before, &:indeterminate:disabled:before": {
backgroundColor: design_system_tokens_1.themeTokens.color.background.disabled,
},
"&:focus": {
outline: `max(2px, 0.15em) solid ${design_system_tokens_1.themeTokens.color.border.selected}`,
outlineOffset: `max(2px, 0.15em)`,
},
});
const checkboxIconStyles = (0, css_1.css)({
display: "grid",
placeContent: "center",
position: "absolute",
top: 0,
width: checkboxButtonSize,
height: checkboxButtonSize,
opacity: 0,
color: design_system_tokens_1.themeTokens.color.icon.inverse.primary,
pointerEvents: "none",
transition: "opacity 100ms ease-in-out",
"@media (prefers-reduced-motion: reduce)": {
transition: "none",
},
"input:checked + &, input:indeterminate + &": {
opacity: 1,
},
"input:disabled:checked + &, input:disabled:indeterminate + &": {
color: design_system_tokens_1.themeTokens.color.icon.tertiary,
},
});