wix-style-react
Version:
wix-style-react
325 lines (273 loc) • 13.1 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _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; };
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _Tag = require('../Tag/Tag');
var _Tag2 = _interopRequireDefault(_Tag);
var _Input = require('../Input');
var _Input2 = _interopRequireDefault(_Input);
var _InputSuffix = require('../Input/InputSuffix');
var _InputSuffix2 = _interopRequireDefault(_InputSuffix);
var _InputWithTags = require('./InputWithTags.scss');
var _InputWithTags2 = _interopRequireDefault(_InputWithTags);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _isUndefined = require('lodash/isUndefined');
var _isUndefined2 = _interopRequireDefault(_isUndefined);
var _SortableList = require('../SortableList/SortableList');
var _SortableList2 = _interopRequireDefault(_SortableList);
var _dndStyles = require('../dnd-styles');
var _dndStyles2 = _interopRequireDefault(_dndStyles);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var InputWithTags = function (_React$Component) {
_inherits(InputWithTags, _React$Component);
function InputWithTags(props) {
_classCallCheck(this, InputWithTags);
var _this = _possibleConstructorReturn(this, (InputWithTags.__proto__ || Object.getPrototypeOf(InputWithTags)).call(this, props));
_this.focus = _this.focus.bind(_this);
_this.blur = _this.blur.bind(_this);
_this.select = _this.select.bind(_this);
_this.renderReorderableTag = _this.renderReorderableTag.bind(_this);
_this.state = { inputValue: '', inputHasFocus: false };
return _this;
}
_createClass(InputWithTags, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.props.autoFocus && this.props.onFocus();
}
}, {
key: 'handleClick',
value: function handleClick(e) {
this.input.focus();
this.props.onInputClicked && this.props.onInputClicked(e);
}
}, {
key: 'handleInputFocus',
value: function handleInputFocus(e) {
!this.state.inputHasFocus && this.setState({ inputHasFocus: true });
this.props.onFocus && this.props.onFocus(e);
}
}, {
key: 'handleInputBlur',
value: function handleInputBlur(e) {
this.state.inputHasFocus && this.setState({ inputHasFocus: false });
this.props.onBlur && this.props.onBlur(e);
}
}, {
key: 'render',
value: function render() {
var _classNames,
_this2 = this;
var _props = this.props,
tags = _props.tags,
onRemoveTag = _props.onRemoveTag,
onReorder = _props.onReorder,
placeholder = _props.placeholder,
error = _props.error,
errorMessage = _props.errorMessage,
disabled = _props.disabled,
delimiters = _props.delimiters,
mode = _props.mode,
inputProps = _objectWithoutProperties(_props, ['tags', 'onRemoveTag', 'onReorder', 'placeholder', 'error', 'errorMessage', 'disabled', 'delimiters', 'mode']);
var hasFocus = this.state.inputHasFocus;
var isSelectMode = mode === 'select';
var className = (0, _classnames2.default)((_classNames = {}, _defineProperty(_classNames, _InputWithTags2.default.inputWithTagsContainer, true), _defineProperty(_classNames, _InputWithTags2.default.disabled, disabled), _defineProperty(_classNames, _InputWithTags2.default.error, error), _defineProperty(_classNames, _InputWithTags2.default.readOnly, isSelectMode), _defineProperty(_classNames, _InputWithTags2.default.hasFocus, hasFocus), _defineProperty(_classNames, _InputWithTags2.default.hasMaxHeight, !(0, _isUndefined2.default)(this.props.maxHeight) || !(0, _isUndefined2.default)(this.props.maxNumRows)), _classNames));
/* eslint-disable no-unused-vars */
var onManuallyInput = inputProps.onManuallyInput,
inputElement = inputProps.inputElement,
closeOnSelect = inputProps.closeOnSelect,
predicate = inputProps.predicate,
onClickOutside = inputProps.onClickOutside,
fixedHeader = inputProps.fixedHeader,
fixedFooter = inputProps.fixedFooter,
dataHook = inputProps.dataHook,
onFocus = inputProps.onFocus,
withSelection = inputProps.withSelection,
onBlur = inputProps.onBlur,
menuArrow = inputProps.menuArrow,
_ = inputProps.errorMessage,
onInputClicked = inputProps.onInputClicked,
desiredProps = _objectWithoutProperties(inputProps, ['onManuallyInput', 'inputElement', 'closeOnSelect', 'predicate', 'onClickOutside', 'fixedHeader', 'fixedFooter', 'dataHook', 'onFocus', 'withSelection', 'onBlur', 'menuArrow', 'errorMessage', 'onInputClicked']);
var fontSize = desiredProps.size && desiredProps.size === 'small' ? '14px' : '16px';
var rowMultiplier = void 0;
if (tags.length && tags[0].size === 'large') {
rowMultiplier = 47;
} else {
rowMultiplier = 35;
}
var maxHeight = this.props.maxHeight || this.props.maxNumRows * rowMultiplier || 'initial';
return _react2.default.createElement(
'div',
{
className: className,
style: { maxHeight: maxHeight },
onClick: function onClick() {
return _this2.handleClick();
},
'data-hook': this.props.dataHook
},
onReorder ? _react2.default.createElement(_SortableList2.default, {
contentClassName: _InputWithTags2.default.tagsContainer,
items: tags,
onDrop: onReorder,
renderItem: this.renderReorderableTag
}) : tags.map(function (_ref) {
var label = _ref.label,
rest = _objectWithoutProperties(_ref, ['label']);
return _react2.default.createElement(
_Tag2.default,
_extends({
key: rest.id,
dataHook: 'tag',
useOldMargins: false,
disabled: disabled,
onRemove: onRemoveTag,
className: _InputWithTags2.default.tag
}, rest),
label
);
}),
_react2.default.createElement(
'span',
{
className: (0, _classnames2.default)(_InputWithTags2.default.input, _defineProperty({}, _InputWithTags2.default.emptyInput, !tags.length)),
'data-hook': 'inner-input-with-tags'
},
_react2.default.createElement(
'div',
{ className: _InputWithTags2.default.hiddenDiv, style: { fontSize: fontSize } },
this.state.inputValue
),
_react2.default.createElement(_Input2.default, _extends({
width: this.props.width,
ref: function ref(input) {
return _this2.input = input;
},
onFocus: function onFocus(e) {
return _this2.handleInputFocus(e);
},
onBlur: function onBlur(e) {
return _this2.handleInputBlur(e);
},
placeholder: tags.length === 0 ? placeholder : ''
}, desiredProps, {
dataHook: 'inputWithTags-input',
disabled: disabled,
readOnly: isSelectMode,
onChange: function onChange(e) {
if (!delimiters.includes(e.target.value)) {
_this2.setState({ inputValue: e.target.value });
desiredProps.onChange && desiredProps.onChange(e);
}
},
withSelection: true
}))
),
(isSelectMode || error) && _react2.default.createElement(
'div',
{ className: _InputWithTags2.default.inputSuffix },
_react2.default.createElement(_InputSuffix2.default, {
disabled: disabled,
status: error && 'error',
statusMessage: errorMessage,
menuArrow: isSelectMode
})
)
);
}
}, {
key: 'renderReorderableTag',
value: function renderReorderableTag(_ref2) {
var _classNames3;
var _ref2$item = _ref2.item,
id = _ref2$item.id,
label = _ref2$item.label,
itemProps = _objectWithoutProperties(_ref2$item, ['id', 'label']),
previewStyles = _ref2.previewStyles,
isPlaceholder = _ref2.isPlaceholder,
isPreview = _ref2.isPreview,
rest = _objectWithoutProperties(_ref2, ['item', 'previewStyles', 'isPlaceholder', 'isPreview']);
var _props2 = this.props,
onRemoveTag = _props2.onRemoveTag,
disabled = _props2.disabled;
var classes = (0, _classnames2.default)(_InputWithTags2.default.tag, (_classNames3 = {}, _defineProperty(_classNames3, _dndStyles2.default.itemPlaceholder, isPlaceholder), _defineProperty(_classNames3, _InputWithTags2.default.draggedTagPlaceholder, isPlaceholder), _defineProperty(_classNames3, _dndStyles2.default.itemPreview, isPreview), _defineProperty(_classNames3, _InputWithTags2.default.draggedTag, isPreview), _classNames3));
return _react2.default.createElement(
'div',
{ style: previewStyles },
_react2.default.createElement(
_Tag2.default,
_extends({
id: id,
dataHook: 'tag',
disabled: disabled,
className: classes,
onRemove: onRemoveTag
}, itemProps, {
useOldMargins: false
}, rest),
label
)
);
}
}, {
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: 'clear',
value: function clear() {
this.input.clear();
}
}]);
return InputWithTags;
}(_react2.default.Component);
InputWithTags.propTypes = {
onRemoveTag: _propTypes2.default.func,
tags: _propTypes2.default.array,
onReorder: _propTypes2.default.func,
maxHeight: _propTypes2.default.string,
maxNumRows: _propTypes2.default.number,
onKeyDown: _propTypes2.default.func,
dataHook: _propTypes2.default.string,
placeholder: _propTypes2.default.string,
onFocus: _propTypes2.default.func,
onBlur: _propTypes2.default.func,
onInputClicked: _propTypes2.default.func,
autoFocus: _propTypes2.default.bool,
disabled: _propTypes2.default.bool,
error: _propTypes2.default.bool,
errorMessage: _propTypes2.default.string,
mode: _propTypes2.default.oneOf(['select']),
delimiters: _propTypes2.default.array,
width: _propTypes2.default.string
};
InputWithTags.defaultProps = {
onRemoveTag: function onRemoveTag() {},
tags: [],
placeholder: '',
delimiters: []
};
exports.default = InputWithTags;