Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

465 řádky
16 KiB

  1. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  2. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. 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); } }
  5. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  6. function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  7. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  8. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  9. 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); }
  10. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  11. 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; }
  12. /* eslint jsx-a11y/no-autofocus: 0 */
  13. import React, { Component } from 'react';
  14. import PropTypes from 'prop-types';
  15. import Trigger from 'rc-trigger';
  16. import moment from 'moment';
  17. import { polyfill } from 'react-lifecycles-compat';
  18. import classNames from 'classnames';
  19. import Panel from './Panel';
  20. import placements from './placements';
  21. function noop() {}
  22. function refFn(field, component) {
  23. this[field] = component;
  24. }
  25. var Picker =
  26. /*#__PURE__*/
  27. function (_Component) {
  28. _inherits(Picker, _Component);
  29. function Picker(props) {
  30. var _this;
  31. _classCallCheck(this, Picker);
  32. _this = _possibleConstructorReturn(this, _getPrototypeOf(Picker).call(this, props));
  33. _defineProperty(_assertThisInitialized(_this), "onPanelChange", function (value) {
  34. _this.setValue(value);
  35. });
  36. _defineProperty(_assertThisInitialized(_this), "onAmPmChange", function (ampm) {
  37. var onAmPmChange = _this.props.onAmPmChange;
  38. onAmPmChange(ampm);
  39. });
  40. _defineProperty(_assertThisInitialized(_this), "onClear", function (event) {
  41. event.stopPropagation();
  42. _this.setValue(null);
  43. _this.setOpen(false);
  44. });
  45. _defineProperty(_assertThisInitialized(_this), "onVisibleChange", function (open) {
  46. _this.setOpen(open);
  47. });
  48. _defineProperty(_assertThisInitialized(_this), "onEsc", function () {
  49. _this.setOpen(false);
  50. _this.focus();
  51. });
  52. _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
  53. if (e.keyCode === 40) {
  54. _this.setOpen(true);
  55. }
  56. });
  57. _this.saveInputRef = refFn.bind(_assertThisInitialized(_this), 'picker');
  58. _this.savePanelRef = refFn.bind(_assertThisInitialized(_this), 'panelInstance');
  59. var defaultOpen = props.defaultOpen,
  60. defaultValue = props.defaultValue,
  61. _props$open = props.open,
  62. _open = _props$open === void 0 ? defaultOpen : _props$open,
  63. _props$value = props.value,
  64. _value = _props$value === void 0 ? defaultValue : _props$value;
  65. _this.state = {
  66. open: _open,
  67. value: _value
  68. };
  69. return _this;
  70. }
  71. _createClass(Picker, [{
  72. key: "setValue",
  73. value: function setValue(value) {
  74. var onChange = this.props.onChange;
  75. if (!('value' in this.props)) {
  76. this.setState({
  77. value: value
  78. });
  79. }
  80. onChange(value);
  81. }
  82. }, {
  83. key: "getFormat",
  84. value: function getFormat() {
  85. var _this$props = this.props,
  86. format = _this$props.format,
  87. showHour = _this$props.showHour,
  88. showMinute = _this$props.showMinute,
  89. showSecond = _this$props.showSecond,
  90. use12Hours = _this$props.use12Hours;
  91. if (format) {
  92. return format;
  93. }
  94. if (use12Hours) {
  95. var fmtString = [showHour ? 'h' : '', showMinute ? 'mm' : '', showSecond ? 'ss' : ''].filter(function (item) {
  96. return !!item;
  97. }).join(':');
  98. return fmtString.concat(' a');
  99. }
  100. return [showHour ? 'HH' : '', showMinute ? 'mm' : '', showSecond ? 'ss' : ''].filter(function (item) {
  101. return !!item;
  102. }).join(':');
  103. }
  104. }, {
  105. key: "getPanelElement",
  106. value: function getPanelElement() {
  107. var _this$props2 = this.props,
  108. prefixCls = _this$props2.prefixCls,
  109. placeholder = _this$props2.placeholder,
  110. disabledHours = _this$props2.disabledHours,
  111. disabledMinutes = _this$props2.disabledMinutes,
  112. disabledSeconds = _this$props2.disabledSeconds,
  113. hideDisabledOptions = _this$props2.hideDisabledOptions,
  114. inputReadOnly = _this$props2.inputReadOnly,
  115. showHour = _this$props2.showHour,
  116. showMinute = _this$props2.showMinute,
  117. showSecond = _this$props2.showSecond,
  118. defaultOpenValue = _this$props2.defaultOpenValue,
  119. clearText = _this$props2.clearText,
  120. addon = _this$props2.addon,
  121. use12Hours = _this$props2.use12Hours,
  122. focusOnOpen = _this$props2.focusOnOpen,
  123. onKeyDown = _this$props2.onKeyDown,
  124. hourStep = _this$props2.hourStep,
  125. minuteStep = _this$props2.minuteStep,
  126. secondStep = _this$props2.secondStep,
  127. clearIcon = _this$props2.clearIcon;
  128. var value = this.state.value;
  129. return React.createElement(Panel, {
  130. clearText: clearText,
  131. prefixCls: "".concat(prefixCls, "-panel"),
  132. ref: this.savePanelRef,
  133. value: value,
  134. inputReadOnly: inputReadOnly,
  135. onChange: this.onPanelChange,
  136. onAmPmChange: this.onAmPmChange,
  137. defaultOpenValue: defaultOpenValue,
  138. showHour: showHour,
  139. showMinute: showMinute,
  140. showSecond: showSecond,
  141. onEsc: this.onEsc,
  142. format: this.getFormat(),
  143. placeholder: placeholder,
  144. disabledHours: disabledHours,
  145. disabledMinutes: disabledMinutes,
  146. disabledSeconds: disabledSeconds,
  147. hideDisabledOptions: hideDisabledOptions,
  148. use12Hours: use12Hours,
  149. hourStep: hourStep,
  150. minuteStep: minuteStep,
  151. secondStep: secondStep,
  152. addon: addon,
  153. focusOnOpen: focusOnOpen,
  154. onKeyDown: onKeyDown,
  155. clearIcon: clearIcon
  156. });
  157. }
  158. }, {
  159. key: "getPopupClassName",
  160. value: function getPopupClassName() {
  161. var _this$props3 = this.props,
  162. showHour = _this$props3.showHour,
  163. showMinute = _this$props3.showMinute,
  164. showSecond = _this$props3.showSecond,
  165. use12Hours = _this$props3.use12Hours,
  166. prefixCls = _this$props3.prefixCls,
  167. popupClassName = _this$props3.popupClassName;
  168. var selectColumnCount = 0;
  169. if (showHour) {
  170. selectColumnCount += 1;
  171. }
  172. if (showMinute) {
  173. selectColumnCount += 1;
  174. }
  175. if (showSecond) {
  176. selectColumnCount += 1;
  177. }
  178. if (use12Hours) {
  179. selectColumnCount += 1;
  180. } // Keep it for old compatibility
  181. return classNames(popupClassName, _defineProperty({}, "".concat(prefixCls, "-panel-narrow"), (!showHour || !showMinute || !showSecond) && !use12Hours), "".concat(prefixCls, "-panel-column-").concat(selectColumnCount));
  182. }
  183. }, {
  184. key: "setOpen",
  185. value: function setOpen(open) {
  186. var _this$props4 = this.props,
  187. onOpen = _this$props4.onOpen,
  188. onClose = _this$props4.onClose;
  189. var currentOpen = this.state.open;
  190. if (currentOpen !== open) {
  191. if (!('open' in this.props)) {
  192. this.setState({
  193. open: open
  194. });
  195. }
  196. if (open) {
  197. onOpen({
  198. open: open
  199. });
  200. } else {
  201. onClose({
  202. open: open
  203. });
  204. }
  205. }
  206. }
  207. }, {
  208. key: "focus",
  209. value: function focus() {
  210. this.picker.focus();
  211. }
  212. }, {
  213. key: "blur",
  214. value: function blur() {
  215. this.picker.blur();
  216. }
  217. }, {
  218. key: "renderClearButton",
  219. value: function renderClearButton() {
  220. var _this2 = this;
  221. var value = this.state.value;
  222. var _this$props5 = this.props,
  223. prefixCls = _this$props5.prefixCls,
  224. allowEmpty = _this$props5.allowEmpty,
  225. clearIcon = _this$props5.clearIcon,
  226. clearText = _this$props5.clearText,
  227. disabled = _this$props5.disabled;
  228. if (!allowEmpty || !value || disabled) {
  229. return null;
  230. }
  231. if (React.isValidElement(clearIcon)) {
  232. var _ref = clearIcon.props || {},
  233. _onClick = _ref.onClick;
  234. return React.cloneElement(clearIcon, {
  235. onClick: function onClick() {
  236. if (_onClick) _onClick.apply(void 0, arguments);
  237. _this2.onClear.apply(_this2, arguments);
  238. }
  239. });
  240. }
  241. return React.createElement("a", {
  242. role: "button",
  243. className: "".concat(prefixCls, "-clear"),
  244. title: clearText,
  245. onClick: this.onClear,
  246. tabIndex: 0
  247. }, clearIcon || React.createElement("i", {
  248. className: "".concat(prefixCls, "-clear-icon")
  249. }));
  250. }
  251. }, {
  252. key: "render",
  253. value: function render() {
  254. var _this$props6 = this.props,
  255. prefixCls = _this$props6.prefixCls,
  256. placeholder = _this$props6.placeholder,
  257. placement = _this$props6.placement,
  258. align = _this$props6.align,
  259. id = _this$props6.id,
  260. disabled = _this$props6.disabled,
  261. transitionName = _this$props6.transitionName,
  262. style = _this$props6.style,
  263. className = _this$props6.className,
  264. getPopupContainer = _this$props6.getPopupContainer,
  265. name = _this$props6.name,
  266. autoComplete = _this$props6.autoComplete,
  267. onFocus = _this$props6.onFocus,
  268. onBlur = _this$props6.onBlur,
  269. autoFocus = _this$props6.autoFocus,
  270. inputReadOnly = _this$props6.inputReadOnly,
  271. inputIcon = _this$props6.inputIcon,
  272. popupStyle = _this$props6.popupStyle;
  273. var _this$state = this.state,
  274. open = _this$state.open,
  275. value = _this$state.value;
  276. var popupClassName = this.getPopupClassName();
  277. return React.createElement(Trigger, {
  278. prefixCls: "".concat(prefixCls, "-panel"),
  279. popupClassName: popupClassName,
  280. popupStyle: popupStyle,
  281. popup: this.getPanelElement(),
  282. popupAlign: align,
  283. builtinPlacements: placements,
  284. popupPlacement: placement,
  285. action: disabled ? [] : ['click'],
  286. destroyPopupOnHide: true,
  287. getPopupContainer: getPopupContainer,
  288. popupTransitionName: transitionName,
  289. popupVisible: open,
  290. onPopupVisibleChange: this.onVisibleChange
  291. }, React.createElement("span", {
  292. className: classNames(prefixCls, className),
  293. style: style
  294. }, React.createElement("input", {
  295. className: "".concat(prefixCls, "-input"),
  296. ref: this.saveInputRef,
  297. type: "text",
  298. placeholder: placeholder,
  299. name: name,
  300. onKeyDown: this.onKeyDown,
  301. disabled: disabled,
  302. value: value && value.format(this.getFormat()) || '',
  303. autoComplete: autoComplete,
  304. onFocus: onFocus,
  305. onBlur: onBlur,
  306. autoFocus: autoFocus,
  307. onChange: noop,
  308. readOnly: !!inputReadOnly,
  309. id: id
  310. }), inputIcon || React.createElement("span", {
  311. className: "".concat(prefixCls, "-icon")
  312. }), this.renderClearButton()));
  313. }
  314. }], [{
  315. key: "getDerivedStateFromProps",
  316. value: function getDerivedStateFromProps(props, state) {
  317. var newState = {};
  318. if ('value' in props) {
  319. newState.value = props.value;
  320. }
  321. if (props.open !== undefined) {
  322. newState.open = props.open;
  323. }
  324. return Object.keys(newState).length > 0 ? _objectSpread({}, state, {}, newState) : null;
  325. }
  326. }]);
  327. return Picker;
  328. }(Component);
  329. _defineProperty(Picker, "propTypes", {
  330. prefixCls: PropTypes.string,
  331. clearText: PropTypes.string,
  332. value: PropTypes.object,
  333. defaultOpenValue: PropTypes.object,
  334. inputReadOnly: PropTypes.bool,
  335. disabled: PropTypes.bool,
  336. allowEmpty: PropTypes.bool,
  337. defaultValue: PropTypes.object,
  338. open: PropTypes.bool,
  339. defaultOpen: PropTypes.bool,
  340. align: PropTypes.object,
  341. placement: PropTypes.any,
  342. transitionName: PropTypes.string,
  343. getPopupContainer: PropTypes.func,
  344. placeholder: PropTypes.string,
  345. format: PropTypes.string,
  346. showHour: PropTypes.bool,
  347. showMinute: PropTypes.bool,
  348. showSecond: PropTypes.bool,
  349. style: PropTypes.object,
  350. className: PropTypes.string,
  351. popupClassName: PropTypes.string,
  352. popupStyle: PropTypes.object,
  353. disabledHours: PropTypes.func,
  354. disabledMinutes: PropTypes.func,
  355. disabledSeconds: PropTypes.func,
  356. hideDisabledOptions: PropTypes.bool,
  357. onChange: PropTypes.func,
  358. onAmPmChange: PropTypes.func,
  359. onOpen: PropTypes.func,
  360. onClose: PropTypes.func,
  361. onFocus: PropTypes.func,
  362. onBlur: PropTypes.func,
  363. addon: PropTypes.func,
  364. name: PropTypes.string,
  365. autoComplete: PropTypes.string,
  366. use12Hours: PropTypes.bool,
  367. hourStep: PropTypes.number,
  368. minuteStep: PropTypes.number,
  369. secondStep: PropTypes.number,
  370. focusOnOpen: PropTypes.bool,
  371. onKeyDown: PropTypes.func,
  372. autoFocus: PropTypes.bool,
  373. id: PropTypes.string,
  374. inputIcon: PropTypes.node,
  375. clearIcon: PropTypes.node
  376. });
  377. _defineProperty(Picker, "defaultProps", {
  378. clearText: 'clear',
  379. prefixCls: 'rc-time-picker',
  380. defaultOpen: false,
  381. inputReadOnly: false,
  382. style: {},
  383. className: '',
  384. popupClassName: '',
  385. popupStyle: {},
  386. align: {},
  387. defaultOpenValue: moment(),
  388. allowEmpty: true,
  389. showHour: true,
  390. showMinute: true,
  391. showSecond: true,
  392. disabledHours: noop,
  393. disabledMinutes: noop,
  394. disabledSeconds: noop,
  395. hideDisabledOptions: false,
  396. placement: 'bottomLeft',
  397. onChange: noop,
  398. onAmPmChange: noop,
  399. onOpen: noop,
  400. onClose: noop,
  401. onFocus: noop,
  402. onBlur: noop,
  403. addon: noop,
  404. use12Hours: false,
  405. focusOnOpen: false,
  406. onKeyDown: noop
  407. });
  408. polyfill(Picker);
  409. export default Picker;