You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
271 lines
8.6 KiB
271 lines
8.6 KiB
import _extends from "@babel/runtime/helpers/extends";
|
|
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
import _createClass from "@babel/runtime/helpers/createClass";
|
|
import _inherits from "@babel/runtime/helpers/inherits";
|
|
import _createSuper from "@babel/runtime/helpers/createSuper";
|
|
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
import * as React from 'react';
|
|
import classNames from 'classnames';
|
|
import omit from 'omit.js';
|
|
import ClearableLabeledInput, { hasPrefixSuffix } from './ClearableLabeledInput';
|
|
import { ConfigConsumer } from '../config-provider';
|
|
import SizeContext from '../config-provider/SizeContext';
|
|
import devWarning from '../_util/devWarning';
|
|
export function fixControlledValue(value) {
|
|
if (typeof value === 'undefined' || value === null) {
|
|
return '';
|
|
}
|
|
|
|
return value;
|
|
}
|
|
export function resolveOnChange(target, e, onChange) {
|
|
if (onChange) {
|
|
var event = e;
|
|
|
|
if (e.type === 'click') {
|
|
// click clear icon
|
|
event = Object.create(e);
|
|
event.target = target;
|
|
event.currentTarget = target;
|
|
var originalInputValue = target.value; // change target ref value cause e.target.value should be '' when clear input
|
|
|
|
target.value = '';
|
|
onChange(event); // reset target ref value
|
|
|
|
target.value = originalInputValue;
|
|
return;
|
|
}
|
|
|
|
onChange(event);
|
|
}
|
|
}
|
|
export function getInputClassName(prefixCls, bordered, size, disabled, direction) {
|
|
var _classNames;
|
|
|
|
return classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-sm"), size === 'small'), _defineProperty(_classNames, "".concat(prefixCls, "-lg"), size === 'large'), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _defineProperty(_classNames, "".concat(prefixCls, "-borderless"), !bordered), _classNames));
|
|
}
|
|
|
|
var Input = /*#__PURE__*/function (_React$Component) {
|
|
_inherits(Input, _React$Component);
|
|
|
|
var _super = _createSuper(Input);
|
|
|
|
function Input(props) {
|
|
var _this;
|
|
|
|
_classCallCheck(this, Input);
|
|
|
|
_this = _super.call(this, props);
|
|
_this.direction = 'ltr';
|
|
|
|
_this.focus = function () {
|
|
_this.input.focus();
|
|
};
|
|
|
|
_this.saveClearableInput = function (input) {
|
|
_this.clearableInput = input;
|
|
};
|
|
|
|
_this.saveInput = function (input) {
|
|
_this.input = input;
|
|
};
|
|
|
|
_this.onFocus = function (e) {
|
|
var onFocus = _this.props.onFocus;
|
|
|
|
_this.setState({
|
|
focused: true
|
|
}, _this.clearPasswordValueAttribute);
|
|
|
|
if (onFocus) {
|
|
onFocus(e);
|
|
}
|
|
};
|
|
|
|
_this.onBlur = function (e) {
|
|
var onBlur = _this.props.onBlur;
|
|
|
|
_this.setState({
|
|
focused: false
|
|
}, _this.clearPasswordValueAttribute);
|
|
|
|
if (onBlur) {
|
|
onBlur(e);
|
|
}
|
|
};
|
|
|
|
_this.handleReset = function (e) {
|
|
_this.setValue('', function () {
|
|
_this.focus();
|
|
});
|
|
|
|
resolveOnChange(_this.input, e, _this.props.onChange);
|
|
};
|
|
|
|
_this.renderInput = function (prefixCls, size, bordered) {
|
|
var input = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
var _this$props = _this.props,
|
|
className = _this$props.className,
|
|
addonBefore = _this$props.addonBefore,
|
|
addonAfter = _this$props.addonAfter,
|
|
customizeSize = _this$props.size,
|
|
disabled = _this$props.disabled; // Fix https://fb.me/react-unknown-prop
|
|
|
|
var otherProps = omit(_this.props, ['prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix', 'allowClear', // Input elements must be either controlled or uncontrolled,
|
|
// specify either the value prop, or the defaultValue prop, but not both.
|
|
'defaultValue', 'size', 'inputType', 'bordered']);
|
|
return /*#__PURE__*/React.createElement("input", _extends({
|
|
autoComplete: input.autoComplete
|
|
}, otherProps, {
|
|
onChange: _this.handleChange,
|
|
onFocus: _this.onFocus,
|
|
onBlur: _this.onBlur,
|
|
onKeyDown: _this.handleKeyDown,
|
|
className: classNames(getInputClassName(prefixCls, bordered, customizeSize || size, disabled, _this.direction), _defineProperty({}, className, className && !addonBefore && !addonAfter)),
|
|
ref: _this.saveInput
|
|
}));
|
|
};
|
|
|
|
_this.clearPasswordValueAttribute = function () {
|
|
// https://github.com/ant-design/ant-design/issues/20541
|
|
_this.removePasswordTimeout = setTimeout(function () {
|
|
if (_this.input && _this.input.getAttribute('type') === 'password' && _this.input.hasAttribute('value')) {
|
|
_this.input.removeAttribute('value');
|
|
}
|
|
});
|
|
};
|
|
|
|
_this.handleChange = function (e) {
|
|
_this.setValue(e.target.value, _this.clearPasswordValueAttribute);
|
|
|
|
resolveOnChange(_this.input, e, _this.props.onChange);
|
|
};
|
|
|
|
_this.handleKeyDown = function (e) {
|
|
var _this$props2 = _this.props,
|
|
onPressEnter = _this$props2.onPressEnter,
|
|
onKeyDown = _this$props2.onKeyDown;
|
|
|
|
if (e.keyCode === 13 && onPressEnter) {
|
|
onPressEnter(e);
|
|
}
|
|
|
|
if (onKeyDown) {
|
|
onKeyDown(e);
|
|
}
|
|
};
|
|
|
|
_this.renderComponent = function (_ref) {
|
|
var getPrefixCls = _ref.getPrefixCls,
|
|
direction = _ref.direction,
|
|
input = _ref.input;
|
|
var _this$state = _this.state,
|
|
value = _this$state.value,
|
|
focused = _this$state.focused;
|
|
var _this$props3 = _this.props,
|
|
customizePrefixCls = _this$props3.prefixCls,
|
|
_this$props3$bordered = _this$props3.bordered,
|
|
bordered = _this$props3$bordered === void 0 ? true : _this$props3$bordered;
|
|
var prefixCls = getPrefixCls('input', customizePrefixCls);
|
|
_this.direction = direction;
|
|
return /*#__PURE__*/React.createElement(SizeContext.Consumer, null, function (size) {
|
|
return /*#__PURE__*/React.createElement(ClearableLabeledInput, _extends({
|
|
size: size
|
|
}, _this.props, {
|
|
prefixCls: prefixCls,
|
|
inputType: "input",
|
|
value: fixControlledValue(value),
|
|
element: _this.renderInput(prefixCls, size, bordered, input),
|
|
handleReset: _this.handleReset,
|
|
ref: _this.saveClearableInput,
|
|
direction: direction,
|
|
focused: focused,
|
|
triggerFocus: _this.focus,
|
|
bordered: bordered
|
|
}));
|
|
});
|
|
};
|
|
|
|
var value = typeof props.value === 'undefined' ? props.defaultValue : props.value;
|
|
_this.state = {
|
|
value: value,
|
|
focused: false,
|
|
// eslint-disable-next-line react/no-unused-state
|
|
prevValue: props.value
|
|
};
|
|
return _this;
|
|
}
|
|
|
|
_createClass(Input, [{
|
|
key: "componentDidMount",
|
|
value: function componentDidMount() {
|
|
this.clearPasswordValueAttribute();
|
|
} // Since polyfill `getSnapshotBeforeUpdate` need work with `componentDidUpdate`.
|
|
// We keep an empty function here.
|
|
|
|
}, {
|
|
key: "componentDidUpdate",
|
|
value: function componentDidUpdate() {}
|
|
}, {
|
|
key: "getSnapshotBeforeUpdate",
|
|
value: function getSnapshotBeforeUpdate(prevProps) {
|
|
if (hasPrefixSuffix(prevProps) !== hasPrefixSuffix(this.props)) {
|
|
devWarning(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");
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}, {
|
|
key: "componentWillUnmount",
|
|
value: function componentWillUnmount() {
|
|
if (this.removePasswordTimeout) {
|
|
clearTimeout(this.removePasswordTimeout);
|
|
}
|
|
}
|
|
}, {
|
|
key: "blur",
|
|
value: function blur() {
|
|
this.input.blur();
|
|
}
|
|
}, {
|
|
key: "select",
|
|
value: function select() {
|
|
this.input.select();
|
|
}
|
|
}, {
|
|
key: "setValue",
|
|
value: function setValue(value, callback) {
|
|
if (this.props.value === undefined) {
|
|
this.setState({
|
|
value: value
|
|
}, callback);
|
|
}
|
|
}
|
|
}, {
|
|
key: "render",
|
|
value: function render() {
|
|
return /*#__PURE__*/React.createElement(ConfigConsumer, null, this.renderComponent);
|
|
}
|
|
}], [{
|
|
key: "getDerivedStateFromProps",
|
|
value: function getDerivedStateFromProps(nextProps, _ref2) {
|
|
var prevValue = _ref2.prevValue;
|
|
var newState = {
|
|
prevValue: nextProps.value
|
|
};
|
|
|
|
if (nextProps.value !== undefined || prevValue !== nextProps.value) {
|
|
newState.value = nextProps.value;
|
|
}
|
|
|
|
return newState;
|
|
}
|
|
}]);
|
|
|
|
return Input;
|
|
}(React.Component);
|
|
|
|
Input.defaultProps = {
|
|
type: 'text'
|
|
};
|
|
export default Input; |