@octopusdeploy/design-system-components
Version:
The design systems component library.
127 lines (126 loc) • 7 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.Input = Input;
const jsx_runtime_1 = require("react/jsx-runtime");
const css_1 = require("@emotion/css");
const design_system_tokens_1 = require("@octopusdeploy/design-system-tokens");
const React = __importStar(require("react"));
const useObserveElementSize_1 = require("../../../hooks/useObserveElementSize");
const SharedFormStyling_styles_1 = require("../Primitives/SharedFormStyling.styles");
function Input(props) {
const { id, value, placeholder, onChange, disabled = false, required = false, type = "text", autoFocus = false, readOnly = false, name, prefix, suffix, "aria-describedby": ariaDescribedBy, "aria-invalid": ariaInvalid } = props;
// min, max, step only when type is "number"
const min = props.type === "number" ? props.min : undefined;
const max = props.type === "number" ? props.max : undefined;
const step = props.type === "number" ? props.step : undefined;
const handleChange = React.useCallback((event) => {
if (onChange) {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
onChange(event.target.value);
}
}, [onChange]);
// React useId adds colons which are not valid in css custom properties
const sanitisedInputId = id.replace(/[:;]/g, "");
const isButtonSuffix = React.isValidElement(suffix);
// Simple text truncation for prefix and suffix
const suffixText = typeof suffix === "string" ? (suffix.length > 4 ? suffix.substring(0, 4) : suffix) : null;
const containerRef = React.useRef(null);
const [prefixElement, setPrefixElement] = React.useState(null);
const [suffixElement, setSuffixElement] = React.useState(null);
// Determines the width of the prefix and suffix and sets a custom property.
// This ensures that the size is correct regardless of the prefix/suffic length or content
(0, useObserveElementSize_1.useObserveElementSize)(prefixElement, ({ width }) => {
containerRef.current?.style.setProperty(`--${sanitisedInputId}-prefix-width`, `${width}px`);
}, () => containerRef.current?.style.removeProperty(`--${sanitisedInputId}-prefix-width`));
(0, useObserveElementSize_1.useObserveElementSize)(suffixElement, ({ width }) => {
containerRef.current?.style.setProperty(`--${sanitisedInputId}-suffix-width`, `${width}px`);
}, () => containerRef.current?.style.removeProperty(`--${sanitisedInputId}-suffix-width`));
const inputPaddingStyles = {
paddingLeft: prefix ? `var(--${sanitisedInputId}-prefix-width)` : undefined,
paddingRight: suffix ? `var(--${sanitisedInputId}-suffix-width)` : undefined,
};
return ((0, jsx_runtime_1.jsxs)("div", { ref: containerRef, className: (0, css_1.cx)(inputContainerStyles), children: [prefix && ((0, jsx_runtime_1.jsx)("div", { ref: setPrefixElement, className: (0, css_1.cx)(inputPrefixStyles), children: prefix ? ((0, jsx_runtime_1.jsx)("p", { className: prefixSuffixTextStyles, title: typeof prefix === "string" ? prefix : undefined, children: prefix })) : (prefix) })), (0, jsx_runtime_1.jsx)("input", { id: id, type: type, name: name, value: value, placeholder: placeholder, disabled: disabled, readOnly: readOnly, "aria-readonly": readOnly, "aria-required": required, autoFocus: autoFocus, min: min, max: max, step: step, className: (0, css_1.cx)(textFieldStyles, SharedFormStyling_styles_1.inputStyles, {
[SharedFormStyling_styles_1.inputErrorStyles]: !!ariaInvalid,
[SharedFormStyling_styles_1.inputDisabledStyles]: disabled,
[SharedFormStyling_styles_1.inputReadOnlyStyles]: readOnly,
}), style: inputPaddingStyles, onChange: handleChange, "aria-invalid": !!ariaInvalid, "aria-describedby": ariaDescribedBy }), suffix && ((0, jsx_runtime_1.jsx)("div", { ref: setSuffixElement, className: (0, css_1.cx)(inputSuffixStyles, {
[inputSuffixButtonStyles]: isButtonSuffix,
}), children: suffixText ? ((0, jsx_runtime_1.jsx)("p", { className: prefixSuffixTextStyles, title: typeof suffix === "string" ? suffix : undefined, children: suffixText })) : (suffix) }))] }));
}
const inputContainerStyles = (0, css_1.css)({
position: "relative",
display: "grid",
gridTemplateColumns: "minmax(0, max-content) 1fr minmax(0, max-content)",
isolation: "isolate",
});
const textFieldStyles = (0, css_1.css)({
minHeight: "2rem",
placeContent: "center",
gridColumn: "1 / -1",
gridRow: 1,
textOverflow: "ellipsis",
whiteSpace: "nowrap",
overflow: "hidden",
});
const inputPrefixStyles = (0, css_1.css)({
font: design_system_tokens_1.text.body.regular.large,
color: design_system_tokens_1.themeTokens.color.text.secondary,
gridColumn: "1 / 2",
gridRow: 1,
placeContent: "center",
padding: `0 ${design_system_tokens_1.space[6]} 0 ${design_system_tokens_1.space[8]}`,
zIndex: 1,
});
const inputSuffixStyles = (0, css_1.css)({
font: design_system_tokens_1.text.body.regular.large,
color: design_system_tokens_1.themeTokens.color.text.secondary,
gridColumn: "3 / 4",
gridRow: 1,
placeContent: "center",
padding: `0 ${design_system_tokens_1.space[8]}`,
pointerEvents: "none",
});
const prefixSuffixTextStyles = (0, css_1.css)({
margin: 0,
});
const inputSuffixButtonStyles = (0, css_1.css)({
pointerEvents: "auto",
padding: 0,
paddingRight: design_system_tokens_1.space[4],
"button:disabled svg": {
fill: design_system_tokens_1.themeTokens.color.icon.secondary,
},
});