UNPKG

@elastic/eui

Version:

Elastic UI Component Library

214 lines (210 loc) 10.5 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.euiDatePickerDefaultTimeFormat = exports.euiDatePickerDefaultDateFormat = exports.EuiDatePicker = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _form = require("../form"); var _num_icons = require("../form/form_control_layout/_num_icons"); var _services = require("../../services"); var _context = require("../context"); var _reactDatepicker = require("./react-datepicker"); var _react2 = require("@emotion/react"); var _excluded = ["adjustDateOnChange", "calendarClassName", "className", "controlOnly", "customInput", "dateFormat", "dayClassName", "disabled", "excludeDates", "filterDate", "fullWidth", "iconType", "injectTimes", "inline", "inputRef", "isInvalid", "isLoading", "locale", "maxDate", "maxTime", "minDate", "minTime", "onChange", "onClear", "openToDate", "placeholder", "popperClassName", "popoverPlacement", "readOnly", "selected", "shadow", "shouldCloseOnSelect", "showIcon", "showTimeSelect", "showTimeSelectOnly", "timeFormat", "utcOffset"]; /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ // eslint-disable-line import/named function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var euiDatePickerDefaultDateFormat = 'MM/DD/YYYY'; exports.euiDatePickerDefaultDateFormat = euiDatePickerDefaultDateFormat; var euiDatePickerDefaultTimeFormat = 'hh:mm A'; // EuiDatePicker only supports a subset of props from react-datepicker. exports.euiDatePickerDefaultTimeFormat = euiDatePickerDefaultTimeFormat; var unsupportedProps = [ // We don't want to show multiple months next to each other 'monthsShown', // There is no need to show week numbers 'showWeekNumbers', // Our css adapts to height, no need to fix it 'fixedHeight', // We force the month / year selection UI. No need to configure it 'dropdownMode', // Short month is unnecessary. Our UI has plenty of room for full months 'useShortMonthInDropdown', // The today button is not needed. This should always be external to the calendar 'todayButton', // We hide the time caption, so there is no need to overwrite its text 'timeCaption', // We always want keyboard accessibility on 'disabledKeyboardNavigation', // This is easy enough to do. It can conflict with isLoading state 'isClearable', // There is no reason to launch the datepicker in its own modal. Can always build these ourselves 'withPortal', // Causes Error: Cannot read property 'clone' of undefined 'showMonthYearDropdown', // We overridde this with `popoverPlacement` 'popperPlacement']; var EuiDatePicker = function EuiDatePicker(_ref) { var _ref$adjustDateOnChan = _ref.adjustDateOnChange, adjustDateOnChange = _ref$adjustDateOnChan === void 0 ? true : _ref$adjustDateOnChan, calendarClassName = _ref.calendarClassName, className = _ref.className, controlOnly = _ref.controlOnly, customInput = _ref.customInput, _ref$dateFormat = _ref.dateFormat, dateFormat = _ref$dateFormat === void 0 ? euiDatePickerDefaultDateFormat : _ref$dateFormat, dayClassName = _ref.dayClassName, disabled = _ref.disabled, excludeDates = _ref.excludeDates, filterDate = _ref.filterDate, _ref$fullWidth = _ref.fullWidth, fullWidth = _ref$fullWidth === void 0 ? false : _ref$fullWidth, iconType = _ref.iconType, injectTimes = _ref.injectTimes, inline = _ref.inline, inputRef = _ref.inputRef, isInvalid = _ref.isInvalid, isLoading = _ref.isLoading, locale = _ref.locale, maxDate = _ref.maxDate, maxTime = _ref.maxTime, minDate = _ref.minDate, minTime = _ref.minTime, onChange = _ref.onChange, onClear = _ref.onClear, openToDate = _ref.openToDate, placeholder = _ref.placeholder, popperClassName = _ref.popperClassName, _ref$popoverPlacement = _ref.popoverPlacement, popoverPlacement = _ref$popoverPlacement === void 0 ? 'downLeft' : _ref$popoverPlacement, readOnly = _ref.readOnly, selected = _ref.selected, _ref$shadow = _ref.shadow, shadow = _ref$shadow === void 0 ? true : _ref$shadow, _ref$shouldCloseOnSel = _ref.shouldCloseOnSelect, shouldCloseOnSelect = _ref$shouldCloseOnSel === void 0 ? true : _ref$shouldCloseOnSel, _ref$showIcon = _ref.showIcon, showIcon = _ref$showIcon === void 0 ? true : _ref$showIcon, _ref$showTimeSelect = _ref.showTimeSelect, showTimeSelect = _ref$showTimeSelect === void 0 ? false : _ref$showTimeSelect, showTimeSelectOnly = _ref.showTimeSelectOnly, _ref$timeFormat = _ref.timeFormat, timeFormat = _ref$timeFormat === void 0 ? euiDatePickerDefaultTimeFormat : _ref$timeFormat, utcOffset = _ref.utcOffset, rest = (0, _objectWithoutProperties2.default)(_ref, _excluded); var classes = (0, _classnames.default)('euiDatePicker', { 'euiDatePicker--inline': inline, 'euiDatePicker--shadow': inline && shadow }); var numIconsClass = controlOnly ? false : (0, _num_icons.getFormControlClassNameForIconCount)({ isInvalid: isInvalid, isLoading: isLoading }); var datePickerClasses = (0, _classnames.default)('euiDatePicker', 'euiFieldText', numIconsClass, { 'euiFieldText--fullWidth': fullWidth, 'euiFieldText-isLoading': isLoading, 'euiFieldText--withIcon': !inline && showIcon, 'euiFieldText--isClearable': !inline && selected && onClear }, className); var optionalIcon; if (inline || customInput || !showIcon) { optionalIcon = undefined; } else if (iconType) { optionalIcon = iconType; } else if (showTimeSelectOnly) { optionalIcon = 'clock'; } else { optionalIcon = 'calendar'; } // In case the consumer did not alter the default date format but wants // to add the time select, we append the default time format var fullDateFormat = dateFormat; if (showTimeSelect && dateFormat === euiDatePickerDefaultDateFormat) { fullDateFormat = "".concat(dateFormat, " ").concat(timeFormat); } // Set an internal ref on ReactDatePicker's `input` so we can set its :invalid state via useEuiValidatableControl var _useState = (0, _react.useState)(null), _useState2 = (0, _slicedToArray2.default)(_useState, 2), inputValidityRef = _useState2[0], _setInputValidityRef = _useState2[1]; var setInputValidityRef = (0, _react.useCallback)(function (ref) { _setInputValidityRef(ref === null || ref === void 0 ? void 0 : ref.input); }, []); (0, _form.useEuiValidatableControl)({ isInvalid: isInvalid, controlEl: inputValidityRef }); var inputRefs = (0, _services.useCombinedRefs)([inputRef, setInputValidityRef]); var control = (0, _react2.jsx)(_context.EuiI18nConsumer, null, function (_ref2) { var contextLocale = _ref2.locale; return (0, _react2.jsx)(_reactDatepicker.ReactDatePicker, (0, _extends2.default)({ adjustDateOnChange: adjustDateOnChange, calendarClassName: calendarClassName, className: datePickerClasses, customInput: customInput, dateFormat: fullDateFormat, dayClassName: dayClassName, disabled: disabled, readOnly: readOnly, excludeDates: excludeDates, filterDate: filterDate, injectTimes: injectTimes, inline: inline, locale: locale || contextLocale, maxDate: maxDate, maxTime: maxTime, minDate: minDate, minTime: minTime, onChange: onChange, openToDate: openToDate, placeholderText: placeholder, popperClassName: popperClassName, ref: inputRefs, selected: selected, shouldCloseOnSelect: shouldCloseOnSelect, showMonthDropdown: true, showTimeSelect: showTimeSelectOnly ? true : showTimeSelect, showTimeSelectOnly: showTimeSelectOnly, showYearDropdown: true, timeFormat: timeFormat, utcOffset: utcOffset, yearDropdownItemNumber: 7, accessibleMode: !(disabled || readOnly), popperPlacement: popoverPlacement }, rest)); }); if (controlOnly) return control; return (0, _react2.jsx)("span", { className: classes }, (0, _react2.jsx)(_form.EuiFormControlLayout, { icon: optionalIcon, fullWidth: fullWidth, clear: selected && onClear ? { onClick: onClear } : undefined, isLoading: isLoading, isInvalid: isInvalid, isDisabled: disabled, readOnly: readOnly, className: (0, _classnames.default)({ // Take advantage of `euiFormControlLayoutDelimited`'s replacement input styling euiFormControlLayoutDelimited: inline, 'euiFormControlLayoutDelimited--isInvalid': inline && isInvalid && !disabled && !readOnly }), iconsPosition: inline ? 'static' : undefined }, control)); }; exports.EuiDatePicker = EuiDatePicker;