1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | var _typeof = require("@babel/runtime/helpers/typeof");
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports.fixControlledValue = fixControlledValue;
|
11 | exports.resolveOnChange = resolveOnChange;
|
12 | exports.getInputClassName = getInputClassName;
|
13 | exports.triggerFocus = triggerFocus;
|
14 | exports["default"] = void 0;
|
15 |
|
16 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
17 |
|
18 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
19 |
|
20 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
21 |
|
22 | var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
23 |
|
24 | var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
|
25 |
|
26 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
27 |
|
28 | var React = _interopRequireWildcard(require("react"));
|
29 |
|
30 | var _classnames = _interopRequireDefault(require("classnames"));
|
31 |
|
32 | var _omit = _interopRequireDefault(require("rc-util/lib/omit"));
|
33 |
|
34 | var _ClearableLabeledInput = _interopRequireWildcard(require("./ClearableLabeledInput"));
|
35 |
|
36 | var _configProvider = require("../config-provider");
|
37 |
|
38 | var _SizeContext = _interopRequireDefault(require("../config-provider/SizeContext"));
|
39 |
|
40 | var _devWarning = _interopRequireDefault(require("../_util/devWarning"));
|
41 |
|
42 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
43 |
|
44 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
45 |
|
46 | function fixControlledValue(value) {
|
47 | if (typeof value === 'undefined' || value === null) {
|
48 | return '';
|
49 | }
|
50 |
|
51 | return value;
|
52 | }
|
53 |
|
54 | function resolveOnChange(target, e, onChange, targetValue) {
|
55 | if (!onChange) {
|
56 | return;
|
57 | }
|
58 |
|
59 | var event = e;
|
60 | var originalInputValue = target.value;
|
61 |
|
62 | if (e.type === 'click') {
|
63 |
|
64 | event = Object.create(e);
|
65 | event.target = target;
|
66 | event.currentTarget = target;
|
67 |
|
68 | target.value = '';
|
69 | onChange(event);
|
70 |
|
71 | target.value = originalInputValue;
|
72 | return;
|
73 | }
|
74 |
|
75 |
|
76 | if (targetValue !== undefined) {
|
77 | event = Object.create(e);
|
78 | event.target = target;
|
79 | event.currentTarget = target;
|
80 | target.value = targetValue;
|
81 | onChange(event);
|
82 | return;
|
83 | }
|
84 |
|
85 | onChange(event);
|
86 | }
|
87 |
|
88 | function getInputClassName(prefixCls, bordered, size, disabled, direction) {
|
89 | var _classNames;
|
90 |
|
91 | return (0, _classnames["default"])(prefixCls, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-sm"), size === 'small'), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-lg"), size === 'large'), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-borderless"), !bordered), _classNames));
|
92 | }
|
93 |
|
94 | function triggerFocus(element, option) {
|
95 | if (!element) return;
|
96 | element.focus(option);
|
97 |
|
98 | var _ref = option || {},
|
99 | cursor = _ref.cursor;
|
100 |
|
101 | if (cursor) {
|
102 | var len = element.value.length;
|
103 |
|
104 | switch (cursor) {
|
105 | case 'start':
|
106 | element.setSelectionRange(0, 0);
|
107 | break;
|
108 |
|
109 | case 'end':
|
110 | element.setSelectionRange(len, len);
|
111 | break;
|
112 |
|
113 | default:
|
114 | element.setSelectionRange(0, len);
|
115 | }
|
116 | }
|
117 | }
|
118 |
|
119 | var Input = function (_React$Component) {
|
120 | (0, _inherits2["default"])(Input, _React$Component);
|
121 |
|
122 | var _super = (0, _createSuper2["default"])(Input);
|
123 |
|
124 | function Input(props) {
|
125 | var _this;
|
126 |
|
127 | (0, _classCallCheck2["default"])(this, Input);
|
128 | _this = _super.call(this, props);
|
129 | _this.direction = 'ltr';
|
130 |
|
131 | _this.focus = function (option) {
|
132 | triggerFocus(_this.input, option);
|
133 | };
|
134 |
|
135 | _this.saveClearableInput = function (input) {
|
136 | _this.clearableInput = input;
|
137 | };
|
138 |
|
139 | _this.saveInput = function (input) {
|
140 | _this.input = input;
|
141 | };
|
142 |
|
143 | _this.onFocus = function (e) {
|
144 | var onFocus = _this.props.onFocus;
|
145 |
|
146 | _this.setState({
|
147 | focused: true
|
148 | }, _this.clearPasswordValueAttribute);
|
149 |
|
150 | onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
|
151 | };
|
152 |
|
153 | _this.onBlur = function (e) {
|
154 | var onBlur = _this.props.onBlur;
|
155 |
|
156 | _this.setState({
|
157 | focused: false
|
158 | }, _this.clearPasswordValueAttribute);
|
159 |
|
160 | onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);
|
161 | };
|
162 |
|
163 | _this.handleReset = function (e) {
|
164 | _this.setValue('', function () {
|
165 | _this.focus();
|
166 | });
|
167 |
|
168 | resolveOnChange(_this.input, e, _this.props.onChange);
|
169 | };
|
170 |
|
171 | _this.renderInput = function (prefixCls, size, bordered) {
|
172 | var input = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
173 | var _this$props = _this.props,
|
174 | className = _this$props.className,
|
175 | addonBefore = _this$props.addonBefore,
|
176 | addonAfter = _this$props.addonAfter,
|
177 | customizeSize = _this$props.size,
|
178 | disabled = _this$props.disabled;
|
179 |
|
180 | var otherProps = (0, _omit["default"])(_this.props, ['prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix', 'allowClear',
|
181 |
|
182 | 'defaultValue', 'size', 'inputType', 'bordered']);
|
183 | return React.createElement("input", (0, _extends2["default"])({
|
184 | autoComplete: input.autoComplete
|
185 | }, otherProps, {
|
186 | onChange: _this.handleChange,
|
187 | onFocus: _this.onFocus,
|
188 | onBlur: _this.onBlur,
|
189 | onKeyDown: _this.handleKeyDown,
|
190 | className: (0, _classnames["default"])(getInputClassName(prefixCls, bordered, customizeSize || size, disabled, _this.direction), (0, _defineProperty2["default"])({}, className, className && !addonBefore && !addonAfter)),
|
191 | ref: _this.saveInput
|
192 | }));
|
193 | };
|
194 |
|
195 | _this.clearPasswordValueAttribute = function () {
|
196 |
|
197 | _this.removePasswordTimeout = setTimeout(function () {
|
198 | if (_this.input && _this.input.getAttribute('type') === 'password' && _this.input.hasAttribute('value')) {
|
199 | _this.input.removeAttribute('value');
|
200 | }
|
201 | });
|
202 | };
|
203 |
|
204 | _this.handleChange = function (e) {
|
205 | _this.setValue(e.target.value, _this.clearPasswordValueAttribute);
|
206 |
|
207 | resolveOnChange(_this.input, e, _this.props.onChange);
|
208 | };
|
209 |
|
210 | _this.handleKeyDown = function (e) {
|
211 | var _this$props2 = _this.props,
|
212 | onPressEnter = _this$props2.onPressEnter,
|
213 | onKeyDown = _this$props2.onKeyDown;
|
214 |
|
215 | if (onPressEnter && e.keyCode === 13) {
|
216 | onPressEnter(e);
|
217 | }
|
218 |
|
219 | onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(e);
|
220 | };
|
221 |
|
222 | _this.renderComponent = function (_ref2) {
|
223 | var getPrefixCls = _ref2.getPrefixCls,
|
224 | direction = _ref2.direction,
|
225 | input = _ref2.input;
|
226 | var _this$state = _this.state,
|
227 | value = _this$state.value,
|
228 | focused = _this$state.focused;
|
229 | var _this$props3 = _this.props,
|
230 | customizePrefixCls = _this$props3.prefixCls,
|
231 | _this$props3$bordered = _this$props3.bordered,
|
232 | bordered = _this$props3$bordered === void 0 ? true : _this$props3$bordered;
|
233 | var prefixCls = getPrefixCls('input', customizePrefixCls);
|
234 | _this.direction = direction;
|
235 | return React.createElement(_SizeContext["default"].Consumer, null, function (size) {
|
236 | return React.createElement(_ClearableLabeledInput["default"], (0, _extends2["default"])({
|
237 | size: size
|
238 | }, _this.props, {
|
239 | prefixCls: prefixCls,
|
240 | inputType: "input",
|
241 | value: fixControlledValue(value),
|
242 | element: _this.renderInput(prefixCls, size, bordered, input),
|
243 | handleReset: _this.handleReset,
|
244 | ref: _this.saveClearableInput,
|
245 | direction: direction,
|
246 | focused: focused,
|
247 | triggerFocus: _this.focus,
|
248 | bordered: bordered
|
249 | }));
|
250 | });
|
251 | };
|
252 |
|
253 | var value = typeof props.value === 'undefined' ? props.defaultValue : props.value;
|
254 | _this.state = {
|
255 | value: value,
|
256 | focused: false,
|
257 |
|
258 | prevValue: props.value
|
259 | };
|
260 | return _this;
|
261 | }
|
262 |
|
263 | (0, _createClass2["default"])(Input, [{
|
264 | key: "componentDidMount",
|
265 | value: function componentDidMount() {
|
266 | this.clearPasswordValueAttribute();
|
267 | }
|
268 |
|
269 |
|
270 | }, {
|
271 | key: "componentDidUpdate",
|
272 | value: function componentDidUpdate() {}
|
273 | }, {
|
274 | key: "getSnapshotBeforeUpdate",
|
275 | value: function getSnapshotBeforeUpdate(prevProps) {
|
276 | if ((0, _ClearableLabeledInput.hasPrefixSuffix)(prevProps) !== (0, _ClearableLabeledInput.hasPrefixSuffix)(this.props)) {
|
277 | (0, _devWarning["default"])(this.input !== document.activeElement, 'Input', "When Input is focused, dynamic add or remove prefix / suffix will make it lose focus caused by dom structure change. Read more: https://ant.design/components/input/#FAQ");
|
278 | }
|
279 |
|
280 | return null;
|
281 | }
|
282 | }, {
|
283 | key: "componentWillUnmount",
|
284 | value: function componentWillUnmount() {
|
285 | if (this.removePasswordTimeout) {
|
286 | clearTimeout(this.removePasswordTimeout);
|
287 | }
|
288 | }
|
289 | }, {
|
290 | key: "blur",
|
291 | value: function blur() {
|
292 | this.input.blur();
|
293 | }
|
294 | }, {
|
295 | key: "setSelectionRange",
|
296 | value: function setSelectionRange(start, end, direction) {
|
297 | this.input.setSelectionRange(start, end, direction);
|
298 | }
|
299 | }, {
|
300 | key: "select",
|
301 | value: function select() {
|
302 | this.input.select();
|
303 | }
|
304 | }, {
|
305 | key: "setValue",
|
306 | value: function setValue(value, callback) {
|
307 | if (this.props.value === undefined) {
|
308 | this.setState({
|
309 | value: value
|
310 | }, callback);
|
311 | } else {
|
312 | callback === null || callback === void 0 ? void 0 : callback();
|
313 | }
|
314 | }
|
315 | }, {
|
316 | key: "render",
|
317 | value: function render() {
|
318 | return React.createElement(_configProvider.ConfigConsumer, null, this.renderComponent);
|
319 | }
|
320 | }], [{
|
321 | key: "getDerivedStateFromProps",
|
322 | value: function getDerivedStateFromProps(nextProps, _ref3) {
|
323 | var prevValue = _ref3.prevValue;
|
324 | var newState = {
|
325 | prevValue: nextProps.value
|
326 | };
|
327 |
|
328 | if (nextProps.value !== undefined || prevValue !== nextProps.value) {
|
329 | newState.value = nextProps.value;
|
330 | }
|
331 |
|
332 | return newState;
|
333 | }
|
334 | }]);
|
335 | return Input;
|
336 | }(React.Component);
|
337 |
|
338 | Input.defaultProps = {
|
339 | type: 'text'
|
340 | };
|
341 | var _default = Input;
|
342 | exports["default"] = _default; |
\ | No newline at end of file |