No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

react-select.browser.esm.js 6.7 KiB

hace 3 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import React, { Component } from 'react';
  2. import 'memoize-one';
  3. import '@emotion/core';
  4. import 'react-dom';
  5. import 'prop-types';
  6. import { k as handleInputChange } from '../../dist/utils-06b0d5a4.browser.esm.js';
  7. import '../../dist/index-4322c0ed.browser.esm.js';
  8. import { S as Select } from '../../dist/Select-9fdb8cd0.browser.esm.js';
  9. import '@emotion/css';
  10. import 'react-input-autosize';
  11. import { m as manageState } from '../../dist/stateManager-04f734a2.browser.esm.js';
  12. 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); }
  13. function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
  14. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  15. var defaultProps = {
  16. cacheOptions: false,
  17. defaultOptions: false,
  18. filterOption: null,
  19. isLoading: false
  20. };
  21. var makeAsyncSelect = function makeAsyncSelect(SelectComponent) {
  22. var _class, _temp;
  23. return _temp = _class =
  24. /*#__PURE__*/
  25. function (_Component) {
  26. _inheritsLoose(Async, _Component);
  27. function Async(props) {
  28. var _this;
  29. _this = _Component.call(this) || this;
  30. _this.select = void 0;
  31. _this.lastRequest = void 0;
  32. _this.mounted = false;
  33. _this.optionsCache = {};
  34. _this.handleInputChange = function (newValue, actionMeta) {
  35. var _this$props = _this.props,
  36. cacheOptions = _this$props.cacheOptions,
  37. onInputChange = _this$props.onInputChange; // TODO
  38. var inputValue = handleInputChange(newValue, actionMeta, onInputChange);
  39. if (!inputValue) {
  40. delete _this.lastRequest;
  41. _this.setState({
  42. inputValue: '',
  43. loadedInputValue: '',
  44. loadedOptions: [],
  45. isLoading: false,
  46. passEmptyOptions: false
  47. });
  48. return;
  49. }
  50. if (cacheOptions && _this.optionsCache[inputValue]) {
  51. _this.setState({
  52. inputValue: inputValue,
  53. loadedInputValue: inputValue,
  54. loadedOptions: _this.optionsCache[inputValue],
  55. isLoading: false,
  56. passEmptyOptions: false
  57. });
  58. } else {
  59. var request = _this.lastRequest = {};
  60. _this.setState({
  61. inputValue: inputValue,
  62. isLoading: true,
  63. passEmptyOptions: !_this.state.loadedInputValue
  64. }, function () {
  65. _this.loadOptions(inputValue, function (options) {
  66. if (!_this.mounted) return;
  67. if (options) {
  68. _this.optionsCache[inputValue] = options;
  69. }
  70. if (request !== _this.lastRequest) return;
  71. delete _this.lastRequest;
  72. _this.setState({
  73. isLoading: false,
  74. loadedInputValue: inputValue,
  75. loadedOptions: options || [],
  76. passEmptyOptions: false
  77. });
  78. });
  79. });
  80. }
  81. return inputValue;
  82. };
  83. _this.state = {
  84. defaultOptions: Array.isArray(props.defaultOptions) ? props.defaultOptions : undefined,
  85. inputValue: typeof props.inputValue !== 'undefined' ? props.inputValue : '',
  86. isLoading: props.defaultOptions === true,
  87. loadedOptions: [],
  88. passEmptyOptions: false
  89. };
  90. return _this;
  91. }
  92. var _proto = Async.prototype;
  93. _proto.componentDidMount = function componentDidMount() {
  94. var _this2 = this;
  95. this.mounted = true;
  96. var defaultOptions = this.props.defaultOptions;
  97. var inputValue = this.state.inputValue;
  98. if (defaultOptions === true) {
  99. this.loadOptions(inputValue, function (options) {
  100. if (!_this2.mounted) return;
  101. var isLoading = !!_this2.lastRequest;
  102. _this2.setState({
  103. defaultOptions: options || [],
  104. isLoading: isLoading
  105. });
  106. });
  107. }
  108. };
  109. _proto.UNSAFE_componentWillReceiveProps = function UNSAFE_componentWillReceiveProps(nextProps) {
  110. // if the cacheOptions prop changes, clear the cache
  111. if (nextProps.cacheOptions !== this.props.cacheOptions) {
  112. this.optionsCache = {};
  113. }
  114. if (nextProps.defaultOptions !== this.props.defaultOptions) {
  115. this.setState({
  116. defaultOptions: Array.isArray(nextProps.defaultOptions) ? nextProps.defaultOptions : undefined
  117. });
  118. }
  119. };
  120. _proto.componentWillUnmount = function componentWillUnmount() {
  121. this.mounted = false;
  122. };
  123. _proto.focus = function focus() {
  124. this.select.focus();
  125. };
  126. _proto.blur = function blur() {
  127. this.select.blur();
  128. };
  129. _proto.loadOptions = function loadOptions(inputValue, callback) {
  130. var loadOptions = this.props.loadOptions;
  131. if (!loadOptions) return callback();
  132. var loader = loadOptions(inputValue, callback);
  133. if (loader && typeof loader.then === 'function') {
  134. loader.then(callback, function () {
  135. return callback();
  136. });
  137. }
  138. };
  139. _proto.render = function render() {
  140. var _this3 = this;
  141. var _this$props2 = this.props,
  142. loadOptions = _this$props2.loadOptions,
  143. isLoadingProp = _this$props2.isLoading,
  144. props = _objectWithoutPropertiesLoose(_this$props2, ["loadOptions", "isLoading"]);
  145. var _this$state = this.state,
  146. defaultOptions = _this$state.defaultOptions,
  147. inputValue = _this$state.inputValue,
  148. isLoading = _this$state.isLoading,
  149. loadedInputValue = _this$state.loadedInputValue,
  150. loadedOptions = _this$state.loadedOptions,
  151. passEmptyOptions = _this$state.passEmptyOptions;
  152. var options = passEmptyOptions ? [] : inputValue && loadedInputValue ? loadedOptions : defaultOptions || [];
  153. return React.createElement(SelectComponent, _extends({}, props, {
  154. ref: function ref(_ref) {
  155. _this3.select = _ref;
  156. },
  157. options: options,
  158. isLoading: isLoading || isLoadingProp,
  159. onInputChange: this.handleInputChange
  160. }));
  161. };
  162. return Async;
  163. }(Component), _class.defaultProps = defaultProps, _temp;
  164. };
  165. var SelectState = manageState(Select);
  166. var Async = makeAsyncSelect(SelectState);
  167. export default Async;
  168. export { defaultProps, makeAsyncSelect };