You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

174 lines
5.9 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  4. var React = require('react');
  5. var React__default = _interopDefault(React);
  6. require('memoize-one');
  7. require('@emotion/core');
  8. require('react-dom');
  9. require('prop-types');
  10. var utils = require('../../dist/utils-03a02e63.cjs.dev.js');
  11. require('../../dist/index-503cf1e8.cjs.dev.js');
  12. var reactSelect = require('../../dist/Select-062d63ee.cjs.dev.js');
  13. require('@emotion/css');
  14. require('react-input-autosize');
  15. var stateManager = require('../../dist/stateManager-d41587a2.cjs.dev.js');
  16. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  17. 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); }
  18. var compareOption = function compareOption(inputValue, option) {
  19. if (inputValue === void 0) {
  20. inputValue = '';
  21. }
  22. var candidate = String(inputValue).toLowerCase();
  23. var optionValue = String(option.value).toLowerCase();
  24. var optionLabel = String(option.label).toLowerCase();
  25. return optionValue === candidate || optionLabel === candidate;
  26. };
  27. var builtins = {
  28. formatCreateLabel: function formatCreateLabel(inputValue) {
  29. return "Create \"" + inputValue + "\"";
  30. },
  31. isValidNewOption: function isValidNewOption(inputValue, selectValue, selectOptions) {
  32. return !(!inputValue || selectValue.some(function (option) {
  33. return compareOption(inputValue, option);
  34. }) || selectOptions.some(function (option) {
  35. return compareOption(inputValue, option);
  36. }));
  37. },
  38. getNewOptionData: function getNewOptionData(inputValue, optionLabel) {
  39. return {
  40. label: optionLabel,
  41. value: inputValue,
  42. __isNew__: true
  43. };
  44. }
  45. };
  46. var defaultProps = _extends({
  47. allowCreateWhileLoading: false,
  48. createOptionPosition: 'last'
  49. }, builtins);
  50. var makeCreatableSelect = function makeCreatableSelect(SelectComponent) {
  51. var _class, _temp;
  52. return _temp = _class =
  53. /*#__PURE__*/
  54. function (_Component) {
  55. _inheritsLoose(Creatable, _Component);
  56. function Creatable(props) {
  57. var _this;
  58. _this = _Component.call(this, props) || this;
  59. _this.select = void 0;
  60. _this.onChange = function (newValue, actionMeta) {
  61. var _this$props = _this.props,
  62. getNewOptionData = _this$props.getNewOptionData,
  63. inputValue = _this$props.inputValue,
  64. isMulti = _this$props.isMulti,
  65. onChange = _this$props.onChange,
  66. onCreateOption = _this$props.onCreateOption,
  67. value = _this$props.value,
  68. name = _this$props.name;
  69. if (actionMeta.action !== 'select-option') {
  70. return onChange(newValue, actionMeta);
  71. }
  72. var newOption = _this.state.newOption;
  73. var valueArray = Array.isArray(newValue) ? newValue : [newValue];
  74. if (valueArray[valueArray.length - 1] === newOption) {
  75. if (onCreateOption) onCreateOption(inputValue);else {
  76. var newOptionData = getNewOptionData(inputValue, inputValue);
  77. var newActionMeta = {
  78. action: 'create-option',
  79. name: name
  80. };
  81. if (isMulti) {
  82. onChange([].concat(utils.cleanValue(value), [newOptionData]), newActionMeta);
  83. } else {
  84. onChange(newOptionData, newActionMeta);
  85. }
  86. }
  87. return;
  88. }
  89. onChange(newValue, actionMeta);
  90. };
  91. var options = props.options || [];
  92. _this.state = {
  93. newOption: undefined,
  94. options: options
  95. };
  96. return _this;
  97. }
  98. var _proto = Creatable.prototype;
  99. _proto.UNSAFE_componentWillReceiveProps = function UNSAFE_componentWillReceiveProps(nextProps) {
  100. var allowCreateWhileLoading = nextProps.allowCreateWhileLoading,
  101. createOptionPosition = nextProps.createOptionPosition,
  102. formatCreateLabel = nextProps.formatCreateLabel,
  103. getNewOptionData = nextProps.getNewOptionData,
  104. inputValue = nextProps.inputValue,
  105. isLoading = nextProps.isLoading,
  106. isValidNewOption = nextProps.isValidNewOption,
  107. value = nextProps.value;
  108. var options = nextProps.options || [];
  109. var newOption = this.state.newOption;
  110. if (isValidNewOption(inputValue, utils.cleanValue(value), options)) {
  111. newOption = getNewOptionData(inputValue, formatCreateLabel(inputValue));
  112. } else {
  113. newOption = undefined;
  114. }
  115. this.setState({
  116. newOption: newOption,
  117. options: (allowCreateWhileLoading || !isLoading) && newOption ? createOptionPosition === 'first' ? [newOption].concat(options) : [].concat(options, [newOption]) : options
  118. });
  119. };
  120. _proto.focus = function focus() {
  121. this.select.focus();
  122. };
  123. _proto.blur = function blur() {
  124. this.select.blur();
  125. };
  126. _proto.render = function render() {
  127. var _this2 = this;
  128. var options = this.state.options;
  129. return React__default.createElement(SelectComponent, _extends({}, this.props, {
  130. ref: function ref(_ref) {
  131. _this2.select = _ref;
  132. },
  133. options: options,
  134. onChange: this.onChange
  135. }));
  136. };
  137. return Creatable;
  138. }(React.Component), _class.defaultProps = defaultProps, _temp;
  139. }; // TODO: do this in package entrypoint
  140. var SelectCreatable = makeCreatableSelect(reactSelect.Select);
  141. var Creatable = stateManager.manageState(SelectCreatable);
  142. exports.default = Creatable;
  143. exports.defaultProps = defaultProps;
  144. exports.makeCreatableSelect = makeCreatableSelect;