Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

166 строки
5.5 KiB

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