UNPKG

@enact/sandstone

Version:

Large-screen/TV support library for Enact, containing a variety of UI components.

416 lines (413 loc) 19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.TimePickerBase = void 0; var _kind = _interopRequireDefault(require("@enact/core/kind")); var _react = require("react"); var _propTypes = _interopRequireDefault(require("prop-types")); var _$L = _interopRequireDefault(require("../internal/$L")); var _DateComponentPicker = require("../internal/DateComponentPicker"); var _DateTime = _interopRequireDefault(require("../internal/DateTime")); var _TimePickerModule = _interopRequireDefault(require("./TimePicker.module.css")); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["hasMeridiem"], _excluded2 = ["data-webos-voice-disabled", "disabled", "hasMeridiem", "hour", "hourAriaLabel", "meridiem", "meridiemAriaLabel", "meridiemLabel", "meridiemPickerWidth", "meridiems", "minute", "minuteAriaLabel", "noLabel", "onChangeHour", "onChangeMeridiem", "onChangeMinute", "onSpotlightDisappear", "onSpotlightLeft", "onSpotlightRight", "order", "rtl", "spotlightDisabled"]; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } 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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : 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; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } // values to use in hour picker for 24 and 12 hour locales var hours24 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']; var hours12 = ['12', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11']; /** * {@link sandstone/TimePicker/TimePickerBase.HourPicker} is a utility component to prevent the * animation of the picker when the display text doesn't change for 12-hour locales. * * @class HourPicker * @memberof sandstone/TimePicker/TimePickerBase * @ui * @private */ var HourPicker = /*#__PURE__*/function (_Component) { _inherits(HourPicker, _Component); var _super = _createSuper(HourPicker); function HourPicker(props) { var _this; _classCallCheck(this, HourPicker); _this = _super.call(this, props); _this.state = { noAnimation: false, prevValue: props.value }; return _this; } _createClass(HourPicker, [{ key: "render", value: function render() { var _this$props = this.props, hasMeridiem = _this$props.hasMeridiem, rest = _objectWithoutProperties(_this$props, _excluded); var hours = hasMeridiem ? hours12 : hours24; return /*#__PURE__*/(0, _jsxRuntime.jsx)(_DateComponentPicker.DateComponentPicker, _objectSpread(_objectSpread({}, rest), {}, { noAnimation: this.state.noAnimation, children: hours })); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { if (state.prevValue !== props.value) { var hours = props.hasMeridiem ? hours12 : hours24; return { noAnimation: hours[state.prevValue] === hours[props.value], prevValue: props.value }; } return null; } }]); return HourPicker; }(_react.Component); /** * {@link sandstone/TimePicker.TimePickerBase} is the stateless functional time picker * component. Should not be used directly but may be composed within another component as it is * within {@link sandstone/TimePicker.TimePicker}. * * @class TimePickerBase * @memberof sandstone/TimePicker * @ui * @public */ HourPicker.propTypes = { hasMeridiem: _propTypes["default"].bool, value: _propTypes["default"].number }; var TimePickerBase = exports.TimePickerBase = (0, _kind["default"])({ name: 'TimePickerBase', propTypes: /** @lends sandstone/TimePicker.TimePickerBase.prototype */{ /** * The `hour` component of the time. * * @type {Number} * @required * @public */ hour: _propTypes["default"].number.isRequired, /** * The `meridiem` component of the time. * * @type {Number} * @required * @public */ meridiem: _propTypes["default"].number.isRequired, /** * The `minute` component of the time. * * @type {Number} * @required * @public */ minute: _propTypes["default"].number.isRequired, /** * The order in which the component pickers are displayed. * * Should be an array of 2 or 3 strings containing one of `'h'`, `'k'`, `'m'`, and `'a'`. * * @type {String[]} * @required * @public */ order: _propTypes["default"].arrayOf(_propTypes["default"].oneOf(['h', 'k', 'm', 'a'])).isRequired, /** * Disables voice control. * * @type {Boolean} * @memberof sandstone/TimePicker.TimePickerBase.prototype * @public */ 'data-webos-voice-disabled': _propTypes["default"].bool, /** * Disables the `TimePicker`. * * @type {Boolean} * @public */ disabled: _propTypes["default"].bool, /** * The "aria-label" for the hour picker * * If not specified, the "aria-label" for the hour picker will be * a combination of the current value and 'hour change a value with up down button'. * * @type {String} * @public */ hourAriaLabel: _propTypes["default"].string, /** * The primary text of `TimePicker`. * * @type {String} * @public */ label: _propTypes["default"].string, /** * The "aria-label" for the meridiem picker. * * If not specified, the "aria-label" for the meridiem picker will be * a combination of the current value and 'change a value with up down button'. * * @type {String} * @public */ meridiemAriaLabel: _propTypes["default"].string, /** * The hint string read when focusing the meridiem picker. * * @type {String} * @public */ meridiemLabel: _propTypes["default"].string, /** * Array of meridiem labels to display. * * @type {String[]} * @required * @public */ meridiems: _propTypes["default"].arrayOf(_propTypes["default"].string), /** * The "aria-label" for the minute picker. * * If not specified, the "aria-label" for the minute picker will be * a combination of the current value and 'minute change a value with up down button'. * * @type {String} * @public */ minuteAriaLabel: _propTypes["default"].string, /** * Hides the label that displays the time. * * @type {Boolean} * @public */ noLabel: _propTypes["default"].bool, /** * Called on changes in the `hour` component of the time. * * @type {Function} * @public */ onChangeHour: _propTypes["default"].func, /** * Called on changes in the `meridiem` component of the time. * * @type {Function} * @public */ onChangeMeridiem: _propTypes["default"].func, /** * Called on changes in the `minute` component of the time. * * @type {Function} * @public */ onChangeMinute: _propTypes["default"].func, /** * Called when the component is removed while retaining focus. * * @type {Function} * @param {Object} event * @public */ onSpotlightDisappear: _propTypes["default"].func, /** * Called when the focus leaves the picker when the 5-way left key is pressed. * * @type {Function} * @param {Object} event * @public */ onSpotlightLeft: _propTypes["default"].func, /** * Called when the focus leaves the picker when the 5-way right key is pressed. * * @type {Function} * @param {Object} event * @public */ onSpotlightRight: _propTypes["default"].func, /** * Set content to RTL. * * @type {Boolean} * @private */ rtl: _propTypes["default"].bool, /** * Disables spotlight navigation into the component. * * @type {Boolean} * @default false * @public */ spotlightDisabled: _propTypes["default"].bool }, defaultProps: { disabled: false, spotlightDisabled: false }, styles: { css: _TimePickerModule["default"], className: 'timePicker' }, computed: { hasMeridiem: function hasMeridiem(_ref) { var order = _ref.order; return order.indexOf('a') >= 0; }, meridiemPickerWidth: function meridiemPickerWidth(_ref2) { var meridiem = _ref2.meridiem, meridiems = _ref2.meridiems; return (meridiems === null || meridiems === void 0 ? void 0 : meridiems[meridiem].length) * 2; } }, render: function render(_ref3) { var voiceDisabled = _ref3['data-webos-voice-disabled'], disabled = _ref3.disabled, hasMeridiem = _ref3.hasMeridiem, hour = _ref3.hour, hourAriaLabel = _ref3.hourAriaLabel, meridiem = _ref3.meridiem, meridiemAriaLabel = _ref3.meridiemAriaLabel, meridiemLabel = _ref3.meridiemLabel, meridiemPickerWidth = _ref3.meridiemPickerWidth, meridiems = _ref3.meridiems, minute = _ref3.minute, minuteAriaLabel = _ref3.minuteAriaLabel, noLabel = _ref3.noLabel, onChangeHour = _ref3.onChangeHour, onChangeMeridiem = _ref3.onChangeMeridiem, onChangeMinute = _ref3.onChangeMinute, onSpotlightDisappear = _ref3.onSpotlightDisappear, onSpotlightLeft = _ref3.onSpotlightLeft, onSpotlightRight = _ref3.onSpotlightRight, order = _ref3.order, rtl = _ref3.rtl, spotlightDisabled = _ref3.spotlightDisabled, rest = _objectWithoutProperties(_ref3, _excluded2); var hourAccessibilityHint = (0, _$L["default"])('hour'), minuteAccessibilityHint = (0, _$L["default"])('minute'); if (noLabel) { delete rest.label; } return /*#__PURE__*/(0, _jsxRuntime.jsx)(_DateTime["default"], _objectSpread(_objectSpread({}, rest), {}, { css: _TimePickerModule["default"], children: order.map(function (picker, index) { // although we create a component array based on the provided // order, we ultimately force order in CSS for RTL var isFirst = index === 0; var isLast = index === order.length - 1; // meridiem will always be the left-most control in RTL, regardless of the provided order var isLeft = rtl && (hasMeridiem ? picker === 'a' : picker === 'h') || isFirst && !rtl; // minute will always be the right-most control in RTL, regardless of the provided order var isRight = rtl && picker === 'm' || isLast && !rtl; var isLastElement = rtl ? isLeft : isLast; switch (picker) { case 'h': case 'k': return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_react.Fragment, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(HourPicker, { accessibilityHint: hourAccessibilityHint, "aria-label": hourAriaLabel, className: _TimePickerModule["default"].hourPicker, "data-last-element": isLastElement, "data-webos-voice-disabled": voiceDisabled, "data-webos-voice-group-label": hourAccessibilityHint, disabled: disabled, hasMeridiem: hasMeridiem, onChange: onChangeHour, onSpotlightDisappear: onSpotlightDisappear, onSpotlightLeft: isLeft ? onSpotlightLeft : null, onSpotlightRight: isRight ? onSpotlightRight : null, spotlightDisabled: spotlightDisabled, value: hour, width: 4, wrap: true }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", { className: _TimePickerModule["default"].timeSeparator, disabled: disabled, children: ":" })] }, "hour-picker"); case 'm': return /*#__PURE__*/(0, _jsxRuntime.jsx)(_DateComponentPicker.DateComponentRangePicker, { accessibilityHint: minuteAccessibilityHint, "aria-label": minuteAriaLabel, className: _TimePickerModule["default"].minutePicker, "data-last-element": isLastElement, "data-webos-voice-disabled": voiceDisabled, "data-webos-voice-group-label": minuteAccessibilityHint, disabled: disabled, max: 59, min: 0, onChange: onChangeMinute, onSpotlightDisappear: onSpotlightDisappear, onSpotlightLeft: isLeft ? onSpotlightLeft : null, onSpotlightRight: isRight ? onSpotlightRight : null, padded: true, spotlightDisabled: spotlightDisabled, value: minute, width: 4, wrap: true }, "minute-picker"); case 'a': return /*#__PURE__*/(0, _jsxRuntime.jsx)(_DateComponentPicker.DateComponentPicker, { "aria-label": meridiemAriaLabel, "aria-valuetext": meridiems ? meridiems[meridiem] : null, className: _TimePickerModule["default"].meridiemPicker, "data-last-element": isLastElement, "data-webos-voice-disabled": voiceDisabled, "data-webos-voice-group-label": meridiemLabel, disabled: disabled, onChange: onChangeMeridiem, onSpotlightDisappear: onSpotlightDisappear, onSpotlightLeft: isLeft ? onSpotlightLeft : null, onSpotlightRight: isRight ? onSpotlightRight : null, reverse: true, spotlightDisabled: spotlightDisabled, value: meridiem, width: meridiemPickerWidth, wrap: true, children: meridiems }, "meridiem-picker"); } return null; }) })); } }); /** * Called when `Enter` key down on the last picker * * @name onComplete * @memberof sandstone/TimePicker.TimePickerBase.prototype * @type {Function} * @public */ var _default = exports["default"] = TimePickerBase;