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.

Header.js 9.9 KiB

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