UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

177 lines (173 loc) 5.54 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; /** * @jsxRuntime classic * @jsx jsx */ import { PureComponent } from 'react'; // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled, @typescript-eslint/consistent-type-imports -- Ignored via go/DSP-18766; jsx required at runtime for @jsxRuntime classic import { jsx } from '@emotion/react'; import { getBrowserInfo } from '../../utils/browser'; import { panelTextInput, panelTextInputWithCustomWidth } from './styles'; const KeyZCode = 90; const KeyYCode = 89; // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/react/no-class-components export default class PanelTextInput extends PureComponent { constructor(props) { super(props); _defineProperty(this, "onMouseDown", () => { const { onMouseDown } = this.props; if (onMouseDown) { onMouseDown(); } }); // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any _defineProperty(this, "onBlur", e => { const { onBlur } = this.props; if (onBlur) { onBlur(e); } }); _defineProperty(this, "handleChange", () => { const { onChange } = this.props; if (this.input) { this.setState({ value: this.input.value }); } if (onChange && this.input) { onChange(this.input.value); } }); // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any _defineProperty(this, "handleKeydown", e => { const { onUndo, onRedo, onSubmit, onCancel } = this.props; if (e.keyCode === 13 && onSubmit) { e.preventDefault(); // Prevent from submitting if an editor is inside a form. // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion onSubmit(this.input.value); } else if (e.keyCode === 27 && onCancel) { onCancel(e); } else if (typeof onUndo === 'function' && this.isUndoEvent(e)) { e.preventDefault(); onUndo(); } else if (typeof onRedo === 'function' && this.isRedoEvent(e)) { e.preventDefault(); onRedo(); } if (this.props.onKeyDown) { this.props.onKeyDown(e); } }); _defineProperty(this, "handleRef", input => { if (input instanceof HTMLInputElement) { this.input = input; if (this.props.autoFocus) { // Need this to prevent jumping when we render TextInput inside Portal @see ED-2992 this.focusTimeoutId = window.setTimeout(() => this.focus()); } } else { this.input = undefined; } }); this.state = { value: props.defaultValue || '' }; } componentDidUpdate(prevProps) { if (prevProps.defaultValue !== this.props.defaultValue) { this.setState({ value: this.props.defaultValue }); } } componentWillUnmount() { window.clearTimeout(this.focusTimeoutId); } render() { var _this$props, _this$props2; const { placeholder, width, maxLength, testId, ariaLabel, describedById, ariaActiveDescendant, ariaControls, ariaExpanded, ariaAutoComplete, role, inputId } = this.props; const { value } = this.state; return jsx("input", { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/design-system/consistent-css-prop-usage -- Ignored via go/DSP-18766 css: [panelTextInput, width !== undefined && panelTextInputWithCustomWidth(width)], role: role, "aria-autocomplete": ariaAutoComplete ? 'list' : undefined, "aria-expanded": ariaExpanded, "aria-controls": ariaControls, "aria-activedescendant": ariaActiveDescendant, "aria-describedby": describedById, "data-testid": testId || '', type: "text", placeholder: placeholder, value: value, onChange: this.handleChange, onKeyDown: this.handleKeydown, onMouseDown: this.onMouseDown, onBlur: this.onBlur, ref: this.handleRef, maxLength: maxLength, "aria-label": ariaLabel, "aria-required": (_this$props = this.props) === null || _this$props === void 0 ? void 0 : _this$props.ariaRequired, "aria-invalid": (_this$props2 = this.props) === null || _this$props2 === void 0 ? void 0 : _this$props2.ariaInvalid, id: inputId }); } focus() { const { input } = this; if (input) { const focusOpts = typeof this.props.autoFocus === 'object' ? this.props.autoFocus : {}; input.focus(focusOpts); } } // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any isUndoEvent(event) { const browser = getBrowserInfo(); return event.keyCode === KeyZCode && ( // cmd + z for mac browser.mac && event.metaKey && !event.shiftKey || // ctrl + z for non-mac !browser.mac && event.ctrlKey); } // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any isRedoEvent(event) { const browser = getBrowserInfo(); return ( // ctrl + y for non-mac !browser.mac && event.ctrlKey && event.keyCode === KeyYCode || browser.mac && event.metaKey && event.shiftKey && event.keyCode === KeyZCode || event.ctrlKey && event.shiftKey && event.keyCode === KeyZCode ); } }