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.
 
 
 
 

135 lines
4.6 KiB

  1. 'use strict';
  2. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  3. var React = require('react');
  4. var React__default = _interopDefault(React);
  5. 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); }
  6. 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; }
  7. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  8. var defaultProps = {
  9. defaultInputValue: '',
  10. defaultMenuIsOpen: false,
  11. defaultValue: null
  12. };
  13. var manageState = function manageState(SelectComponent) {
  14. var _class, _temp;
  15. return _temp = _class =
  16. /*#__PURE__*/
  17. function (_Component) {
  18. _inheritsLoose(StateManager, _Component);
  19. function StateManager() {
  20. var _this;
  21. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  22. args[_key] = arguments[_key];
  23. }
  24. _this = _Component.call.apply(_Component, [this].concat(args)) || this;
  25. _this.select = void 0;
  26. _this.state = {
  27. inputValue: _this.props.inputValue !== undefined ? _this.props.inputValue : _this.props.defaultInputValue,
  28. menuIsOpen: _this.props.menuIsOpen !== undefined ? _this.props.menuIsOpen : _this.props.defaultMenuIsOpen,
  29. value: _this.props.value !== undefined ? _this.props.value : _this.props.defaultValue
  30. };
  31. _this.onChange = function (value, actionMeta) {
  32. _this.callProp('onChange', value, actionMeta);
  33. _this.setState({
  34. value: value
  35. });
  36. };
  37. _this.onInputChange = function (value, actionMeta) {
  38. // TODO: for backwards compatibility, we allow the prop to return a new
  39. // value, but now inputValue is a controllable prop we probably shouldn't
  40. var newValue = _this.callProp('onInputChange', value, actionMeta);
  41. _this.setState({
  42. inputValue: newValue !== undefined ? newValue : value
  43. });
  44. };
  45. _this.onMenuOpen = function () {
  46. _this.callProp('onMenuOpen');
  47. _this.setState({
  48. menuIsOpen: true
  49. });
  50. };
  51. _this.onMenuClose = function () {
  52. _this.callProp('onMenuClose');
  53. _this.setState({
  54. menuIsOpen: false
  55. });
  56. };
  57. return _this;
  58. }
  59. var _proto = StateManager.prototype;
  60. _proto.focus = function focus() {
  61. this.select.focus();
  62. };
  63. _proto.blur = function blur() {
  64. this.select.blur();
  65. } // FIXME: untyped flow code, return any
  66. ;
  67. _proto.getProp = function getProp(key) {
  68. return this.props[key] !== undefined ? this.props[key] : this.state[key];
  69. } // FIXME: untyped flow code, return any
  70. ;
  71. _proto.callProp = function callProp(name) {
  72. if (typeof this.props[name] === 'function') {
  73. var _this$props;
  74. for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  75. args[_key2 - 1] = arguments[_key2];
  76. }
  77. return (_this$props = this.props)[name].apply(_this$props, args);
  78. }
  79. };
  80. _proto.render = function render() {
  81. var _this2 = this;
  82. var _this$props2 = this.props,
  83. defaultInputValue = _this$props2.defaultInputValue,
  84. defaultMenuIsOpen = _this$props2.defaultMenuIsOpen,
  85. defaultValue = _this$props2.defaultValue,
  86. props = _objectWithoutPropertiesLoose(_this$props2, ["defaultInputValue", "defaultMenuIsOpen", "defaultValue"]);
  87. return React__default.createElement(SelectComponent, _extends({}, props, {
  88. ref: function ref(_ref) {
  89. _this2.select = _ref;
  90. },
  91. inputValue: this.getProp('inputValue'),
  92. menuIsOpen: this.getProp('menuIsOpen'),
  93. onChange: this.onChange,
  94. onInputChange: this.onInputChange,
  95. onMenuClose: this.onMenuClose,
  96. onMenuOpen: this.onMenuOpen,
  97. value: this.getProp('value')
  98. }));
  99. };
  100. return StateManager;
  101. }(React.Component), _class.defaultProps = defaultProps, _temp;
  102. };
  103. exports.manageState = manageState;