@atlaskit/inline-edit
Version:
An inline edit displays a custom input component that switches between reading and editing on the same page.
103 lines • 4.05 kB
JavaScript
/* inline-editable-textfield.tsx generated by @compiled/babel-plugin v3.0.4 */
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["errorMessage", "isInvalid"];
import "./inline-editable-textfield.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
import React, { useCallback, useRef } from 'react';
import { cx } from '@atlaskit/css';
import ErrorIcon from '@atlaskit/icon/core/status-error';
import InlineDialog from '@atlaskit/inline-dialog/inline-dialog';
import { fg } from '@atlaskit/platform-feature-flags/fg';
import { Popup } from '@atlaskit/popup/popup';
import { Box } from '@atlaskit/primitives/compiled/box';
import Textfield from '@atlaskit/textfield/text-field';
import InlineEdit from './inline-edit';
var errorIconContainerStyles = {
root: "_vwz41osq _y4ti12x7"
};
var validationPopupStyles = {
content: "_p12flti4 _1q51pxbi _85i5pxbi _bozg1ejb _y4ti1ejb"
};
var readViewForTextFieldStyles = {
root: "_11c8fhey _1rjcu2gc _18zr12x7 _1e0c1txw _p12f1osq _1nmz1hna"
};
var compactStyles = {
root: "_1rjc1b66 _18zr12x7"
};
var InlineEditableTextfield = function InlineEditableTextfield(props) {
var _props$isCompact = props.isCompact,
isCompact = _props$isCompact === void 0 ? false : _props$isCompact,
defaultValue = props.defaultValue,
placeholder = props.placeholder,
testId = props.testId,
providedOnCancel = props.onCancel;
var textFieldRef = useRef();
var onCancel = useCallback(function () {
if (textFieldRef.current) {
textFieldRef.current.value = defaultValue || '';
}
providedOnCancel === null || providedOnCancel === void 0 || providedOnCancel();
}, [defaultValue, providedOnCancel]);
return /*#__PURE__*/React.createElement(InlineEdit
// eslint-disable-next-line @repo/internal/react/no-unsafe-spread-props
, _extends({}, props, {
onCancel: onCancel,
defaultValue: defaultValue,
editView: function editView(_ref) {
var errorMessage = _ref.errorMessage,
isInvalid = _ref.isInvalid,
props = _objectWithoutProperties(_ref, _excluded);
var field = /*#__PURE__*/React.createElement(Textfield, _extends({}, props, {
ref: textFieldRef,
elemAfterInput: isInvalid && /*#__PURE__*/React.createElement(Box, {
xcss: errorIconContainerStyles.root
}, /*#__PURE__*/React.createElement(ErrorIcon, {
label: "error",
color: "var(--ds-icon-danger, #C9372C)",
size: "small"
})),
testId: testId,
isCompact: isCompact,
autoFocus: true
}));
if (fg('platform-dst-top-layer')) {
return /*#__PURE__*/React.createElement(Popup, {
isOpen: isInvalid,
placement: "right"
// Validation follows field state without stealing focus or light-dismissing.
,
mode: "manual",
role: "none",
trigger: function trigger(_ref2) {
var ref = _ref2.ref;
return /*#__PURE__*/React.createElement("div", {
ref: ref
}, field);
},
content: function content() {
return /*#__PURE__*/React.createElement("div", {
id: "".concat(props.id, "-error"),
className: ax([validationPopupStyles.content])
}, errorMessage);
}
});
}
return /*#__PURE__*/React.createElement(InlineDialog, {
isOpen: isInvalid,
content: /*#__PURE__*/React.createElement("div", {
id: "error-message"
}, errorMessage),
placement: "right"
}, field);
},
readView: function readView() {
return /*#__PURE__*/React.createElement(Box, {
xcss: cx(readViewForTextFieldStyles.root, isCompact && compactStyles.root),
"data-compact": isCompact,
testId: testId && "read-view-".concat(testId)
}, defaultValue || placeholder);
}
}));
};
export default InlineEditableTextfield;