Panel.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _moment = require('moment');
  6. var _moment2 = _interopRequireDefault(_moment);
  7. var _vueTypes = require('../_util/vue-types');
  8. var _vueTypes2 = _interopRequireDefault(_vueTypes);
  9. var _BaseMixin = require('../_util/BaseMixin');
  10. var _BaseMixin2 = _interopRequireDefault(_BaseMixin);
  11. var _Header = require('./Header');
  12. var _Header2 = _interopRequireDefault(_Header);
  13. var _Combobox = require('./Combobox');
  14. var _Combobox2 = _interopRequireDefault(_Combobox);
  15. var _propsUtil = require('../_util/props-util');
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  17. function noop() {}
  18. function generateOptions(length, disabledOptions, hideDisabledOptions) {
  19. var step = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
  20. var arr = [];
  21. for (var value = 0; value < length; value += step) {
  22. if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) {
  23. arr.push(value);
  24. }
  25. }
  26. return arr;
  27. }
  28. function toNearestValidTime(time, hourOptions, minuteOptions, secondOptions) {
  29. var hour = hourOptions.slice().sort(function (a, b) {
  30. return Math.abs(time.hour() - a) - Math.abs(time.hour() - b);
  31. })[0];
  32. var minute = minuteOptions.slice().sort(function (a, b) {
  33. return Math.abs(time.minute() - a) - Math.abs(time.minute() - b);
  34. })[0];
  35. var second = secondOptions.slice().sort(function (a, b) {
  36. return Math.abs(time.second() - a) - Math.abs(time.second() - b);
  37. })[0];
  38. return (0, _moment2['default'])(hour + ':' + minute + ':' + second, 'HH:mm:ss');
  39. }
  40. var Panel = {
  41. mixins: [_BaseMixin2['default']],
  42. props: {
  43. clearText: _vueTypes2['default'].string,
  44. prefixCls: _vueTypes2['default'].string.def('rc-time-picker-panel'),
  45. defaultOpenValue: {
  46. type: Object,
  47. 'default': function _default() {
  48. return (0, _moment2['default'])();
  49. }
  50. },
  51. value: _vueTypes2['default'].any,
  52. defaultValue: _vueTypes2['default'].any,
  53. placeholder: _vueTypes2['default'].string,
  54. format: _vueTypes2['default'].string,
  55. inputReadOnly: _vueTypes2['default'].bool.def(false),
  56. disabledHours: _vueTypes2['default'].func.def(noop),
  57. disabledMinutes: _vueTypes2['default'].func.def(noop),
  58. disabledSeconds: _vueTypes2['default'].func.def(noop),
  59. hideDisabledOptions: _vueTypes2['default'].bool,
  60. // onChange: PropTypes.func,
  61. // onEsc: PropTypes.func,
  62. allowEmpty: _vueTypes2['default'].bool,
  63. showHour: _vueTypes2['default'].bool,
  64. showMinute: _vueTypes2['default'].bool,
  65. showSecond: _vueTypes2['default'].bool,
  66. // onClear: PropTypes.func,
  67. use12Hours: _vueTypes2['default'].bool.def(false),
  68. hourStep: _vueTypes2['default'].number,
  69. minuteStep: _vueTypes2['default'].number,
  70. secondStep: _vueTypes2['default'].number,
  71. addon: _vueTypes2['default'].func.def(noop),
  72. focusOnOpen: _vueTypes2['default'].bool,
  73. // onKeydown: PropTypes.func,
  74. clearIcon: _vueTypes2['default'].any
  75. },
  76. data: function data() {
  77. return {
  78. sValue: this.value,
  79. selectionRange: [],
  80. currentSelectPanel: ''
  81. };
  82. },
  83. watch: {
  84. value: function value(val) {
  85. this.setState({
  86. sValue: val
  87. });
  88. }
  89. },
  90. methods: {
  91. onChange: function onChange(newValue) {
  92. this.setState({ sValue: newValue });
  93. this.__emit('change', newValue);
  94. },
  95. onAmPmChange: function onAmPmChange(ampm) {
  96. this.__emit('amPmChange', ampm);
  97. },
  98. onCurrentSelectPanelChange: function onCurrentSelectPanelChange(currentSelectPanel) {
  99. this.setState({ currentSelectPanel: currentSelectPanel });
  100. },
  101. // https://github.com/ant-design/ant-design/issues/5829
  102. close: function close() {
  103. this.__emit('esc');
  104. },
  105. onEsc: function onEsc(e) {
  106. this.__emit('esc', e);
  107. },
  108. disabledHours2: function disabledHours2() {
  109. var use12Hours = this.use12Hours,
  110. disabledHours = this.disabledHours;
  111. var disabledOptions = disabledHours();
  112. if (use12Hours && Array.isArray(disabledOptions)) {
  113. if (this.isAM()) {
  114. disabledOptions = disabledOptions.filter(function (h) {
  115. return h < 12;
  116. }).map(function (h) {
  117. return h === 0 ? 12 : h;
  118. });
  119. } else {
  120. disabledOptions = disabledOptions.map(function (h) {
  121. return h === 12 ? 12 : h - 12;
  122. });
  123. }
  124. }
  125. return disabledOptions;
  126. },
  127. isAM: function isAM() {
  128. var value = this.sValue || this.defaultOpenValue;
  129. return value.hour() >= 0 && value.hour() < 12;
  130. }
  131. },
  132. render: function render() {
  133. var h = arguments[0];
  134. var prefixCls = this.prefixCls,
  135. placeholder = this.placeholder,
  136. disabledMinutes = this.disabledMinutes,
  137. addon = this.addon,
  138. disabledSeconds = this.disabledSeconds,
  139. hideDisabledOptions = this.hideDisabledOptions,
  140. showHour = this.showHour,
  141. showMinute = this.showMinute,
  142. showSecond = this.showSecond,
  143. format = this.format,
  144. defaultOpenValue = this.defaultOpenValue,
  145. clearText = this.clearText,
  146. use12Hours = this.use12Hours,
  147. focusOnOpen = this.focusOnOpen,
  148. hourStep = this.hourStep,
  149. minuteStep = this.minuteStep,
  150. secondStep = this.secondStep,
  151. inputReadOnly = this.inputReadOnly,
  152. sValue = this.sValue,
  153. currentSelectPanel = this.currentSelectPanel;
  154. var clearIcon = (0, _propsUtil.getComponentFromProp)(this, 'clearIcon');
  155. var _getListeners = (0, _propsUtil.getListeners)(this),
  156. _getListeners$esc = _getListeners.esc,
  157. esc = _getListeners$esc === undefined ? noop : _getListeners$esc,
  158. _getListeners$keydown = _getListeners.keydown,
  159. keydown = _getListeners$keydown === undefined ? noop : _getListeners$keydown;
  160. var disabledHourOptions = this.disabledHours2();
  161. var disabledMinuteOptions = disabledMinutes(sValue ? sValue.hour() : null);
  162. var disabledSecondOptions = disabledSeconds(sValue ? sValue.hour() : null, sValue ? sValue.minute() : null);
  163. var hourOptions = generateOptions(24, disabledHourOptions, hideDisabledOptions, hourStep);
  164. var minuteOptions = generateOptions(60, disabledMinuteOptions, hideDisabledOptions, minuteStep);
  165. var secondOptions = generateOptions(60, disabledSecondOptions, hideDisabledOptions, secondStep);
  166. var validDefaultOpenValue = toNearestValidTime(defaultOpenValue, hourOptions, minuteOptions, secondOptions);
  167. return h(
  168. 'div',
  169. { 'class': prefixCls + '-inner' },
  170. [h(_Header2['default'], {
  171. attrs: {
  172. clearText: clearText,
  173. prefixCls: prefixCls,
  174. defaultOpenValue: validDefaultOpenValue,
  175. value: sValue,
  176. currentSelectPanel: currentSelectPanel,
  177. format: format,
  178. placeholder: placeholder,
  179. hourOptions: hourOptions,
  180. minuteOptions: minuteOptions,
  181. secondOptions: secondOptions,
  182. disabledHours: this.disabledHours2,
  183. disabledMinutes: disabledMinutes,
  184. disabledSeconds: disabledSeconds,
  185. focusOnOpen: focusOnOpen,
  186. inputReadOnly: inputReadOnly,
  187. clearIcon: clearIcon
  188. },
  189. on: {
  190. 'esc': esc,
  191. 'change': this.onChange,
  192. 'keydown': keydown
  193. }
  194. }), h(_Combobox2['default'], {
  195. attrs: {
  196. prefixCls: prefixCls,
  197. value: sValue,
  198. defaultOpenValue: validDefaultOpenValue,
  199. format: format,
  200. showHour: showHour,
  201. showMinute: showMinute,
  202. showSecond: showSecond,
  203. hourOptions: hourOptions,
  204. minuteOptions: minuteOptions,
  205. secondOptions: secondOptions,
  206. disabledHours: this.disabledHours2,
  207. disabledMinutes: disabledMinutes,
  208. disabledSeconds: disabledSeconds,
  209. use12Hours: use12Hours,
  210. isAM: this.isAM()
  211. },
  212. on: {
  213. 'change': this.onChange,
  214. 'amPmChange': this.onAmPmChange,
  215. 'currentSelectPanelChange': this.onCurrentSelectPanelChange,
  216. 'esc': this.onEsc
  217. }
  218. }), addon(this)]
  219. );
  220. }
  221. };
  222. exports['default'] = Panel;