Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

238 Zeilen
8.3 KiB

  1. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  2. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  3. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  4. function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  5. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  6. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  7. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  8. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  9. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  10. import React, { Component } from 'react';
  11. import PropTypes from 'prop-types';
  12. import moment from 'moment';
  13. import classNames from 'classnames';
  14. var Header =
  15. /*#__PURE__*/
  16. function (_Component) {
  17. _inherits(Header, _Component);
  18. function Header(props) {
  19. var _this;
  20. _classCallCheck(this, Header);
  21. _this = _possibleConstructorReturn(this, _getPrototypeOf(Header).call(this, props));
  22. _defineProperty(_assertThisInitialized(_this), "onInputChange", function (event) {
  23. var str = event.target.value;
  24. _this.setState({
  25. str: str
  26. });
  27. var _this$props = _this.props,
  28. format = _this$props.format,
  29. hourOptions = _this$props.hourOptions,
  30. minuteOptions = _this$props.minuteOptions,
  31. secondOptions = _this$props.secondOptions,
  32. disabledHours = _this$props.disabledHours,
  33. disabledMinutes = _this$props.disabledMinutes,
  34. disabledSeconds = _this$props.disabledSeconds,
  35. onChange = _this$props.onChange;
  36. if (str) {
  37. var originalValue = _this.props.value;
  38. var value = _this.getProtoValue().clone();
  39. var parsed = moment(str, format, true);
  40. if (!parsed.isValid()) {
  41. _this.setState({
  42. invalid: true
  43. });
  44. return;
  45. }
  46. value.hour(parsed.hour()).minute(parsed.minute()).second(parsed.second()); // if time value not allowed, response warning.
  47. if (hourOptions.indexOf(value.hour()) < 0 || minuteOptions.indexOf(value.minute()) < 0 || secondOptions.indexOf(value.second()) < 0) {
  48. _this.setState({
  49. invalid: true
  50. });
  51. return;
  52. } // if time value is disabled, response warning.
  53. var disabledHourOptions = disabledHours();
  54. var disabledMinuteOptions = disabledMinutes(value.hour());
  55. var disabledSecondOptions = disabledSeconds(value.hour(), value.minute());
  56. if (disabledHourOptions && disabledHourOptions.indexOf(value.hour()) >= 0 || disabledMinuteOptions && disabledMinuteOptions.indexOf(value.minute()) >= 0 || disabledSecondOptions && disabledSecondOptions.indexOf(value.second()) >= 0) {
  57. _this.setState({
  58. invalid: true
  59. });
  60. return;
  61. }
  62. if (originalValue) {
  63. if (originalValue.hour() !== value.hour() || originalValue.minute() !== value.minute() || originalValue.second() !== value.second()) {
  64. // keep other fields for rc-calendar
  65. var changedValue = originalValue.clone();
  66. changedValue.hour(value.hour());
  67. changedValue.minute(value.minute());
  68. changedValue.second(value.second());
  69. onChange(changedValue);
  70. }
  71. } else if (originalValue !== value) {
  72. onChange(value);
  73. }
  74. } else {
  75. onChange(null);
  76. }
  77. _this.setState({
  78. invalid: false
  79. });
  80. });
  81. _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
  82. var _this$props2 = _this.props,
  83. onEsc = _this$props2.onEsc,
  84. onKeyDown = _this$props2.onKeyDown;
  85. if (e.keyCode === 27) {
  86. onEsc();
  87. }
  88. onKeyDown(e);
  89. });
  90. var _value = props.value,
  91. _format = props.format;
  92. _this.state = {
  93. str: _value && _value.format(_format) || '',
  94. invalid: false
  95. };
  96. return _this;
  97. }
  98. _createClass(Header, [{
  99. key: "componentDidMount",
  100. value: function componentDidMount() {
  101. var _this2 = this;
  102. var focusOnOpen = this.props.focusOnOpen;
  103. if (focusOnOpen) {
  104. // Wait one frame for the panel to be positioned before focusing
  105. var requestAnimationFrame = window.requestAnimationFrame || window.setTimeout;
  106. requestAnimationFrame(function () {
  107. _this2.refInput.focus();
  108. _this2.refInput.select();
  109. });
  110. }
  111. }
  112. }, {
  113. key: "componentDidUpdate",
  114. value: function componentDidUpdate(prevProps) {
  115. var _this$props3 = this.props,
  116. value = _this$props3.value,
  117. format = _this$props3.format;
  118. if (value !== prevProps.value) {
  119. // eslint-disable-next-line react/no-did-update-set-state
  120. this.setState({
  121. str: value && value.format(format) || '',
  122. invalid: false
  123. });
  124. }
  125. }
  126. }, {
  127. key: "getProtoValue",
  128. value: function getProtoValue() {
  129. var _this$props4 = this.props,
  130. value = _this$props4.value,
  131. defaultOpenValue = _this$props4.defaultOpenValue;
  132. return value || defaultOpenValue;
  133. }
  134. }, {
  135. key: "getInput",
  136. value: function getInput() {
  137. var _this3 = this;
  138. var _this$props5 = this.props,
  139. prefixCls = _this$props5.prefixCls,
  140. placeholder = _this$props5.placeholder,
  141. inputReadOnly = _this$props5.inputReadOnly;
  142. var _this$state = this.state,
  143. invalid = _this$state.invalid,
  144. str = _this$state.str;
  145. var invalidClass = invalid ? "".concat(prefixCls, "-input-invalid") : '';
  146. return React.createElement("input", {
  147. className: classNames("".concat(prefixCls, "-input"), invalidClass),
  148. ref: function ref(_ref) {
  149. _this3.refInput = _ref;
  150. },
  151. onKeyDown: this.onKeyDown,
  152. value: str,
  153. placeholder: placeholder,
  154. onChange: this.onInputChange,
  155. readOnly: !!inputReadOnly
  156. });
  157. }
  158. }, {
  159. key: "render",
  160. value: function render() {
  161. var prefixCls = this.props.prefixCls;
  162. return React.createElement("div", {
  163. className: "".concat(prefixCls, "-input-wrap")
  164. }, this.getInput());
  165. }
  166. }]);
  167. return Header;
  168. }(Component);
  169. _defineProperty(Header, "propTypes", {
  170. format: PropTypes.string,
  171. prefixCls: PropTypes.string,
  172. disabledDate: PropTypes.func,
  173. placeholder: PropTypes.string,
  174. clearText: PropTypes.string,
  175. value: PropTypes.object,
  176. inputReadOnly: PropTypes.bool,
  177. hourOptions: PropTypes.array,
  178. minuteOptions: PropTypes.array,
  179. secondOptions: PropTypes.array,
  180. disabledHours: PropTypes.func,
  181. disabledMinutes: PropTypes.func,
  182. disabledSeconds: PropTypes.func,
  183. onChange: PropTypes.func,
  184. onEsc: PropTypes.func,
  185. defaultOpenValue: PropTypes.object,
  186. currentSelectPanel: PropTypes.string,
  187. focusOnOpen: PropTypes.bool,
  188. onKeyDown: PropTypes.func,
  189. clearIcon: PropTypes.node
  190. });
  191. _defineProperty(Header, "defaultProps", {
  192. inputReadOnly: false
  193. });
  194. export default Header;