antd
Version:
An enterprise-class UI design language and React components implementation
313 lines (247 loc) • 13.2 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.fixControlledValue = fixControlledValue;
exports.resolveOnChange = resolveOnChange;
exports.getInputClassName = getInputClassName;
exports["default"] = void 0;
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _omit = _interopRequireDefault(require("omit.js"));
var _ClearableLabeledInput = _interopRequireWildcard(require("./ClearableLabeledInput"));
var _configProvider = require("../config-provider");
var _SizeContext = _interopRequireDefault(require("../config-provider/SizeContext"));
var _devWarning = _interopRequireDefault(require("../_util/devWarning"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function fixControlledValue(value) {
if (typeof value === 'undefined' || value === null) {
return '';
}
return value;
}
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);
}
}
function getInputClassName(prefixCls, size, disabled, direction) {
var _classNames;
return (0, _classnames["default"])(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'), _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) {
var input = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
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 = (0, _omit["default"])(_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']);
return /*#__PURE__*/React.createElement("input", _extends({
autoComplete: input.autoComplete
}, otherProps, {
onChange: _this.handleChange,
onFocus: _this.onFocus,
onBlur: _this.onBlur,
onKeyDown: _this.handleKeyDown,
className: (0, _classnames["default"])(getInputClassName(prefixCls, 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 customizePrefixCls = _this.props.prefixCls;
var prefixCls = getPrefixCls('input', customizePrefixCls);
_this.direction = direction;
return /*#__PURE__*/React.createElement(_SizeContext["default"].Consumer, null, function (size) {
return /*#__PURE__*/React.createElement(_ClearableLabeledInput["default"], _extends({
size: size
}, _this.props, {
prefixCls: prefixCls,
inputType: "input",
value: fixControlledValue(value),
element: _this.renderInput(prefixCls, size, input),
handleReset: _this.handleReset,
ref: _this.saveClearableInput,
direction: direction,
focused: focused,
triggerFocus: _this.focus
}));
});
};
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 ((0, _ClearableLabeledInput.hasPrefixSuffix)(prevProps) !== (0, _ClearableLabeledInput.hasPrefixSuffix)(this.props)) {
(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");
}
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(_configProvider.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'
};
var _default = Input;
exports["default"] = _default;
;