UNPKG

6.6 kBJavaScriptView Raw
1import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2import _typeof from "@babel/runtime/helpers/esm/typeof";
3import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
4import _extends from "@babel/runtime/helpers/esm/extends";
5import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
6import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
7import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
8var _excluded = ["autoComplete", "onChange", "onFocus", "onBlur", "onPressEnter", "onKeyDown", "prefixCls", "disabled", "htmlSize", "className", "maxLength", "suffix", "showCount", "type", "classes", "classNames", "styles"];
9import clsx from 'classnames';
10import useMergedState from "rc-util/es/hooks/useMergedState";
11import omit from "rc-util/es/omit";
12import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
13import BaseInput from "./BaseInput";
14import { fixControlledValue, resolveOnChange, triggerFocus } from "./utils/commonUtils";
15var Input = /*#__PURE__*/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 // Fix https://fb.me/react-unknown-prop
106 var otherProps = omit(props, ['prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix', 'allowClear',
107 // Input elements must be either controlled or uncontrolled,
108 // specify either the value prop, or the defaultValue prop, but not both.
109 'defaultValue', 'showCount', 'classes', 'htmlSize', 'styles', 'classNames']);
110 return /*#__PURE__*/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 // Max length value
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 /*#__PURE__*/React.createElement(React.Fragment, null, !!showCount && /*#__PURE__*/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 /*#__PURE__*/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});
157export default Input;
\No newline at end of file