1 | import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
2 | import _extends from "@babel/runtime/helpers/esm/extends";
|
3 | import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
4 | import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
5 | import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
6 | import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
7 | var _excluded = ["autoComplete", "onChange", "onFocus", "onBlur", "onPressEnter", "onKeyDown", "prefixCls", "disabled", "htmlSize", "className", "maxLength", "suffix", "showCount", "count", "type", "classes", "classNames", "styles", "onCompositionStart", "onCompositionEnd"];
|
8 | import clsx from 'classnames';
|
9 | import useMergedState from "rc-util/es/hooks/useMergedState";
|
10 | import omit from "rc-util/es/omit";
|
11 | import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
|
12 | import BaseInput from "./BaseInput";
|
13 | import useCount from "./hooks/useCount";
|
14 | import { resolveOnChange, triggerFocus } from "./utils/commonUtils";
|
15 | var Input = forwardRef(function (props, ref) {
|
16 | var autoComplete = props.autoComplete,
|
17 | onChange = props.onChange,
|
18 | onFocus = props.onFocus,
|
19 | onBlur = props.onBlur,
|
20 | onPressEnter = props.onPressEnter,
|
21 | onKeyDown = props.onKeyDown,
|
22 | _props$prefixCls = props.prefixCls,
|
23 | prefixCls = _props$prefixCls === void 0 ? 'rc-input' : _props$prefixCls,
|
24 | disabled = props.disabled,
|
25 | htmlSize = props.htmlSize,
|
26 | className = props.className,
|
27 | maxLength = props.maxLength,
|
28 | suffix = props.suffix,
|
29 | showCount = props.showCount,
|
30 | count = props.count,
|
31 | _props$type = props.type,
|
32 | type = _props$type === void 0 ? 'text' : _props$type,
|
33 | classes = props.classes,
|
34 | classNames = props.classNames,
|
35 | styles = props.styles,
|
36 | _onCompositionStart = props.onCompositionStart,
|
37 | onCompositionEnd = props.onCompositionEnd,
|
38 | rest = _objectWithoutProperties(props, _excluded);
|
39 | var _useState = useState(false),
|
40 | _useState2 = _slicedToArray(_useState, 2),
|
41 | focused = _useState2[0],
|
42 | setFocused = _useState2[1];
|
43 | var compositionRef = React.useRef(false);
|
44 | var inputRef = useRef(null);
|
45 | var focus = function focus(option) {
|
46 | if (inputRef.current) {
|
47 | triggerFocus(inputRef.current, option);
|
48 | }
|
49 | };
|
50 |
|
51 |
|
52 | var _useMergedState = useMergedState(props.defaultValue, {
|
53 | value: props.value
|
54 | }),
|
55 | _useMergedState2 = _slicedToArray(_useMergedState, 2),
|
56 | value = _useMergedState2[0],
|
57 | setValue = _useMergedState2[1];
|
58 | var formatValue = value === undefined || value === null ? '' : String(value);
|
59 |
|
60 |
|
61 | var _React$useState = React.useState(null),
|
62 | _React$useState2 = _slicedToArray(_React$useState, 2),
|
63 | selection = _React$useState2[0],
|
64 | setSelection = _React$useState2[1];
|
65 |
|
66 |
|
67 | var countConfig = useCount(count, showCount);
|
68 | var mergedMax = countConfig.max || maxLength;
|
69 | var valueLength = countConfig.strategy(formatValue);
|
70 | var isOutOfRange = !!mergedMax && valueLength > mergedMax;
|
71 |
|
72 |
|
73 | useImperativeHandle(ref, function () {
|
74 | return {
|
75 | focus: focus,
|
76 | blur: function blur() {
|
77 | var _inputRef$current;
|
78 | (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.blur();
|
79 | },
|
80 | setSelectionRange: function setSelectionRange(start, end, direction) {
|
81 | var _inputRef$current2;
|
82 | (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 || _inputRef$current2.setSelectionRange(start, end, direction);
|
83 | },
|
84 | select: function select() {
|
85 | var _inputRef$current3;
|
86 | (_inputRef$current3 = inputRef.current) === null || _inputRef$current3 === void 0 || _inputRef$current3.select();
|
87 | },
|
88 | input: inputRef.current
|
89 | };
|
90 | });
|
91 | useEffect(function () {
|
92 | setFocused(function (prev) {
|
93 | return prev && disabled ? false : prev;
|
94 | });
|
95 | }, [disabled]);
|
96 | var triggerChange = function triggerChange(e, currentValue) {
|
97 | var cutValue = currentValue;
|
98 | if (!compositionRef.current && countConfig.exceedFormatter && countConfig.max && countConfig.strategy(currentValue) > countConfig.max) {
|
99 | cutValue = countConfig.exceedFormatter(currentValue, {
|
100 | max: countConfig.max
|
101 | });
|
102 | if (currentValue !== cutValue) {
|
103 | var _inputRef$current4, _inputRef$current5;
|
104 | setSelection([((_inputRef$current4 = inputRef.current) === null || _inputRef$current4 === void 0 ? void 0 : _inputRef$current4.selectionStart) || 0, ((_inputRef$current5 = inputRef.current) === null || _inputRef$current5 === void 0 ? void 0 : _inputRef$current5.selectionEnd) || 0]);
|
105 | }
|
106 | }
|
107 | setValue(cutValue);
|
108 | if (inputRef.current) {
|
109 | resolveOnChange(inputRef.current, e, onChange, cutValue);
|
110 | }
|
111 | };
|
112 | React.useEffect(function () {
|
113 | if (selection) {
|
114 | var _inputRef$current6;
|
115 | (_inputRef$current6 = inputRef.current) === null || _inputRef$current6 === void 0 || _inputRef$current6.setSelectionRange.apply(_inputRef$current6, _toConsumableArray(selection));
|
116 | }
|
117 | }, [selection]);
|
118 | var onInternalChange = function onInternalChange(e) {
|
119 | triggerChange(e, e.target.value);
|
120 | };
|
121 | var onInternalCompositionEnd = function onInternalCompositionEnd(e) {
|
122 | compositionRef.current = false;
|
123 | triggerChange(e, e.currentTarget.value);
|
124 | onCompositionEnd === null || onCompositionEnd === void 0 || onCompositionEnd(e);
|
125 | };
|
126 | var handleKeyDown = function handleKeyDown(e) {
|
127 | if (onPressEnter && e.key === 'Enter') {
|
128 | onPressEnter(e);
|
129 | }
|
130 | onKeyDown === null || onKeyDown === void 0 || onKeyDown(e);
|
131 | };
|
132 | var handleFocus = function handleFocus(e) {
|
133 | setFocused(true);
|
134 | onFocus === null || onFocus === void 0 || onFocus(e);
|
135 | };
|
136 | var handleBlur = function handleBlur(e) {
|
137 | setFocused(false);
|
138 | onBlur === null || onBlur === void 0 || onBlur(e);
|
139 | };
|
140 | var handleReset = function handleReset(e) {
|
141 | setValue('');
|
142 | focus();
|
143 | if (inputRef.current) {
|
144 | resolveOnChange(inputRef.current, e, onChange);
|
145 | }
|
146 | };
|
147 |
|
148 |
|
149 | var outOfRangeCls = isOutOfRange && "".concat(prefixCls, "-out-of-range");
|
150 | var getInputElement = function getInputElement() {
|
151 |
|
152 | var otherProps = omit(props, ['prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix', 'allowClear',
|
153 |
|
154 |
|
155 | 'defaultValue', 'showCount', 'count', 'classes', 'htmlSize', 'styles', 'classNames']);
|
156 | return React.createElement("input", _extends({
|
157 | autoComplete: autoComplete
|
158 | }, otherProps, {
|
159 | onChange: onInternalChange,
|
160 | onFocus: handleFocus,
|
161 | onBlur: handleBlur,
|
162 | onKeyDown: handleKeyDown,
|
163 | className: clsx(prefixCls, _defineProperty({}, "".concat(prefixCls, "-disabled"), disabled), classNames === null || classNames === void 0 ? void 0 : classNames.input),
|
164 | style: styles === null || styles === void 0 ? void 0 : styles.input,
|
165 | ref: inputRef,
|
166 | size: htmlSize,
|
167 | type: type,
|
168 | onCompositionStart: function onCompositionStart(e) {
|
169 | compositionRef.current = true;
|
170 | _onCompositionStart === null || _onCompositionStart === void 0 || _onCompositionStart(e);
|
171 | },
|
172 | onCompositionEnd: onInternalCompositionEnd
|
173 | }));
|
174 | };
|
175 | var getSuffix = function getSuffix() {
|
176 |
|
177 | var hasMaxLength = Number(mergedMax) > 0;
|
178 | if (suffix || countConfig.show) {
|
179 | var dataCount = countConfig.showFormatter ? countConfig.showFormatter({
|
180 | value: formatValue,
|
181 | count: valueLength,
|
182 | maxLength: mergedMax
|
183 | }) : "".concat(valueLength).concat(hasMaxLength ? " / ".concat(mergedMax) : '');
|
184 | return React.createElement(React.Fragment, null, countConfig.show && React.createElement("span", {
|
185 | className: clsx("".concat(prefixCls, "-show-count-suffix"), _defineProperty({}, "".concat(prefixCls, "-show-count-has-suffix"), !!suffix), classNames === null || classNames === void 0 ? void 0 : classNames.count),
|
186 | style: _objectSpread({}, styles === null || styles === void 0 ? void 0 : styles.count)
|
187 | }, dataCount), suffix);
|
188 | }
|
189 | return null;
|
190 | };
|
191 |
|
192 |
|
193 | return React.createElement(BaseInput, _extends({}, rest, {
|
194 | prefixCls: prefixCls,
|
195 | className: clsx(className, outOfRangeCls),
|
196 | handleReset: handleReset,
|
197 | value: formatValue,
|
198 | focused: focused,
|
199 | triggerFocus: focus,
|
200 | suffix: getSuffix(),
|
201 | disabled: disabled,
|
202 | classes: classes,
|
203 | classNames: classNames,
|
204 | styles: styles
|
205 | }), getInputElement());
|
206 | });
|
207 | export default Input; |
\ | No newline at end of file |