UNPKG

@ant-design/pro-flow

Version:
90 lines (89 loc) 3.7 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _excluded = ["value", "onChange", "onValueChanging", "onChangeEnd"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { Button } from 'antd'; import { Input } from "../Input"; import { memo, useCallback, useEffect, useRef, useState } from 'react'; /** * 控制输入框属性 */ import { jsx as _jsx } from "react/jsx-runtime"; import { Fragment as _Fragment } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; export var ControlInput = /*#__PURE__*/memo(function (_ref) { var value = _ref.value, onChange = _ref.onChange, onValueChanging = _ref.onValueChanging, onChangeEnd = _ref.onChangeEnd, props = _objectWithoutProperties(_ref, _excluded); var initialValue = value || ''; var _useState = useState(initialValue), _useState2 = _slicedToArray(_useState, 2), input = _useState2[0], setInput = _useState2[1]; var inputRef = useRef(null); var isChineseInput = useRef(false); var isFocusing = useRef(false); var updateValue = useCallback(function () { onChange === null || onChange === void 0 || onChange(input); }, [input]); useEffect(function () { if (typeof value !== 'undefined') setInput(value); }, [value]); return /*#__PURE__*/_jsx(Input, _objectSpread(_objectSpread({ ref: inputRef }, props), {}, { value: input, onCompositionStart: function onCompositionStart() { isChineseInput.current = true; }, onCompositionEnd: function onCompositionEnd() { isChineseInput.current = false; }, onFocus: function onFocus() { isFocusing.current = true; }, onBlur: function onBlur() { isFocusing.current = false; onChangeEnd === null || onChangeEnd === void 0 || onChangeEnd(input); }, onChange: function onChange(e) { setInput(e.target.value); onValueChanging === null || onValueChanging === void 0 || onValueChanging(e.target.value); }, onPressEnter: function onPressEnter(e) { if (!e.shiftKey && !isChineseInput.current) { e.preventDefault(); updateValue(); isFocusing.current = false; onChangeEnd === null || onChangeEnd === void 0 || onChangeEnd(input); } }, suffix: value === input ? /*#__PURE__*/_jsx("span", {}) : /*#__PURE__*/_jsxs(_Fragment, { children: [/*#__PURE__*/_jsx(Button, { type: 'link', size: 'small', onClick: function onClick() { setInput(value); }, style: { padding: 0 }, children: "\u91CD\u7F6E" }), /*#__PURE__*/_jsx(Button, { type: 'link', size: 'small', style: { padding: 0 }, onClick: function onClick() { updateValue(); }, children: "\u4FDD\u5B58 \u21B5" })] }) })); });