wix-style-react
Version:
165 lines (131 loc) • 7.04 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["value", "customInput", "onChange"];
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; }
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; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import React from 'react';
import PropTypes from 'prop-types';
import CalendarIcon from 'wix-ui-icons-common/Date';
import Input from '../../Input';
import { formatDate, formatDateV2 } from '../../common/LocaleUtils/DateInputLocaleUtils';
import legacyParse from '../../common/LocaleUtils/legacyParse';
import { WixStyleReactEnvironmentContext } from '../../WixStyleReactEnvironmentProvider/context';
import deprecationLog from '../../utils/deprecationLog';
import { dateTimeFormat, supportedWixlocales } from 'wix-design-systems-locale-utils';
var DateInput = /*#__PURE__*/function (_React$PureComponent) {
_inherits(DateInput, _React$PureComponent);
var _super = _createSuper(DateInput);
function DateInput() {
var _this;
_classCallCheck(this, DateInput);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "_formatDateValue", function () {
var _this$props = _this.props,
value = _this$props.value,
dateFormat = _this$props.dateFormat,
dateFormatV2 = _this$props.dateFormatV2,
dateStyle = _this$props.dateStyle;
var locale = _this._getLocale();
if (!value) {
return '';
}
if (typeof value === 'string') {
return value;
}
if (dateFormatV2) {
if (typeof dateFormatV2 === 'function') {
return dateFormatV2(value);
}
return formatDateV2(value, dateFormatV2, locale);
}
if (dateFormat) {
if (typeof dateFormat === 'function') {
return dateFormat(value);
}
return formatDate(value, dateFormat, locale);
}
if (dateStyle === 'medium') {
return dateTimeFormat.getMediumDate(locale, value);
}
return dateTimeFormat.getShortDate(locale, value);
});
_defineProperty(_assertThisInitialized(_this), "_handleInputChange", function (event) {
var onChange = _this.props.onChange;
var val = event.target.value;
var dateObjectFormat = legacyParse(val);
var newVal = {
dateVal: !isNaN(dateObjectFormat) ? dateObjectFormat : new Date(),
textVal: val
};
onChange(newVal);
});
return _this;
}
_createClass(DateInput, [{
key: "_getLocale",
value: function _getLocale() {
if (typeof this.props.locale !== 'string') {
deprecationLog('<DateInput/> prop "locale" with value `date-fns locale object` is deprecated and will be removed in next major release, please pass a string instead');
}
return this.props.locale || this.context.locale || 'en';
}
}, {
key: "render",
value: function render() {
var _this$props2 = this.props,
initialValue = _this$props2.value,
customInput = _this$props2.customInput,
onChange = _this$props2.onChange,
rest = _objectWithoutProperties(_this$props2, _excluded);
var _inputProps = _objectSpread(_objectSpread({
focusOnClearClick: false,
value: this._formatDateValue(),
prefix: /*#__PURE__*/React.createElement(Input.IconAffix, {
dataHook: "date-input-date-icon"
}, /*#__PURE__*/React.createElement(CalendarIcon, null)),
autoSelect: false,
onChange: this._handleInputChange
}, rest), customInput ? customInput.props : {});
return /*#__PURE__*/React.cloneElement(customInput || /*#__PURE__*/React.createElement(Input, null), _inputProps);
}
}]);
return DateInput;
}(React.PureComponent);
_defineProperty(DateInput, "displayName", 'DateInput');
DateInput.contextType = WixStyleReactEnvironmentContext;
DateInput.propTypes = _objectSpread(_objectSpread({}, Input.propTypes), {}, {
/** The selected date */
value: PropTypes.oneOfType([PropTypes.object, PropTypes.string, PropTypes.number]),
/** Instance locale */
locale: PropTypes.oneOfType([PropTypes.oneOf(supportedWixlocales), PropTypes.shape({
code: PropTypes.string,
formatDistance: PropTypes.func,
formatRelative: PropTypes.func,
localize: PropTypes.object,
formatLong: PropTypes.object,
match: PropTypes.object,
options: PropTypes.object
})]),
/** Sets date format of locale */
dateStyle: PropTypes.oneOf(['short', 'medium']),
/** this prop is deprecated and should not be used
* @deprecated
*/
dateFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
/** this prop is deprecated and should not be used
* @deprecated
*/
dateFormatV2: PropTypes.oneOfType([PropTypes.string, PropTypes.func])
});
export default DateInput;