@bigfishtv/cockpit
Version:
182 lines (156 loc) • 6.61 kB
JavaScript
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 _class, _temp;
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; }
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import keycode from 'keycode';
import { titleCase } from '../../utils/stringUtils';
import Button from '../button/Button';
import AutosuggestInput from '../input/AutosuggestInput';
import Icon from '../Icon';
var DefaultCell = function DefaultCell(_ref) {
var defaultValue = _ref.defaultValue,
model = _ref.model,
onChange = _ref.onChange,
onEdit = _ref.onEdit;
return React.createElement(
'div',
{ className: 'cell', onDoubleClick: onEdit },
React.createElement(
'div',
{ className: 'cell-icon' },
React.createElement(Icon, { name: 'link', size: '18' })
),
React.createElement(
'div',
{ className: 'cell-content' },
defaultValue ? defaultValue : titleCase(model)
),
React.createElement(
'div',
{ className: 'cell-control' },
React.createElement(
Button,
{ size: 'icon', onClick: function onClick() {
return onChange(null);
} },
React.createElement(Icon, { name: 'close', size: '18' })
)
)
);
};
var AutosuggestModelInput = (_temp = _class = function (_Component) {
_inherits(AutosuggestModelInput, _Component);
function AutosuggestModelInput(props) {
_classCallCheck(this, AutosuggestModelInput);
var _this = _possibleConstructorReturn(this, _Component.call(this, props));
_this.handleKeyDown = function (event) {
var autoSaveInput = _this.props.autoSaveInput;
if (keycode(event) == 'enter' && autoSaveInput && _this.inputFocused && _this.state.input && !_this.suggestionsTotal) {
event.preventDefault();
_this.handleAdd();
}
};
_this.handleAdd = function () {
Promise.resolve(_this.props.autoSaveTransform(_this.state.input)).then(function (transformedInput) {
_this.handleSuggestion(transformedInput);
_this.props.onAdd(transformedInput);
}).catch(function (error) {
alert(error.message);
});
};
_this.inputFocused = false;
_this.suggestionsTotal = 0;
_this.state = { input: '' };
return _this;
}
AutosuggestModelInput.prototype.componentDidMount = function componentDidMount() {
window.addEventListener('keydown', this.handleKeyDown);
};
AutosuggestModelInput.prototype.handleSuggestion = function handleSuggestion(suggestion, event) {
this.props.onChange(suggestion);
};
AutosuggestModelInput.prototype.handleRemove = function handleRemove() {
if ('formValue' in this.props) this.props.formValue.update(null);else console.warn('[ModelAutosuggestInput] no onRemove prop provided');
};
AutosuggestModelInput.prototype.queryUrl = function queryUrl(props) {
return props.queryUrl ? props.queryUrl : props.model ? '/admin/' + props.model + '/search.json' : null;
};
AutosuggestModelInput.prototype.render = function render() {
var _this2 = this;
var _props = this.props,
Cell = _props.Cell,
model = _props.model,
onAdd = _props.onAdd,
renderSuggestion = _props.renderSuggestion,
autoSaveInput = _props.autoSaveInput,
autoSaveTransform = _props.autoSaveTransform,
onRemove = _props.onRemove,
rest = _objectWithoutProperties(_props, ['Cell', 'model', 'onAdd', 'renderSuggestion', 'autoSaveInput', 'autoSaveTransform', 'onRemove']);
if (!this.props.value) {
return React.createElement(
'div',
{ className: 'input-group' },
React.createElement(AutosuggestInput, _extends({}, rest, {
queryUrl: this.queryUrl(this.props),
onChange: this.props.onChange,
renderSuggestion: renderSuggestion,
onSuggestionsChange: function onSuggestionsChange(suggestions) {
return _this2.suggestionsTotal = suggestions.length;
},
onInputChange: function onInputChange(input) {
return _this2.setState({ input: input });
},
onInputFocus: function onInputFocus() {
return _this2.inputFocused = true;
},
onInputBlur: function onInputBlur() {
return _this2.inputFocused = false;
}
})),
autoSaveInput && this.state.input && !this.suggestionsTotal && React.createElement(Button, { text: 'Add', onClick: this.handleAdd })
);
}
if (model) {
return React.createElement(Cell, _extends({
value: this.props.value,
model: model,
onDoubleClick: this.props.onEdit,
onRemove: onRemove ? onRemove : function () {
return _this2.handleRemove();
}
}, rest));
}
return null;
};
return AutosuggestModelInput;
}(Component), _class.propTypes = {
model: PropTypes.string.isRequired,
queryUrl: PropTypes.string,
onChange: PropTypes.func,
autoSaveInput: PropTypes.bool,
autoSaveTransform: PropTypes.func
}, _class.defaultProps = {
Cell: DefaultCell,
onAdd: function onAdd() {
return console.warn('[ModelAutosuggestInput] no onAdd prop provided');
},
onEdit: function onEdit() {
return console.warn('[ModelAutosuggestInput] no onEdit prop provided');
},
renderSuggestion: function renderSuggestion(suggestion, input) {
return React.createElement(
'div',
null,
suggestion.name || suggestion.title || suggestion.toString()
);
},
autoSaveInput: false,
autoSaveTransform: function autoSaveTransform(value) {
return value;
}
}, _temp);
export { AutosuggestModelInput as default };