UNPKG

antd

Version:

An enterprise-class UI design language and React-based implementation

192 lines (151 loc) 6.87 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); var _defineProperty3 = _interopRequireDefault(_defineProperty2); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); exports.generateShowHourMinuteSecond = generateShowHourMinuteSecond; var _react = require('react'); var React = _interopRequireWildcard(_react); var _moment = require('moment'); var moment = _interopRequireWildcard(_moment); var _reactLifecyclesCompat = require('react-lifecycles-compat'); var _TimePicker = require('rc-time-picker/lib/TimePicker'); var _TimePicker2 = _interopRequireDefault(_TimePicker); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _LocaleReceiver = require('../locale-provider/LocaleReceiver'); var _LocaleReceiver2 = _interopRequireDefault(_LocaleReceiver); var _en_US = require('./locale/en_US'); var _en_US2 = _interopRequireDefault(_en_US); var _interopDefault = require('../_util/interopDefault'); var _interopDefault2 = _interopRequireDefault(_interopDefault); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function generateShowHourMinuteSecond(format) { // Ref: http://momentjs.com/docs/#/parsing/string-format/ return { showHour: format.indexOf('H') > -1 || format.indexOf('h') > -1 || format.indexOf('k') > -1, showMinute: format.indexOf('m') > -1, showSecond: format.indexOf('s') > -1 }; } var TimePicker = function (_React$Component) { (0, _inherits3['default'])(TimePicker, _React$Component); function TimePicker(props) { (0, _classCallCheck3['default'])(this, TimePicker); var _this = (0, _possibleConstructorReturn3['default'])(this, (TimePicker.__proto__ || Object.getPrototypeOf(TimePicker)).call(this, props)); _this.handleChange = function (value) { if (!('value' in _this.props)) { _this.setState({ value: value }); } var _this$props = _this.props, onChange = _this$props.onChange, _this$props$format = _this$props.format, format = _this$props$format === undefined ? 'HH:mm:ss' : _this$props$format; if (onChange) { onChange(value, value && value.format(format) || ''); } }; _this.handleOpenClose = function (_ref) { var open = _ref.open; var onOpenChange = _this.props.onOpenChange; if (onOpenChange) { onOpenChange(open); } }; _this.saveTimePicker = function (timePickerRef) { _this.timePickerRef = timePickerRef; }; _this.renderTimePicker = function (locale) { var props = (0, _extends3['default'])({}, _this.props); delete props.defaultValue; var format = _this.getDefaultFormat(); var className = (0, _classnames2['default'])(props.className, (0, _defineProperty3['default'])({}, props.prefixCls + '-' + props.size, !!props.size)); var addon = function addon(panel) { return props.addon ? React.createElement( 'div', { className: props.prefixCls + '-panel-addon' }, props.addon(panel) ) : null; }; return React.createElement(_TimePicker2['default'], (0, _extends3['default'])({}, generateShowHourMinuteSecond(format), props, { ref: _this.saveTimePicker, format: format, className: className, value: _this.state.value, placeholder: props.placeholder === undefined ? locale.placeholder : props.placeholder, onChange: _this.handleChange, onOpen: _this.handleOpenClose, onClose: _this.handleOpenClose, addon: addon })); }; var value = props.value || props.defaultValue; if (value && !(0, _interopDefault2['default'])(moment).isMoment(value)) { throw new Error('The value/defaultValue of TimePicker must be a moment object after `antd@2.0`, ' + 'see: https://u.ant.design/time-picker-value'); } _this.state = { value: value }; return _this; } (0, _createClass3['default'])(TimePicker, [{ key: 'focus', value: function focus() { this.timePickerRef.focus(); } }, { key: 'blur', value: function blur() { this.timePickerRef.blur(); } }, { key: 'getDefaultFormat', value: function getDefaultFormat() { var _props = this.props, format = _props.format, use12Hours = _props.use12Hours; if (format) { return format; } else if (use12Hours) { return 'h:mm:ss a'; } return 'HH:mm:ss'; } }, { key: 'render', value: function render() { return React.createElement( _LocaleReceiver2['default'], { componentName: 'TimePicker', defaultLocale: _en_US2['default'] }, this.renderTimePicker ); } }], [{ key: 'getDerivedStateFromProps', value: function getDerivedStateFromProps(nextProps) { if ('value' in nextProps) { return { value: nextProps.value }; } return null; } }]); return TimePicker; }(React.Component); TimePicker.defaultProps = { prefixCls: 'ant-time-picker', align: { offset: [0, -2] }, disabled: false, disabledHours: undefined, disabledMinutes: undefined, disabledSeconds: undefined, hideDisabledOptions: false, placement: 'bottomLeft', transitionName: 'slide-up', focusOnOpen: true }; (0, _reactLifecyclesCompat.polyfill)(TimePicker); exports['default'] = TimePicker;