UNPKG

@hitachivantara/uikit-react-pentaho

Version:
155 lines (154 loc) 4.97 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const jsxRuntime = require("react/jsx-runtime"); const react = require("react"); const uikitReactCore = require("@hitachivantara/uikit-react-core"); const uikitReactIcons = require("@hitachivantara/uikit-react-icons"); const { staticClasses, useClasses } = uikitReactCore.createClasses( "HvCanvasToolbarTabs-editor", { root: { position: "relative", display: "flex", width: "100%", overflow: "hidden", "&:has($label:hover:not($edit))": { color: uikitReactCore.theme.colors.textSubtle, "& $editIcon": { visibility: "visible" } } }, edit: { color: uikitReactCore.theme.colors.textSubtle, borderColor: "currentColor", backgroundColor: uikitReactCore.theme.colors.bgContainer, cursor: "text" }, label: { width: "100%", boxSizing: "border-box", border: "1px solid transparent", borderRadius: uikitReactCore.theme.radii.base, padding: uikitReactCore.theme.spacing(0, "sm", 0, "xs"), textAlign: "start", whiteSpace: "nowrap", overflow: "hidden", outline: "none", "&:not($edit)": { textOverflow: "ellipsis" }, "&:hover:not($edit)": { color: uikitReactCore.theme.colors.textSubtle, borderColor: uikitReactCore.theme.colors.bgHover, backgroundColor: uikitReactCore.theme.colors.bgHover } }, editIcon: { position: "absolute", right: uikitReactCore.theme.space.xxs, top: 4, width: 16, height: 16, visibility: "hidden", pointerEvents: "none" } } ); const sanitize = (value) => value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;"); const ToolbarTabEditor = ({ id, className, edit: editProp, value: valueProp, defaultValue: defaultValueProp = "", classes: classesProp, onInput: onInputProp, onClick: onClickProp, onBlur: onBlurProp, onKeyDown: onKeyDownProp, onChange: onChangeProp, onEditChange: onEditChangeProp, ...others }) => { const { cx, classes } = useClasses(classesProp); const contentEditableRef = react.useRef(null); const [value, setValue] = uikitReactCore.useControlled( valueProp ? sanitize(valueProp) : valueProp, defaultValueProp ? sanitize(defaultValueProp) : defaultValueProp ); const [cachedValue, setCachedValue] = react.useState(value); const [isEditing, setIsEditing] = uikitReactCore.useControlled(editProp, false); const moveCursorToEnd = () => { if (!contentEditableRef.current) return; const element = contentEditableRef.current; const range = document.createRange(); const selection = window.getSelection(); range.selectNodeContents(element); range.collapse(false); selection?.removeAllRanges(); selection?.addRange(range); element.scrollLeft = element.scrollWidth; }; const scrollContentToStart = () => { if (!contentEditableRef.current) return; const element = contentEditableRef.current; element.scrollLeft = 0; }; const changeEdit = (edit) => { setIsEditing(edit); onEditChangeProp?.(edit); }; uikitReactCore.useEnhancedEffect(() => { if (isEditing) moveCursorToEnd(); }, [isEditing, value]); const handleInput = (event) => { const newValue = sanitize(event.target.textContent) || ""; setValue(newValue); onInputProp?.(event); onChangeProp?.(event, newValue); }; const handleClick = (event) => { setCachedValue(value); changeEdit(true); onClickProp?.(event); }; const handleBlur = (event) => { changeEdit(false); scrollContentToStart(); const newValue = value.trim() || cachedValue; setValue(newValue); onBlurProp?.(event, newValue); }; const handleKeyDown = (event) => { if (uikitReactCore.isKey(event, "Enter")) { handleBlur(event); } else if (uikitReactCore.isKey(event, "Esc")) { changeEdit(false); setValue(cachedValue); onChangeProp?.(event, cachedValue); } onKeyDownProp?.(event); }; return /* @__PURE__ */ jsxRuntime.jsxs("div", { id, className: cx(classes.root, className), children: [ /* @__PURE__ */ jsxRuntime.jsx( uikitReactCore.HvTypography, { ref: contentEditableRef, contentEditable: isEditing, className: cx(classes.label, { [classes.edit]: isEditing }), variant: "label", component: "span", onInput: handleInput, onClick: handleClick, onBlur: handleBlur, onKeyDown: handleKeyDown, dangerouslySetInnerHTML: { __html: value }, ...others } ), /* @__PURE__ */ jsxRuntime.jsx(uikitReactIcons.Edit, { className: classes.editIcon, iconSize: "XS" }) ] }); }; exports.ToolbarTabEditor = ToolbarTabEditor; exports.toolbarTabEditorClasses = staticClasses;