UNPKG

@seafile/seafile-calendar

Version:
235 lines (197 loc) 6.55 kB
import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; import React from 'react'; import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; import KeyCode from 'rc-util/es/KeyCode'; import { polyfill } from 'react-lifecycles-compat'; import dayjs from 'dayjs'; import { formatDate, initializeStr } from '../util'; import { CloseIcon } from '../icons'; var customParseFormat = require('dayjs/plugin/customParseFormat'); dayjs.extend(customParseFormat); var cachedSelectionStart = void 0; var cachedSelectionEnd = void 0; var dateInputInstance = void 0; var DateInput = function (_React$Component) { _inherits(DateInput, _React$Component); function DateInput(props) { _classCallCheck(this, DateInput); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _initialiseProps.call(_this); var selectedValue = props.selectedValue; _this.state = { str: formatDate(selectedValue, _this.props.format), hasFocus: false, isInputEmpty: false, localFormat: _this.props.format[0] }; return _this; } DateInput.prototype.componentDidMount = function componentDidMount() { var _this2 = this; setTimeout(function () { _this2.focus(); }, 100); }; DateInput.prototype.componentDidUpdate = function componentDidUpdate() { if (dateInputInstance && this.state.hasFocus && !(cachedSelectionStart === 0 && cachedSelectionEnd === 0)) { dateInputInstance.setSelectionRange(cachedSelectionStart, cachedSelectionEnd); } }; DateInput.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, state) { var newState = {}; if (dateInputInstance) { cachedSelectionStart = dateInputInstance.selectionStart; cachedSelectionEnd = dateInputInstance.selectionEnd; } // when popup show, click body will call this, bug! var selectedValue = nextProps.selectedValue; if (!state.hasFocus) { newState = { str: formatDate(selectedValue, nextProps.format) }; } return newState; }; DateInput.getInstance = function getInstance() { return dateInputInstance; }; DateInput.prototype.render = function render() { var props = this.props; var str = this.state.str; var locale = props.locale, prefixCls = props.prefixCls, placeholder = props.placeholder, clearIcon = props.clearIcon, inputMode = props.inputMode; return React.createElement( 'div', { className: prefixCls + '-input-wrap' }, React.createElement( 'div', { className: prefixCls + '-date-input-wrap' }, React.createElement('input', { id: 'date-input', ref: this.saveDateInput, className: prefixCls + '-input', value: str, disabled: props.disabled, placeholder: placeholder, onChange: this.onInputChange, onKeyDown: this.onKeyDown, onFocus: this.onFocus, onBlur: this.onBlur, inputMode: inputMode, tabIndex: '0' }) ), props.showClear && React.createElement( 'a', { role: 'button', title: locale.clear, onClick: this.onClear }, clearIcon || React.createElement( 'span', { className: prefixCls + '-clear-btn' }, React.createElement(CloseIcon, null) ) ) ); }; return DateInput; }(React.Component); DateInput.propTypes = { prefixCls: PropTypes.string, timePicker: PropTypes.object, value: PropTypes.object, disabledTime: PropTypes.any, format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]), locale: PropTypes.object, disabledDate: PropTypes.func, onChange: PropTypes.func, onClear: PropTypes.func, placeholder: PropTypes.string, onSelect: PropTypes.func, selectedValue: PropTypes.object, clearIcon: PropTypes.node, inputMode: PropTypes.string }; var _initialiseProps = function _initialiseProps() { var _this3 = this; this.onClear = function () { _this3.setState({ str: '' }); _this3.props.onClear(null); }; this.onInputChange = function (event) { var str = event.target.value; var calendarStr = initializeStr(str, _this3.state.localFormat) || ''; var _props = _this3.props, disabledDate = _props.disabledDate, format = _props.format, onChange = _props.onChange, selectedValue = _props.selectedValue; if (!str || !calendarStr) { _this3.setState({ isInputEmpty: true }); _this3.onClear(); return; } if (_this3.state.isInputEmpty) { _this3.setState({ isInputEmpty: false }); } var parsed = dayjs(calendarStr, format[0]); var value = _this3.props.value.clone(); value = value.year(parsed.year()).month(parsed.month()).date(parsed.date()).hour(parsed.hour()).minute(parsed.minute()).second(parsed.second()); if (!value || disabledDate && disabledDate(value)) { _this3.setState({ str: str }); return; } if (selectedValue !== value || selectedValue && value && !selectedValue.isSame(value)) { _this3.setState({ str: str }); onChange(value); } }; this.onFocus = function () { _this3.setState({ hasFocus: true }); }; this.onBlur = function () { _this3.setState(function (prevState, prevProps) { return { hasFocus: false, str: formatDate(prevProps.value, prevProps.format) }; }); }; this.onKeyDown = function (event) { var keyCode = event.keyCode; var _props2 = _this3.props, onSelect = _props2.onSelect, value = _props2.value, disabledDate = _props2.disabledDate; if (keyCode === KeyCode.ENTER && onSelect) { var validateDate = !disabledDate || !disabledDate(value); if (validateDate) { if (_this3.state.isInputEmpty) { onSelect(null); } else { onSelect(value.clone()); } } event.preventDefault(); } }; this.getRootDOMNode = function () { return ReactDOM.findDOMNode(_this3); }; this.focus = function () { if (dateInputInstance) { dateInputInstance.focus(); } }; this.saveDateInput = function (dateInput) { dateInputInstance = dateInput; }; }; polyfill(DateInput); export default DateInput;