UNPKG

@enact/sandstone

Version:

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

128 lines (123 loc) 4.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.limitNumberLength = exports.extractInputProps = exports.extractInputFieldProps = exports.convertToPasswordFormat = exports.calcAriaLabel = exports.SEPARATE_DIGITS_LIMIT = exports.OVERLAY_JOINED_DIGITS_LIMIT = exports.FULLSCREEN_JOINED_DIGITS_LIMIT = exports.DEFAULT_LENGTH = void 0; var _$L = _interopRequireDefault(require("../internal/$L")); var _warning = _interopRequireDefault(require("warning")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } // A default value for the numeric field length. Used by maxLength and minLength. var DEFAULT_LENGTH = exports.DEFAULT_LENGTH = 4; // The cutoff length, at which point the numeric field switches from separated boxes to one box var SEPARATE_DIGITS_LIMIT = exports.SEPARATE_DIGITS_LIMIT = 6; var OVERLAY_JOINED_DIGITS_LIMIT = exports.OVERLAY_JOINED_DIGITS_LIMIT = 10; var FULLSCREEN_JOINED_DIGITS_LIMIT = exports.FULLSCREEN_JOINED_DIGITS_LIMIT = 25; /** * Determines the `aria-label` for an Input * * @method * @memberof sandstone/Input * @param {String} prefix Text to precede the value in the aria-label * @param {String} type `type` of the Input * @param {String} [value] Current value of the input * @returns {String} `aria-label` value * @private */ var calcAriaLabel = exports.calcAriaLabel = function calcAriaLabel(prefix, type) { var value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; var hint = (0, _$L["default"])('Input field'); if ((type === 'password' || type === 'passwordtel') && value) { var character = value.length > 1 ? (0, _$L["default"])('characters') : (0, _$L["default"])('character'); value = "".concat(value.length, " ").concat(character); } return "".concat(prefix, " ").concat(value, " ").concat(hint); }; var convertToPasswordFormat = exports.convertToPasswordFormat = function convertToPasswordFormat(value) { return '*'.repeat(value && value.length); }; /** * Removes `<InputField>` related props from `props` and returns them in a new object. * * Useful when redirecting `<InputField>` related props from a non-input root element to the * `<InputField>` component. * * @method * @memberof sandstone/Input * @param {Object} props Props object * @returns {Object} input related props * @private */ var extractInputFieldProps = exports.extractInputFieldProps = function extractInputFieldProps(props) { var inputProps = {}; Object.keys(props).forEach(function (key) { switch (key) { case 'autoComplete': case 'data-webos-voice-group-label': case 'data-webos-voice-intent': case 'data-webos-voice-label': case 'disabled': case 'dismissOnEnter': case 'iconAfter': case 'iconBefore': case 'invalid': case 'invalidMessage': case 'list': case 'maxLength': case 'minLength': case 'onActivate': case 'onChange': case 'onBlur': case 'onDeactivate': case 'pattern': case 'required': case 'size': inputProps[key] = props[key]; delete props[key]; } }); return inputProps; }; /** * Removes `<input>` related props from `props` and returns them in a new object. * * Useful when redirecting `<input>` related props from a non-input root element to the `<input>` * element. * * @method * @memberof sandstone/Input * @param {Object} props Props object * @returns {Object} input related props * @private */ var extractInputProps = exports.extractInputProps = function extractInputProps(props) { var inputProps = {}; Object.keys(props).forEach(function (key) { switch (key) { case 'autoComplete': case 'list': case 'maxLength': case 'minLength': case 'pattern': case 'required': case 'size': inputProps[key] = props[key]; delete props[key]; } }); return inputProps; }; var limitNumberLength = exports.limitNumberLength = function limitNumberLength(popupType, length) { var limitedLength = length; if (popupType === 'fullscreen') { if (length > FULLSCREEN_JOINED_DIGITS_LIMIT) { limitedLength = FULLSCREEN_JOINED_DIGITS_LIMIT; process.env.NODE_ENV !== "production" ? (0, _warning["default"])(false, "Max length of fullscreen type input must not exceed ".concat(FULLSCREEN_JOINED_DIGITS_LIMIT, " digits.")) : void 0; } } else if (popupType === 'overlay') { if (length > OVERLAY_JOINED_DIGITS_LIMIT) { limitedLength = OVERLAY_JOINED_DIGITS_LIMIT; process.env.NODE_ENV !== "production" ? (0, _warning["default"])(false, "Max length of overlay type input must not exceed ".concat(OVERLAY_JOINED_DIGITS_LIMIT, " digits.")) : void 0; } } return limitedLength; };