UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

372 lines (312 loc) 17.1 kB
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } import _times from "lodash/times"; import _keys from "lodash/keys"; import _get from "lodash/get"; import _noop from "lodash/noop"; import _clamp from "lodash/clamp"; 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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } 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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import PropTypes from 'react-peek/prop-types'; import React from 'react'; import { buildModernHybridComponent } from '../../util/state-management'; import { lucidClassNames } from '../../util/style-helpers'; import { getFirst, omitProps } from '../../util/component-types'; import * as reducers from './DateSelect.reducers'; import InfiniteSlidePanel from '../InfiniteSlidePanel/InfiniteSlidePanel'; import CalendarMonth from '../CalendarMonth/CalendarMonth'; import ChevronIcon from '../Icon/ChevronIcon/ChevronIcon'; import DayPicker from 'react-day-picker'; var cx = lucidClassNames.bind('&-DateSelect'); var DateUtils = DayPicker.DateUtils; var NAV_BUTTON_SIZE = 32; var clampMonthsShown = function clampMonthsShown(monthsShown) { return _clamp(monthsShown, 1, 6); }; var any = PropTypes.any, bool = PropTypes.bool, node = PropTypes.node, func = PropTypes.func, instanceOf = PropTypes.instanceOf, number = PropTypes.number, oneOf = PropTypes.oneOf, string = PropTypes.string; var DateSelectCalendarMonth = function DateSelectCalendarMonth(_props) { return null; }; DateSelectCalendarMonth.displayName = 'DateSelect.CalendarMonth'; DateSelectCalendarMonth.peek = { description: "\n\t\tChild component to pass thru props to underlying CalendarMonth.\n\t" }; DateSelectCalendarMonth.propName = 'CalendarMonth'; var DateSelect = /*#__PURE__*/function (_React$Component) { _inherits(DateSelect, _React$Component); var _super = _createSuper(DateSelect); function DateSelect(props) { var _this; _classCallCheck(this, DateSelect); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "initialMonth", void 0); _defineProperty(_assertThisInitialized(_this), "rootRef", null); _defineProperty(_assertThisInitialized(_this), "handleDayClick", function (day, _ref, event) { var disabled = _ref.disabled; var onSelectDate = _this.props.onSelectDate; if (!disabled) { onSelectDate(day, { event: event, props: _this.props }); } }); _defineProperty(_assertThisInitialized(_this), "handleDayMouseEnter", function (day, _ref2) { var disabled = _ref2.disabled; if (disabled) { _this.setState({ cursor: null }); } else { _this.setState({ cursor: day }); } }); _defineProperty(_assertThisInitialized(_this), "handleDayMouseLeave", function () { _this.setState({ cursor: null }); }); _defineProperty(_assertThisInitialized(_this), "handlePrev", function (_ref3) { var event = _ref3.event; _this.props.onPrev({ event: event, props: _this.props }); }); _defineProperty(_assertThisInitialized(_this), "handleNext", function (_ref4) { var event = _ref4.event; _this.props.onNext({ event: event, props: _this.props }); }); _defineProperty(_assertThisInitialized(_this), "componentDidMount", function () { var _this$props = _this.props, isFontSizeRelative = _this$props.isFontSizeRelative, monthsShownRaw = _this$props.monthsShown; var monthsShown = clampMonthsShown(monthsShownRaw); if (isFontSizeRelative && _this.rootRef) { var rootElement = _this.rootRef; var _rootElement$getBound = rootElement.getBoundingClientRect(), width = _rootElement$getBound.width, height = _rootElement$getBound.height; var navButtonsWidth = NAV_BUTTON_SIZE * 2; var oneMonthShownWidth = (width - navButtonsWidth) / monthsShown + navButtonsWidth; var size = Math.sqrt(oneMonthShownWidth * height); var relativeFontSize = Math.round(size / 24); var relativeMinWidth = (width - navButtonsWidth) / monthsShown * 10.1075 / relativeFontSize * monthsShown + navButtonsWidth; rootElement.style.fontSize = "".concat(relativeFontSize, "px"); rootElement.style.minWidth = "".concat(relativeMinWidth, "px"); } }); _defineProperty(_assertThisInitialized(_this), "renderCalendarMonth", function (monthOffset, isRangeSameDay, selectedDays, _ref5) { var key = _ref5.key, initialMonth = _ref5.initialMonth, cursor = _ref5.cursor, from = _ref5.from, to = _ref5.to, disabledDays = _ref5.disabledDays, selectMode = _ref5.selectMode, onDayClick = _ref5.onDayClick, showCursorHighlight = _ref5.showCursorHighlight, onDayMouseEnter = _ref5.onDayMouseEnter, onDayMouseLeave = _ref5.onDayMouseLeave, rest = _objectWithoutProperties(_ref5, ["key", "initialMonth", "cursor", "from", "to", "disabledDays", "selectMode", "onDayClick", "showCursorHighlight", "onDayMouseEnter", "onDayMouseLeave"]); return /*#__PURE__*/React.createElement(CalendarMonth, _extends({ key: key, className: cx('&-CalendarMonth'), monthOffset: monthOffset, initialMonth: initialMonth, cursor: cursor, from: isRangeSameDay ? null : from, to: isRangeSameDay ? null : to, selectedDays: isRangeSameDay ? from : selectedDays, disabledDays: disabledDays, selectMode: selectMode, onDayClick: onDayClick, onDayMouseEnter: showCursorHighlight ? onDayMouseEnter : _noop, onDayMouseLeave: showCursorHighlight ? onDayMouseLeave : _noop }, rest)); }); _this.initialMonth = new Date(_this.props.initialMonth); _this.state = { offset: 0, cursor: null }; return _this; } _createClass(DateSelect, [{ key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, className = _this$props2.className, monthsShownRaw = _this$props2.monthsShown, calendarsRendered = _this$props2.calendarsRendered, offset = _this$props2.offset, from = _this$props2.from, to = _this$props2.to, selectMode = _this$props2.selectMode, selectedDays = _this$props2.selectedDays, disabledDays = _this$props2.disabledDays, showDivider = _this$props2.showDivider, onSwipe = _this$props2.onSwipe, showCursorHighlight = _this$props2.showCursorHighlight, useSlidePanel = _this$props2.useSlidePanel, passThroughs = _objectWithoutProperties(_this$props2, ["className", "monthsShown", "calendarsRendered", "offset", "from", "to", "selectMode", "selectedDays", "disabledDays", "showDivider", "onSwipe", "showCursorHighlight", "useSlidePanel"]); var cursor = this.state.cursor; //@ts-ignore // For some reason react-day-pickers type doesn't allow `null` values but // we seem to be passing them in and it seems to "work" at least in that // the code doesn't blow up. var isRangeSameDay = DateUtils.isSameDay(from, to); var calendarMonthProps = _get(getFirst(this.props, DateSelect.CalendarMonth), 'props'); var monthsShown = clampMonthsShown(monthsShownRaw); /* istanbul ignore next */ return /*#__PURE__*/React.createElement("section", _extends({ ref: function ref(_ref6) { return _this2.rootRef = _ref6; }, className: cx('&', className, { '&-show-divider': showDivider }), style: _objectSpread({ minWidth: NAV_BUTTON_SIZE * 2 + 185 * monthsShown }, passThroughs.style) }, omitProps(passThroughs, undefined, _keys(DateSelect.propTypes))), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(ChevronIcon, { className: cx('&-chevron'), size: NAV_BUTTON_SIZE, isClickable: true, direction: "left", onClick: this.handlePrev })), useSlidePanel ? /*#__PURE__*/React.createElement(InfiniteSlidePanel, { className: cx('&-InfiniteSlidePanel', '&-slidePanel'), totalSlides: calendarsRendered, slidesToShow: monthsShown, offset: offset, onSwipe: onSwipe }, /*#__PURE__*/React.createElement(InfiniteSlidePanel.Slide, { className: cx('&-slide') }, function (slideOffset) { return /*#__PURE__*/React.createElement("div", { className: cx('&-slide-content') }, _this2.renderCalendarMonth(offset + slideOffset - offset, isRangeSameDay, selectedDays, _objectSpread({ key: slideOffset, initialMonth: _this2.initialMonth, cursor: cursor, from: from, to: to, disabledDays: disabledDays, selectMode: selectMode, onDayClick: _this2.handleDayClick, showCursorHighlight: showCursorHighlight, onDayMouseEnter: _this2.handleDayMouseEnter, onDayMouseLeave: _this2.handleDayMouseLeave }, calendarMonthProps))); })) : /*#__PURE__*/React.createElement("div", { className: cx('&-slidePanel', '&-slidePanel-simple') }, _times(monthsShown, function (calendarIndex) { return /*#__PURE__*/React.createElement("div", { className: cx('&-slide', '&-slide-simple'), key: calendarIndex }, /*#__PURE__*/React.createElement("div", { className: cx('&-slide-content') }, _this2.renderCalendarMonth(offset + calendarIndex, isRangeSameDay, selectedDays, _objectSpread({ initialMonth: _this2.initialMonth, cursor: cursor, from: from, to: to, disabledDays: disabledDays, selectMode: selectMode, onDayClick: _this2.handleDayClick, showCursorHighlight: showCursorHighlight, onDayMouseEnter: _this2.handleDayMouseEnter, onDayMouseLeave: _this2.handleDayMouseLeave }, calendarMonthProps)))); })), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(ChevronIcon, { className: cx('&-chevron'), size: NAV_BUTTON_SIZE, isClickable: true, direction: "right", onClick: this.handleNext }))); } }]); return DateSelect; }(React.Component); _defineProperty(DateSelect, "displayName", 'DateSelect'); _defineProperty(DateSelect, "CalendarMonth", DateSelectCalendarMonth); _defineProperty(DateSelect, "peek", { description: "\n\t\t\t\tDate selection component capabaple of supporting single date and date\n\t\t\t\trange selections.\n\t\t\t", categories: ['controls', 'selectors'], madeFrom: ['InfiniteSlidePanel', 'CalendarMonth'] }); _defineProperty(DateSelect, "propTypes", { className: string, monthsShown: number, calendarsRendered: number, offset: number, from: instanceOf(Date), to: instanceOf(Date), selectMode: oneOf(['day', 'from', 'to']), initialMonth: instanceOf(Date), selectedDays: any, disabledDays: any, showDivider: bool, onSwipe: func, onPrev: func, onNext: func, onSelectDate: func, isFontSizeRelative: bool, showCursorHighlight: bool, useSlidePanel: bool, CalendarMonth: node }); _defineProperty(DateSelect, "defaultProps", { monthsShown: 1, calendarsRendered: 6, offset: 0, from: null, to: null, initialMonth: new Date(), selectedDays: function selectedDays() { return false; }, disabledDays: function disabledDays() { return false; }, showDivider: false, onSwipe: _noop, onPrev: _noop, onNext: _noop, onSelectDate: _noop, isFontSizeRelative: false, showCursorHighlight: true, useSlidePanel: true }); _defineProperty(DateSelect, "reducers", reducers); export default buildModernHybridComponent(DateSelect, { reducers: reducers }); export { DateSelect as DateSelectDumb };