25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

130 satır
4.4 KiB

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