1 | import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
2 | import _typeof from "@babel/runtime/helpers/esm/typeof";
|
3 | import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
4 | import _extends from "@babel/runtime/helpers/esm/extends";
|
5 | import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
6 | import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
7 | import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
8 | var _excluded = ["autoComplete", "onChange", "onFocus", "onBlur", "onPressEnter", "onKeyDown", "prefixCls", "disabled", "htmlSize", "className", "maxLength", "suffix", "showCount", "type", "classes", "classNames", "styles"];
|
9 | import clsx from 'classnames';
|
10 | import useMergedState from "rc-util/es/hooks/useMergedState";
|
11 | import omit from "rc-util/es/omit";
|
12 | import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
|
13 | import BaseInput from "./BaseInput";
|
14 | import { fixControlledValue, 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 | _props$type = props.type,
|
31 | type = _props$type === void 0 ? 'text' : _props$type,
|
32 | classes = props.classes,
|
33 | classNames = props.classNames,
|
34 | styles = props.styles,
|
35 | rest = _objectWithoutProperties(props, _excluded);
|
36 | var _useMergedState = useMergedState(props.defaultValue, {
|
37 | value: props.value
|
38 | }),
|
39 | _useMergedState2 = _slicedToArray(_useMergedState, 2),
|
40 | value = _useMergedState2[0],
|
41 | setValue = _useMergedState2[1];
|
42 | var _useState = useState(false),
|
43 | _useState2 = _slicedToArray(_useState, 2),
|
44 | focused = _useState2[0],
|
45 | setFocused = _useState2[1];
|
46 | var inputRef = useRef(null);
|
47 | var focus = function focus(option) {
|
48 | if (inputRef.current) {
|
49 | triggerFocus(inputRef.current, option);
|
50 | }
|
51 | };
|
52 | useImperativeHandle(ref, function () {
|
53 | return {
|
54 | focus: focus,
|
55 | blur: function blur() {
|
56 | var _inputRef$current;
|
57 | (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.blur();
|
58 | },
|
59 | setSelectionRange: function setSelectionRange(start, end, direction) {
|
60 | var _inputRef$current2;
|
61 | (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 ? void 0 : _inputRef$current2.setSelectionRange(start, end, direction);
|
62 | },
|
63 | select: function select() {
|
64 | var _inputRef$current3;
|
65 | (_inputRef$current3 = inputRef.current) === null || _inputRef$current3 === void 0 ? void 0 : _inputRef$current3.select();
|
66 | },
|
67 | input: inputRef.current
|
68 | };
|
69 | });
|
70 | useEffect(function () {
|
71 | setFocused(function (prev) {
|
72 | return prev && disabled ? false : prev;
|
73 | });
|
74 | }, [disabled]);
|
75 | var handleChange = function handleChange(e) {
|
76 | if (props.value === undefined) {
|
77 | setValue(e.target.value);
|
78 | }
|
79 | if (inputRef.current) {
|
80 | resolveOnChange(inputRef.current, e, onChange);
|
81 | }
|
82 | };
|
83 | var handleKeyDown = function handleKeyDown(e) {
|
84 | if (onPressEnter && e.key === 'Enter') {
|
85 | onPressEnter(e);
|
86 | }
|
87 | onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(e);
|
88 | };
|
89 | var handleFocus = function handleFocus(e) {
|
90 | setFocused(true);
|
91 | onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
|
92 | };
|
93 | var handleBlur = function handleBlur(e) {
|
94 | setFocused(false);
|
95 | onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);
|
96 | };
|
97 | var handleReset = function handleReset(e) {
|
98 | setValue('');
|
99 | focus();
|
100 | if (inputRef.current) {
|
101 | resolveOnChange(inputRef.current, e, onChange);
|
102 | }
|
103 | };
|
104 | var getInputElement = function getInputElement() {
|
105 |
|
106 | var otherProps = omit(props, ['prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix', 'allowClear',
|
107 |
|
108 |
|
109 | 'defaultValue', 'showCount', 'classes', 'htmlSize', 'styles', 'classNames']);
|
110 | return React.createElement("input", _extends({
|
111 | autoComplete: autoComplete
|
112 | }, otherProps, {
|
113 | onChange: handleChange,
|
114 | onFocus: handleFocus,
|
115 | onBlur: handleBlur,
|
116 | onKeyDown: handleKeyDown,
|
117 | className: clsx(prefixCls, _defineProperty({}, "".concat(prefixCls, "-disabled"), disabled), classNames === null || classNames === void 0 ? void 0 : classNames.input),
|
118 | style: styles === null || styles === void 0 ? void 0 : styles.input,
|
119 | ref: inputRef,
|
120 | size: htmlSize,
|
121 | type: type
|
122 | }));
|
123 | };
|
124 | var getSuffix = function getSuffix() {
|
125 |
|
126 | var hasMaxLength = Number(maxLength) > 0;
|
127 | if (suffix || showCount) {
|
128 | var val = fixControlledValue(value);
|
129 | var valueLength = _toConsumableArray(val).length;
|
130 | var dataCount = _typeof(showCount) === 'object' ? showCount.formatter({
|
131 | value: val,
|
132 | count: valueLength,
|
133 | maxLength: maxLength
|
134 | }) : "".concat(valueLength).concat(hasMaxLength ? " / ".concat(maxLength) : '');
|
135 | return React.createElement(React.Fragment, null, !!showCount && React.createElement("span", {
|
136 | className: clsx("".concat(prefixCls, "-show-count-suffix"), _defineProperty({}, "".concat(prefixCls, "-show-count-has-suffix"), !!suffix), classNames === null || classNames === void 0 ? void 0 : classNames.count),
|
137 | style: _objectSpread({}, styles === null || styles === void 0 ? void 0 : styles.count)
|
138 | }, dataCount), suffix);
|
139 | }
|
140 | return null;
|
141 | };
|
142 | return React.createElement(BaseInput, _extends({}, rest, {
|
143 | prefixCls: prefixCls,
|
144 | className: className,
|
145 | inputElement: getInputElement(),
|
146 | handleReset: handleReset,
|
147 | value: fixControlledValue(value),
|
148 | focused: focused,
|
149 | triggerFocus: focus,
|
150 | suffix: getSuffix(),
|
151 | disabled: disabled,
|
152 | classes: classes,
|
153 | classNames: classNames,
|
154 | styles: styles
|
155 | }));
|
156 | });
|
157 | export default Input; |
\ | No newline at end of file |