lucid-ui
Version:
A UI component library from AppNexus.
320 lines (251 loc) • 14.3 kB
JavaScript
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); }
import _has from "lodash/has";
import _get from "lodash/get";
import _debounce from "lodash/debounce";
import _noop from "lodash/noop";
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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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; }
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { omitProps } from '../../util/component-types';
import reducers from './TextField.reducers';
import * as KEYCODE from '../../constants/key-code';
var cx = lucidClassNames.bind('&-TextField');
var bool = PropTypes.bool,
string = PropTypes.string,
func = PropTypes.func,
number = PropTypes.number,
object = PropTypes.object,
oneOfType = PropTypes.oneOfType;
var TextField = /*#__PURE__*/function (_React$Component) {
_inherits(TextField, _React$Component);
var _super = _createSuper(TextField);
function TextField() {
var _this;
_classCallCheck(this, TextField);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "state", {
value: _this.props.value,
isHolding: false,
isMounted: false
});
_defineProperty(_assertThisInitialized(_this), "textareaElement", /*#__PURE__*/React.createRef());
_defineProperty(_assertThisInitialized(_this), "inputElement", /*#__PURE__*/React.createRef());
_defineProperty(_assertThisInitialized(_this), "nativeElement", _this.props.isMultiLine ? _this.textareaElement : _this.inputElement);
_defineProperty(_assertThisInitialized(_this), "handleChangeDebounced", _debounce(function (value, _ref) {
var event = _ref.event,
props = _ref.props;
_this.props.onChangeDebounced && _this.props.onChangeDebounced(value, {
event: event,
props: props
});
}, _this.props.debounceLevel));
_defineProperty(_assertThisInitialized(_this), "releaseHold", _debounce(function () {
if (!_this.state.isMounted) {
return;
}
_this.setState({
isHolding: false
});
}, _this.props.lazyLevel));
_defineProperty(_assertThisInitialized(_this), "updateWhenReady", _debounce(function (newValue) {
if (!_this.state.isMounted) {
return;
}
if (_this.state.isHolding) {
_this.updateWhenReady(newValue);
} else if (newValue !== _this.state.value) {
_this.setState({
value: newValue
});
}
}, _this.props.lazyLevel));
_defineProperty(_assertThisInitialized(_this), "handleChange", function (event) {
var _this$props = _this.props,
onChange = _this$props.onChange,
onChangeDebounced = _this$props.onChangeDebounced;
var value = _get(event, 'target.value', '');
_this.setState({
value: value,
isHolding: true
});
_this.releaseHold();
onChange && onChange(value, {
event: event,
props: _this.props
}); // Also call the debounced handler in case the user wants debounced change
// events.
if (onChangeDebounced !== _noop) {
event.persist(); // https://facebook.github.io/react/docs/events.html#event-pooling
_this.handleChangeDebounced(value, {
event: event,
props: _this.props
});
}
});
_defineProperty(_assertThisInitialized(_this), "handleBlur", function (event) {
var _this$props2 = _this.props,
onBlur = _this$props2.onBlur,
onChangeDebounced = _this$props2.onChangeDebounced;
var value = _get(event, 'target.value', '');
if (onChangeDebounced !== _noop) {
_this.handleChangeDebounced.flush();
}
onBlur && onBlur(value, {
event: event,
props: _this.props
});
});
_defineProperty(_assertThisInitialized(_this), "handleKeyDown", function (event) {
var _assertThisInitialize = _assertThisInitialized(_this),
props = _assertThisInitialize.props,
_assertThisInitialize2 = _assertThisInitialize.props,
onSubmit = _assertThisInitialize2.onSubmit,
onKeyDown = _assertThisInitialize2.onKeyDown,
onChangeDebounced = _assertThisInitialize2.onChangeDebounced;
var value = _get(event, 'target.value', ''); // If the consumer passed an onKeyDown, we call it
if (onKeyDown) {
onKeyDown({
event: event,
props: props
});
}
if (event.keyCode === KEYCODE.Enter) {
if (onChangeDebounced !== _noop) {
_this.handleChangeDebounced.flush();
}
onSubmit && onSubmit(value, {
event: event,
props: _this.props
});
}
});
_defineProperty(_assertThisInitialized(_this), "focus", function () {
/* istanbul ignore next */
_this.nativeElement.current.focus();
});
return _this;
}
_createClass(TextField, [{
key: "UNSAFE_componentWillMount",
value: function UNSAFE_componentWillMount() {
this.setState({
isMounted: true
});
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.setState({
isMounted: false
});
}
}, {
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
// Allow consumer to optionally control state
if (_has(nextProps, 'value')) {
if (this.state.isHolding) {
this.updateWhenReady(nextProps.value);
} else {
this.setState({
value: nextProps.value
});
}
}
}
}, {
key: "render",
value: function render() {
var _this$props3 = this.props,
className = _this$props3.className,
isDisabled = _this$props3.isDisabled,
isMultiLine = _this$props3.isMultiLine,
rows = _this$props3.rows,
style = _this$props3.style,
passThroughs = _objectWithoutProperties(_this$props3, ["className", "isDisabled", "isMultiLine", "rows", "style"]);
var value = this.state.value;
var finalProps = _objectSpread(_objectSpread({}, omitProps(passThroughs, undefined, [].concat(_toConsumableArray(Object.keys(TextField.propTypes)), ['children']))), {}, {
className: cx('&', {
'&-is-disabled': isDisabled,
'&-is-multi-line': isMultiLine,
'&-is-single-line': !isMultiLine
}, className),
disabled: isDisabled,
onChange: this.handleChange,
onBlur: this.handleBlur,
onKeyDown: this.handleKeyDown,
style: style,
rows: rows,
value: value
});
return isMultiLine ? /*#__PURE__*/React.createElement("textarea", _extends({
ref: this.textareaElement
}, finalProps)) : /*#__PURE__*/React.createElement("input", _extends({
type: "text",
ref: this.inputElement
}, finalProps));
}
}]);
return TextField;
}(React.Component);
_defineProperty(TextField, "displayName", 'TextField');
_defineProperty(TextField, "peek", {
description: "\n\t\t\tTextField should cover all your text input needs. It is able to handle\n\t\t\tsingle and multi line inputs.\n\t\t",
categories: ['controls', 'text']
});
_defineProperty(TextField, "propTypes", {
style: object,
isMultiLine: bool,
isDisabled: bool,
rows: number,
className: string,
onChange: func,
onBlur: func,
onChangeDebounced: func,
onKeyDown: func,
onSubmit: func,
value: oneOfType([number, string]),
debounceLevel: number,
lazyLevel: number
});
_defineProperty(TextField, "defaultProps", {
style: undefined,
isDisabled: false,
isMultiLine: false,
onBlur: _noop,
onChange: _noop,
onChangeDebounced: _noop,
onSubmit: _noop,
onKeyDown: _noop,
rows: 5,
debounceLevel: 500,
lazyLevel: 1000,
value: ''
});
_defineProperty(TextField, "reducers", reducers);
export default TextField;