@atlaskit/inline-edit
Version:
An inline edit displays a custom input component that switches between reading and editing on the same page.
98 lines • 3.5 kB
JavaScript
/* inline-editable-textfield.tsx generated by @compiled/babel-plugin v3.0.4 */
import _extends from "@babel/runtime/helpers/extends";
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';
const errorIconContainerStyles = {
root: "_vwz41osq _y4ti12x7"
};
const validationPopupStyles = {
content: "_p12flti4 _1q51pxbi _85i5pxbi _bozg1ejb _y4ti1ejb"
};
const readViewForTextFieldStyles = {
root: "_11c8fhey _1rjcu2gc _18zr12x7 _1e0c1txw _p12f1osq _1nmz1hna"
};
const compactStyles = {
root: "_1rjc1b66 _18zr12x7"
};
const InlineEditableTextfield = props => {
const {
isCompact = false,
defaultValue,
placeholder,
testId,
onCancel: providedOnCancel
} = props;
const textFieldRef = useRef();
const onCancel = useCallback(() => {
if (textFieldRef.current) {
textFieldRef.current.value = defaultValue || '';
}
providedOnCancel === null || providedOnCancel === void 0 ? 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: ({
errorMessage,
isInvalid,
...props
}) => {
const 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: ({
ref
}) => /*#__PURE__*/React.createElement("div", {
ref: ref
}, field),
content: () => /*#__PURE__*/React.createElement("div", {
id: `${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: () => /*#__PURE__*/React.createElement(Box, {
xcss: cx(readViewForTextFieldStyles.root, isCompact && compactStyles.root),
"data-compact": isCompact,
testId: testId && `read-view-${testId}`
}, defaultValue || placeholder)
}));
};
export default InlineEditableTextfield;