UNPKG

@teamix/typography

Version:
119 lines (102 loc) 3.51 kB
import * as React from 'react'; import classNames from 'classnames'; import KeyCode from 'rc-util/lib/KeyCode'; import { Input } from '@alicloudfe/components'; import { EnterOutlined } from '@ant-design/icons'; var TextArea = Input.TextArea; var Editable = function Editable(_ref) { var _classNames; var prefixCls = _ref.prefixCls, ariaLabel = _ref['aria-label'], className = _ref.className, style = _ref.style, direction = _ref.direction, maxLength = _ref.maxLength, _ref$autoHeight = _ref.autoHeight, autoHeight = _ref$autoHeight === void 0 ? true : _ref$autoHeight, value = _ref.value, onSave = _ref.onSave, onCancel = _ref.onCancel, onEnd = _ref.onEnd; var ref = React.useRef(); var inComposition = React.useRef(false); var lastKeyCode = React.useRef(); var _React$useState = React.useState(value), current = _React$useState[0], setCurrent = _React$useState[1]; React.useEffect(function () { setCurrent(value); }, [value]); React.useEffect(function () { var _ref$current; var textAreaRef = (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.getInputNode(); if (ref.current && textAreaRef) { textAreaRef.focus(); var length = textAreaRef.value.length; textAreaRef.setSelectionRange(length, length); } }, []); var _onChange = function onChange(_ref2) { var target = _ref2.target; setCurrent(target.value.replace(/[\n\r]/g, '')); }; var onCompositionStart = function onCompositionStart() { inComposition.current = true; }; var onCompositionEnd = function onCompositionEnd() { inComposition.current = false; }; var onKeyDown = function onKeyDown(e) { var keyCode = e.keyCode; // We don't record keyCode when IME is using if (inComposition.current) return; lastKeyCode.current = keyCode; e.preventDefault(); }; var confirmChange = function confirmChange() { onSave(current.trim()); }; var onKeyUp = function onKeyUp(_ref3) { var keyCode = _ref3.keyCode, ctrlKey = _ref3.ctrlKey, altKey = _ref3.altKey, metaKey = _ref3.metaKey, shiftKey = _ref3.shiftKey; // Check if it's a real key if (lastKeyCode.current === keyCode && !inComposition.current && !ctrlKey && !altKey && !metaKey && !shiftKey) { if (keyCode === KeyCode.ENTER) { confirmChange(); onEnd === null || onEnd === void 0 ? void 0 : onEnd(); } else if (keyCode === KeyCode.ESC) { onCancel(); } } }; var onBlur = function onBlur() { confirmChange(); }; var textAreaClassName = classNames(prefixCls, prefixCls + "-edit-content", (_classNames = {}, _classNames[prefixCls + "-rtl"] = direction === 'rtl', _classNames), className); return /*#__PURE__*/React.createElement("div", { className: textAreaClassName, style: style }, /*#__PURE__*/React.createElement(TextArea, { style: { width: '100%' }, ref: ref, maxLength: maxLength, value: current, onChange: function onChange(_, e) { return _onChange(e); }, onKeyDown: onKeyDown, onKeyUp: onKeyUp, onCompositionStart: onCompositionStart, onCompositionEnd: onCompositionEnd, onBlur: onBlur, "aria-label": ariaLabel, autoHeight: autoHeight }), /*#__PURE__*/React.createElement(EnterOutlined, { className: prefixCls + "-edit-content-confirm" })); }; export default Editable;