antd
Version:
An enterprise-class UI design language and React components implementation
242 lines (202 loc) • 9.25 kB
JavaScript
function _typeof(obj) { 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 _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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 _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; }
import * as React from 'react';
import * as PropTypes from 'prop-types';
import { polyfill } from 'react-lifecycles-compat';
import classNames from 'classnames';
import omit from 'omit.js';
import { tuple } from '../_util/type';
import ClearableLabeledInput, { hasPrefixSuffix } from './ClearableLabeledInput';
import { ConfigConsumer } from '../config-provider';
import warning from '../_util/warning';
export var InputSizes = tuple('small', 'default', 'large');
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, size, disabled) {
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), _classNames));
}
var Input =
/*#__PURE__*/
function (_React$Component) {
_inherits(Input, _React$Component);
function Input(props) {
var _this;
_classCallCheck(this, Input);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Input).call(this, props));
_this.saveClearableInput = function (input) {
_this.clearableInput = input;
};
_this.saveInput = function (input) {
_this.input = input;
};
_this.handleReset = function (e) {
_this.setValue('', function () {
_this.focus();
});
resolveOnChange(_this.input, e, _this.props.onChange);
};
_this.renderInput = function (prefixCls) {
var _this$props = _this.props,
className = _this$props.className,
addonBefore = _this$props.addonBefore,
addonAfter = _this$props.addonAfter,
size = _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']);
return React.createElement("input", _extends({}, otherProps, {
onChange: _this.handleChange,
onKeyDown: _this.handleKeyDown,
className: classNames(getInputClassName(prefixCls, size, disabled), _defineProperty({}, className, className && !addonBefore && !addonAfter)),
ref: _this.saveInput
}));
};
_this.handleChange = function (e) {
_this.setValue(e.target.value);
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;
var value = _this.state.value;
var customizePrefixCls = _this.props.prefixCls;
var prefixCls = getPrefixCls('input', customizePrefixCls);
return React.createElement(ClearableLabeledInput, _extends({}, _this.props, {
prefixCls: prefixCls,
inputType: "input",
value: fixControlledValue(value),
element: _this.renderInput(prefixCls),
handleReset: _this.handleReset,
ref: _this.saveClearableInput
}));
};
var value = typeof props.value === 'undefined' ? props.defaultValue : props.value;
_this.state = {
value: value
};
return _this;
}
_createClass(Input, [{
key: "componentDidUpdate",
// Since polyfill `getSnapshotBeforeUpdate` need work with `componentDidUpdate`.
// We keep an empty function here.
value: function componentDidUpdate() {}
}, {
key: "getSnapshotBeforeUpdate",
value: function getSnapshotBeforeUpdate(prevProps) {
if (hasPrefixSuffix(prevProps) !== hasPrefixSuffix(this.props)) {
warning(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: "focus",
value: function focus() {
this.input.focus();
}
}, {
key: "blur",
value: function blur() {
this.input.blur();
}
}, {
key: "select",
value: function select() {
this.input.select();
}
}, {
key: "setValue",
value: function setValue(value, callback) {
if (!('value' in this.props)) {
this.setState({
value: value
}, callback);
}
}
}, {
key: "render",
value: function render() {
return React.createElement(ConfigConsumer, null, this.renderComponent);
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps) {
if ('value' in nextProps) {
return {
value: nextProps.value
};
}
return null;
}
}]);
return Input;
}(React.Component);
Input.defaultProps = {
type: 'text'
};
Input.propTypes = {
type: PropTypes.string,
id: PropTypes.string,
size: PropTypes.oneOf(InputSizes),
maxLength: PropTypes.number,
disabled: PropTypes.bool,
value: PropTypes.any,
defaultValue: PropTypes.any,
className: PropTypes.string,
addonBefore: PropTypes.node,
addonAfter: PropTypes.node,
prefixCls: PropTypes.string,
onPressEnter: PropTypes.func,
onKeyDown: PropTypes.func,
onKeyUp: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
prefix: PropTypes.node,
suffix: PropTypes.node,
allowClear: PropTypes.bool
};
polyfill(Input);
export default Input;
//# sourceMappingURL=Input.js.map