193 lines
8.2 KiB
JavaScript
193 lines
8.2 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
|
|
require('@babel/runtime/helpers/objectWithoutProperties');
|
|
var _extends = _interopDefault(require('@babel/runtime/helpers/extends'));
|
|
require('@babel/runtime/helpers/slicedToArray');
|
|
var _toConsumableArray = _interopDefault(require('@babel/runtime/helpers/toConsumableArray'));
|
|
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty'));
|
|
var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));
|
|
var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
|
|
require('@babel/runtime/helpers/assertThisInitialized');
|
|
var _inherits = _interopDefault(require('@babel/runtime/helpers/inherits'));
|
|
var _possibleConstructorReturn = _interopDefault(require('@babel/runtime/helpers/possibleConstructorReturn'));
|
|
var _getPrototypeOf = _interopDefault(require('@babel/runtime/helpers/getPrototypeOf'));
|
|
var React = require('react');
|
|
var React__default = _interopDefault(React);
|
|
require('memoize-one');
|
|
require('@emotion/core');
|
|
require('react-dom');
|
|
require('@babel/runtime/helpers/typeof');
|
|
var index$1 = require('../../dist/index-aee072ef.cjs.dev.js');
|
|
var reactSelect = require('../../dist/Select-d63d1b1b.cjs.dev.js');
|
|
require('@emotion/css');
|
|
require('@babel/runtime/helpers/taggedTemplateLiteral');
|
|
require('react-input-autosize');
|
|
var stateManager = require('../../dist/stateManager-71df4ad8.cjs.dev.js');
|
|
|
|
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 _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 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; }
|
|
|
|
var compareOption = function compareOption() {
|
|
var inputValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
var option = arguments.length > 1 ? arguments[1] : undefined;
|
|
var candidate = String(inputValue).toLowerCase();
|
|
var optionValue = String(option.value).toLowerCase();
|
|
var optionLabel = String(option.label).toLowerCase();
|
|
return optionValue === candidate || optionLabel === candidate;
|
|
};
|
|
|
|
var builtins = {
|
|
formatCreateLabel: function formatCreateLabel(inputValue) {
|
|
return "Create \"".concat(inputValue, "\"");
|
|
},
|
|
isValidNewOption: function isValidNewOption(inputValue, selectValue, selectOptions) {
|
|
return !(!inputValue || selectValue.some(function (option) {
|
|
return compareOption(inputValue, option);
|
|
}) || selectOptions.some(function (option) {
|
|
return compareOption(inputValue, option);
|
|
}));
|
|
},
|
|
getNewOptionData: function getNewOptionData(inputValue, optionLabel) {
|
|
return {
|
|
label: optionLabel,
|
|
value: inputValue,
|
|
__isNew__: true
|
|
};
|
|
}
|
|
};
|
|
var defaultProps = _objectSpread({
|
|
allowCreateWhileLoading: false,
|
|
createOptionPosition: 'last'
|
|
}, builtins);
|
|
var makeCreatableSelect = function makeCreatableSelect(SelectComponent) {
|
|
var _class, _temp;
|
|
|
|
return _temp = _class = /*#__PURE__*/function (_Component) {
|
|
_inherits(Creatable, _Component);
|
|
|
|
var _super = _createSuper(Creatable);
|
|
|
|
function Creatable(props) {
|
|
var _this;
|
|
|
|
_classCallCheck(this, Creatable);
|
|
|
|
_this = _super.call(this, props);
|
|
_this.select = void 0;
|
|
|
|
_this.onChange = function (newValue, actionMeta) {
|
|
var _this$props = _this.props,
|
|
getNewOptionData = _this$props.getNewOptionData,
|
|
inputValue = _this$props.inputValue,
|
|
isMulti = _this$props.isMulti,
|
|
onChange = _this$props.onChange,
|
|
onCreateOption = _this$props.onCreateOption,
|
|
value = _this$props.value,
|
|
name = _this$props.name;
|
|
|
|
if (actionMeta.action !== 'select-option') {
|
|
return onChange(newValue, actionMeta);
|
|
}
|
|
|
|
var newOption = _this.state.newOption;
|
|
var valueArray = Array.isArray(newValue) ? newValue : [newValue];
|
|
|
|
if (valueArray[valueArray.length - 1] === newOption) {
|
|
if (onCreateOption) onCreateOption(inputValue);else {
|
|
var newOptionData = getNewOptionData(inputValue, inputValue);
|
|
var newActionMeta = {
|
|
action: 'create-option',
|
|
name: name
|
|
};
|
|
|
|
if (isMulti) {
|
|
onChange([].concat(_toConsumableArray(index$1.cleanValue(value)), [newOptionData]), newActionMeta);
|
|
} else {
|
|
onChange(newOptionData, newActionMeta);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
onChange(newValue, actionMeta);
|
|
};
|
|
|
|
var options = props.options || [];
|
|
_this.state = {
|
|
newOption: undefined,
|
|
options: options
|
|
};
|
|
return _this;
|
|
}
|
|
|
|
_createClass(Creatable, [{
|
|
key: "UNSAFE_componentWillReceiveProps",
|
|
value: function UNSAFE_componentWillReceiveProps(nextProps) {
|
|
var allowCreateWhileLoading = nextProps.allowCreateWhileLoading,
|
|
createOptionPosition = nextProps.createOptionPosition,
|
|
formatCreateLabel = nextProps.formatCreateLabel,
|
|
getNewOptionData = nextProps.getNewOptionData,
|
|
inputValue = nextProps.inputValue,
|
|
isLoading = nextProps.isLoading,
|
|
isValidNewOption = nextProps.isValidNewOption,
|
|
value = nextProps.value;
|
|
var options = nextProps.options || [];
|
|
var newOption = this.state.newOption;
|
|
|
|
if (isValidNewOption(inputValue, index$1.cleanValue(value), options)) {
|
|
newOption = getNewOptionData(inputValue, formatCreateLabel(inputValue));
|
|
} else {
|
|
newOption = undefined;
|
|
}
|
|
|
|
this.setState({
|
|
newOption: newOption,
|
|
options: (allowCreateWhileLoading || !isLoading) && newOption ? createOptionPosition === 'first' ? [newOption].concat(_toConsumableArray(options)) : [].concat(_toConsumableArray(options), [newOption]) : options
|
|
});
|
|
}
|
|
}, {
|
|
key: "focus",
|
|
value: function focus() {
|
|
this.select.focus();
|
|
}
|
|
}, {
|
|
key: "blur",
|
|
value: function blur() {
|
|
this.select.blur();
|
|
}
|
|
}, {
|
|
key: "render",
|
|
value: function render() {
|
|
var _this2 = this;
|
|
|
|
var options = this.state.options;
|
|
return /*#__PURE__*/React__default.createElement(SelectComponent, _extends({}, this.props, {
|
|
ref: function ref(_ref) {
|
|
_this2.select = _ref;
|
|
},
|
|
options: options,
|
|
onChange: this.onChange
|
|
}));
|
|
}
|
|
}]);
|
|
|
|
return Creatable;
|
|
}(React.Component), _class.defaultProps = defaultProps, _temp;
|
|
}; // TODO: do this in package entrypoint
|
|
|
|
var SelectCreatable = makeCreatableSelect(reactSelect.Select);
|
|
var Creatable = stateManager.manageState(SelectCreatable);
|
|
|
|
exports.default = Creatable;
|
|
exports.defaultProps = defaultProps;
|
|
exports.makeCreatableSelect = makeCreatableSelect;
|