@enact/sandstone
Version:
Large-screen/TV support library for Enact, containing a variety of UI components.
368 lines (364 loc) • 14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.RangePickerBase = exports.RangePicker = void 0;
var _classnames2 = _interopRequireDefault(require("classnames"));
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _util = require("@enact/core/util");
var _Changeable = _interopRequireDefault(require("@enact/ui/Changeable"));
var _Pure = _interopRequireDefault(require("@enact/ui/internal/Pure"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _Heading = _interopRequireDefault(require("../Heading"));
var _Picker = require("../internal/Picker");
var _validators = require("../internal/validators");
var _RangePickerModule = _interopRequireDefault(require("./RangePicker.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["css", "label", "inlineTitle", "title", "value", "voiceLabel"];
/**
* A component for selecting a number from a range of numbers.
*
* @example
* <RangePicker defaultValue={70} min={0} max={100}></RangePicker>
*
* @module sandstone/RangePicker
* @exports RangePicker
* @exports RangePickerBase
*/
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 _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 _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; }
var digits = function digits(num) {
// minor optimization
return num > -10 && num < 10 && 1 || num > -100 && num < 100 && 2 || num > -1000 && num < 1000 && 3 || Math.floor(Math.log(Math.abs(num)) * Math.LOG10E) + 1;
};
/**
* RangePicker base component.
*
* @class RangePickerBase
* @memberof sandstone/RangePicker
* @ui
* @public
*/
var RangePickerBase = exports.RangePickerBase = (0, _kind["default"])({
name: 'RangePicker',
propTypes: /** @lends sandstone/RangePicker.RangePickerBase.prototype */{
/**
* The maximum value selectable by the picker (inclusive).
*
* The range between `min` and `max` should be evenly divisible by
* {@link sandstone/RangePicker.RangePickerBase.step|step}.
*
* @type {Number}
* @required
* @public
*/
max: _propTypes["default"].number.isRequired,
/**
* The minimum value selectable by the picker (inclusive).
*
* The range between `min` and `max` should be evenly divisible by
* {@link sandstone/RangePicker.RangePickerBase.step|step}.
*
* @type {Number}
* @required
* @public
*/
min: _propTypes["default"].number.isRequired,
/**
* Current value.
*
* @type {Number}
* @required
* @public
*/
value: _propTypes["default"].number.isRequired,
/**
* The `aria-valuetext` for the picker.
*
* By default, `aria-valuetext` is set to the current selected child value.
*
* @type {String}
* @memberof sandstone/RangePicker.RangePickerBase.prototype
* @public
*/
'aria-valuetext': _propTypes["default"].string,
/**
* Determines which key to adjust the picker's value for the joined horizontal one.
*
* * `'enter'` allows the user to use the enter key to adjust the picker's value
* * `'arrow'` allows the user to use the left or right keys to adjust the picker's value.
*
* The default value for joined horizontal picker is `'enter'`.
* If {@link sandstone/RangePicker.RangePickerBase.orientation|orientation} is `'vertical'` or
* {@link sandstone/RangePicker.RangePickerBase.joined|joined} is undefined or is `false`, this prop is ignored.
*
* @type {('enter'|'arrow')}
* @public
*/
changedBy: _propTypes["default"].oneOf(['enter', 'arrow']),
/**
* Children from which to pick.
*
* @type {Node}
* @public
*/
children: _propTypes["default"].node,
/**
* Class name for component.
*
* @type {String}
* @public
*/
className: _propTypes["default"].string,
/**
* Customizes the component by mapping the supplied collection of CSS class names to the
* corresponding internal elements and states of this component.
*
* The following classes are supported:
*
* * `title` - The title component class
* * `inlineTitle` - The title component class when `inlineTitle` is true
*
* @type {Object}
* @public
*/
css: _propTypes["default"].object,
/**
* A custom icon for the decrementer.
*
* All strings supported by {@link sandstone/Icon.Icon|Icon} are supported. Without a
* custom icon, the default is used, and is automatically changed when the
* {@link sandstone/RangePicker.RangePickerBase.orientation|orientation} is changed.
*
* @type {string}
* @public
*/
decrementIcon: _propTypes["default"].string,
/**
* Disables the picker.
*
* @type {Boolean}
* @public
*/
disabled: _propTypes["default"].bool,
/**
* A custom icon for the incrementer.
*
* All strings supported by {@link sandstone/Icon.Icon|Icon} are supported. Without a
* custom icon, the default is used, and is automatically changed when the
* {@link sandstone/RangePicker.RangePickerBase.orientation|orientation} is changed.
*
* @type {String}
* @public
*/
incrementIcon: _propTypes["default"].string,
/**
* Applies inline styling to the title.
*
* @type {Boolean}
* @public
*/
inlineTitle: _propTypes["default"].bool,
/**
* Allows the user can use the arrow keys or enter key to adjust the picker's value.
*
* The user may no longer use those arrow keys to navigate while this control is focused.
* A default control allows full navigation, but requires individual ENTER presses on the
* incrementer and decrementer buttons. Pointer interaction is the same for both formats.
*
* @type {Boolean}
* @public
*/
joined: _propTypes["default"].bool,
/**
* Disables animation.
*
* By default, the picker will animate transitions between items, provided a `width` is
* defined.
*
* @type {Boolean}
* @public
*/
noAnimation: _propTypes["default"].bool,
/**
* Called when `value` changes.
*
* @type {Function}
* @public
*/
onChange: _propTypes["default"].func,
/**
* Orientation of the picker.
*
* Controls whether the buttons are arranged horizontally or vertically around the value.
*
* * Values: `'horizontal'`, `'vertical'`
*
* @type {'horizontal'|'vertical'}
* @default 'horizontal'
* @public
*/
orientation: _propTypes["default"].oneOf(['horizontal', 'vertical']),
/**
* Pads the display value with zeros up to the number of digits of `min` or max`, whichever
* is greater.
*
* @type {Boolean}
* @public
*/
padded: _propTypes["default"].bool,
/**
* The smallest value change allowed for the picker.
*
* For example, a step of `2` would cause the picker to increment from 0 to 2 to 4, etc.
* It must evenly divide into the range designated by `min` and `max`.
*
* @type {Number}
* @default 1
* @public
*/
step: _propTypes["default"].number,
/**
* The primary text of the `Picker`.
*
* @type {String}
* @public
*/
title: _propTypes["default"].string,
/**
* The width of the picker.
*
* A number can be used to set the minimum number of characters to be shown. Setting a
* number to less than the number of characters in the longest value will cause the width to
* grow for the longer values.
*
* A string can be used to select from pre-defined widths:
*
* * `'small'` - numeric values
* * `'medium'` - single or short words
* * `'large'` - maximum-sized pickers taking full width of its parent
*
* By default, the picker will size according to the longest valid value.
*
* @type {('small'|'medium'|'large'|Number)}
* @public
*/
width: _propTypes["default"].oneOfType([_propTypes["default"].oneOf([null, 'small', 'medium', 'large']), _propTypes["default"].number]),
/**
* Allows picker to continue from the start of the list after it reaches the end and
* vice-versa.
*
* @type {Boolean}
* @public
*/
wrap: _propTypes["default"].bool
},
styles: {
css: _RangePickerModule["default"],
className: 'rangePicker',
publicClassNames: ['inlineTitle', 'title']
},
computed: {
disabled: function disabled(_ref) {
var _disabled = _ref.disabled,
max = _ref.max,
min = _ref.min;
return min >= max ? true : _disabled;
},
label: function label(_ref2) {
var max = _ref2.max,
min = _ref2.min,
padded = _ref2.padded,
value = _ref2.value;
value = (0, _util.clamp)(min, max, value);
if (padded) {
var maxDigits = digits(Math.max(Math.abs(min), Math.abs(max)));
var valueDigits = digits(value);
var start = value < 0 ? 0 : 1;
var padding = '-00000000000000000000';
return padding.slice(start, maxDigits - valueDigits + start) + Math.abs(value);
}
return value;
},
width: function width(_ref3) {
var max = _ref3.max,
min = _ref3.min,
_width = _ref3.width;
return _width || Math.max(max.toString().length, min.toString().length);
},
value: function value(_ref4) {
var min = _ref4.min,
max = _ref4.max,
_value = _ref4.value;
if (process.env.NODE_ENV !== "production") {
(0, _validators.validateRange)(_value, min, max, 'RangePicker');
}
return (0, _util.clamp)(min, max, _value);
},
voiceLabel: function voiceLabel(_ref5) {
var min = _ref5.min,
max = _ref5.max;
return JSON.stringify([min, max]);
}
},
render: function render(_ref6) {
var css = _ref6.css,
label = _ref6.label,
inlineTitle = _ref6.inlineTitle,
title = _ref6.title,
value = _ref6.value,
voiceLabel = _ref6.voiceLabel,
rest = _objectWithoutProperties(_ref6, _excluded);
delete rest.padded;
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [title ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Heading["default"], {
className: (0, _classnames2["default"])(css.title, _defineProperty({}, css.inlineTitle, inlineTitle)),
size: "tiny",
children: title
}) : null, /*#__PURE__*/(0, _jsxRuntime.jsx)(_Picker.Picker, _objectSpread(_objectSpread({}, rest), {}, {
css: css,
"data-webos-voice-labels-ext": voiceLabel,
index: 0,
reverse: false,
type: "number",
value: value,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Picker.PickerItem, {
marqueeDisabled: true,
style: {
direction: 'ltr'
},
children: label
}, value)
}))]
});
}
});
/**
* A component that lets the user select a number from a range of numbers.
*
* By default, `RangePicker` maintains the state of its `value` property. Supply the `defaultValue`
* property to control its initial value. If you wish to directly control updates to the component,
* supply a value to `value` at creation time and update it in response to `onChange` events.
*
* @class RangePicker
* @memberof sandstone/RangePicker
* @mixes ui/Changeable.Changeable
* @ui
* @public
*/
var RangePicker = exports.RangePicker = (0, _Pure["default"])((0, _Changeable["default"])(RangePickerBase));
/**
* Default value
*
* @name defaultValue
* @memberof sandstone/RangePicker.RangePicker.prototype
* @type {Number}
* @public
*/
var _default = exports["default"] = RangePicker;