@moda/om 21.8.0 → 21.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -12318,6 +12318,273 @@ var Paginator = function Paginator(_ref) {
12318
12318
  })));
12319
12319
  };
12320
12320
 
12321
+ var isStyleObject = function isStyleObject(obj) {
12322
+ return _typeof$1(obj) === 'object' && obj !== null;
12323
+ };
12324
+ var OTPInput = function OTPInput(_a) {
12325
+ var _b = _a.value,
12326
+ value = _b === void 0 ? '' : _b,
12327
+ _c = _a.numInputs,
12328
+ numInputs = _c === void 0 ? 4 : _c,
12329
+ onChange = _a.onChange,
12330
+ onPaste = _a.onPaste,
12331
+ renderInput = _a.renderInput,
12332
+ _d = _a.shouldAutoFocus,
12333
+ shouldAutoFocus = _d === void 0 ? false : _d,
12334
+ _e = _a.inputType,
12335
+ inputType = _e === void 0 ? 'text' : _e,
12336
+ renderSeparator = _a.renderSeparator,
12337
+ placeholder = _a.placeholder,
12338
+ containerStyle = _a.containerStyle,
12339
+ inputStyle = _a.inputStyle,
12340
+ _f = _a.skipDefaultStyles,
12341
+ skipDefaultStyles = _f === void 0 ? false : _f;
12342
+ var _g = React__default.useState(0),
12343
+ activeInput = _g[0],
12344
+ setActiveInput = _g[1];
12345
+ var inputRefs = React__default.useRef([]);
12346
+ var getOTPValue = function getOTPValue() {
12347
+ return value ? value.toString().split('') : [];
12348
+ };
12349
+ var isInputNum = inputType === 'number' || inputType === 'tel';
12350
+ React__default.useEffect(function () {
12351
+ inputRefs.current = inputRefs.current.slice(0, numInputs);
12352
+ }, [numInputs]);
12353
+ React__default.useEffect(function () {
12354
+ var _a;
12355
+ if (shouldAutoFocus) {
12356
+ (_a = inputRefs.current[0]) === null || _a === void 0 ? void 0 : _a.focus();
12357
+ }
12358
+ }, [shouldAutoFocus]);
12359
+ var getPlaceholderValue = function getPlaceholderValue() {
12360
+ if (typeof placeholder === 'string') {
12361
+ if (placeholder.length === numInputs) {
12362
+ return placeholder;
12363
+ }
12364
+ if (placeholder.length > 0) {
12365
+ console.error('Length of the placeholder should be equal to the number of inputs.');
12366
+ }
12367
+ }
12368
+ return undefined;
12369
+ };
12370
+ var isInputValueValid = function isInputValueValid(value) {
12371
+ var isTypeValid = isInputNum ? !isNaN(Number(value)) : typeof value === 'string';
12372
+ return isTypeValid && value.trim().length === 1;
12373
+ };
12374
+ var handleChange = function handleChange(event) {
12375
+ var value = event.target.value;
12376
+ if (isInputValueValid(value)) {
12377
+ changeCodeAtFocus(value);
12378
+ focusInput(activeInput + 1);
12379
+ }
12380
+ };
12381
+ var handleInputChange = function handleInputChange(event) {
12382
+ var nativeEvent = event.nativeEvent;
12383
+ var value = event.target.value;
12384
+ if (!isInputValueValid(value)) {
12385
+ // Pasting from the native autofill suggestion on a mobile device can pass
12386
+ // the pasted string as one long input to one of the cells. This ensures
12387
+ // that we handle the full input and not just the first character.
12388
+ if (value.length === numInputs) {
12389
+ var hasInvalidInput = value.split('').some(function (cellInput) {
12390
+ return !isInputValueValid(cellInput);
12391
+ });
12392
+ if (!hasInvalidInput) {
12393
+ handleOTPChange(value.split(''));
12394
+ focusInput(numInputs - 1);
12395
+ }
12396
+ }
12397
+ // @ts-expect-error - This was added previously to handle and edge case
12398
+ // for dealing with keyCode "229 Unidentified" on Android. Check if this is
12399
+ // still needed.
12400
+ if (nativeEvent.data === null && nativeEvent.inputType === 'deleteContentBackward') {
12401
+ event.preventDefault();
12402
+ changeCodeAtFocus('');
12403
+ focusInput(activeInput - 1);
12404
+ }
12405
+ // Clear the input if it's not valid value because firefox allows
12406
+ // pasting non-numeric characters in a number type input
12407
+ event.target.value = '';
12408
+ }
12409
+ };
12410
+ var handleFocus = function handleFocus(event) {
12411
+ return function (index) {
12412
+ setActiveInput(index);
12413
+ event.target.select();
12414
+ };
12415
+ };
12416
+ var handleBlur = function handleBlur() {
12417
+ setActiveInput(activeInput - 1);
12418
+ };
12419
+ var handleKeyDown = function handleKeyDown(event) {
12420
+ var otp = getOTPValue();
12421
+ if ([event.code, event.key].includes('Backspace')) {
12422
+ event.preventDefault();
12423
+ changeCodeAtFocus('');
12424
+ focusInput(activeInput - 1);
12425
+ } else if (event.code === 'Delete') {
12426
+ event.preventDefault();
12427
+ changeCodeAtFocus('');
12428
+ } else if (event.code === 'ArrowLeft') {
12429
+ event.preventDefault();
12430
+ focusInput(activeInput - 1);
12431
+ } else if (event.code === 'ArrowRight') {
12432
+ event.preventDefault();
12433
+ focusInput(activeInput + 1);
12434
+ }
12435
+ // React does not trigger onChange when the same value is entered
12436
+ // again. So we need to focus the next input manually in this case.
12437
+ else if (event.key === otp[activeInput]) {
12438
+ event.preventDefault();
12439
+ focusInput(activeInput + 1);
12440
+ } else if (event.code === 'Spacebar' || event.code === 'Space' || event.code === 'ArrowUp' || event.code === 'ArrowDown') {
12441
+ event.preventDefault();
12442
+ }
12443
+ };
12444
+ var focusInput = function focusInput(index) {
12445
+ var _a, _b;
12446
+ var activeInput = Math.max(Math.min(numInputs - 1, index), 0);
12447
+ if (inputRefs.current[activeInput]) {
12448
+ (_a = inputRefs.current[activeInput]) === null || _a === void 0 ? void 0 : _a.focus();
12449
+ (_b = inputRefs.current[activeInput]) === null || _b === void 0 ? void 0 : _b.select();
12450
+ setActiveInput(activeInput);
12451
+ }
12452
+ };
12453
+ var changeCodeAtFocus = function changeCodeAtFocus(value) {
12454
+ var otp = getOTPValue();
12455
+ otp[activeInput] = value[0];
12456
+ handleOTPChange(otp);
12457
+ };
12458
+ var handleOTPChange = function handleOTPChange(otp) {
12459
+ var otpValue = otp.join('');
12460
+ onChange(otpValue);
12461
+ };
12462
+ var handlePaste = function handlePaste(event) {
12463
+ var _a;
12464
+ event.preventDefault();
12465
+ var otp = getOTPValue();
12466
+ var nextActiveInput = activeInput;
12467
+ // Get pastedData in an array of max size (num of inputs - current position)
12468
+ var pastedData = event.clipboardData.getData('text/plain').slice(0, numInputs - activeInput).split('');
12469
+ // Prevent pasting if the clipboard data contains non-numeric values for number inputs
12470
+ if (isInputNum && pastedData.some(function (value) {
12471
+ return isNaN(Number(value));
12472
+ })) {
12473
+ return;
12474
+ }
12475
+ // Paste data from focused input onwards
12476
+ for (var pos = 0; pos < numInputs; ++pos) {
12477
+ if (pos >= activeInput && pastedData.length > 0) {
12478
+ otp[pos] = (_a = pastedData.shift()) !== null && _a !== void 0 ? _a : '';
12479
+ nextActiveInput++;
12480
+ }
12481
+ }
12482
+ focusInput(nextActiveInput);
12483
+ handleOTPChange(otp);
12484
+ };
12485
+ return /*#__PURE__*/React__default.createElement("div", {
12486
+ style: Object.assign({
12487
+ display: 'flex',
12488
+ alignItems: 'center'
12489
+ }, isStyleObject(containerStyle) && containerStyle),
12490
+ className: typeof containerStyle === 'string' ? containerStyle : undefined,
12491
+ onPaste: onPaste
12492
+ }, Array.from({
12493
+ length: numInputs
12494
+ }, function (_, index) {
12495
+ return index;
12496
+ }).map(function (index) {
12497
+ var _a, _b, _c;
12498
+ return /*#__PURE__*/React__default.createElement(React__default.Fragment, {
12499
+ key: index
12500
+ }, renderInput({
12501
+ value: (_a = getOTPValue()[index]) !== null && _a !== void 0 ? _a : '',
12502
+ placeholder: (_c = (_b = getPlaceholderValue()) === null || _b === void 0 ? void 0 : _b[index]) !== null && _c !== void 0 ? _c : undefined,
12503
+ ref: function ref(element) {
12504
+ return inputRefs.current[index] = element;
12505
+ },
12506
+ onChange: handleChange,
12507
+ onFocus: function onFocus(event) {
12508
+ return handleFocus(event)(index);
12509
+ },
12510
+ onBlur: handleBlur,
12511
+ onKeyDown: handleKeyDown,
12512
+ onPaste: handlePaste,
12513
+ autoComplete: 'off',
12514
+ 'aria-label': "Please enter OTP character ".concat(index + 1),
12515
+ style: Object.assign(!skipDefaultStyles ? {
12516
+ width: '1em',
12517
+ textAlign: 'center'
12518
+ } : {}, isStyleObject(inputStyle) ? inputStyle : {}),
12519
+ className: typeof inputStyle === 'string' ? inputStyle : undefined,
12520
+ type: inputType,
12521
+ inputMode: isInputNum ? 'numeric' : 'text',
12522
+ onInput: handleInputChange
12523
+ }, index), index < numInputs - 1 && (typeof renderSeparator === 'function' ? renderSeparator(index) : renderSeparator));
12524
+ }));
12525
+ };
12526
+
12527
+ var getInputAriaLabel = function getInputAriaLabel(index, inputCount, ariaLabel, label) {
12528
+ var baseLabel = ariaLabel || label || 'One-time password digit';
12529
+ return "".concat(baseLabel, " ").concat(index + 1, " of ").concat(inputCount);
12530
+ };
12531
+ var OtpInput = function OtpInput(_ref) {
12532
+ var ariaDescribedBy = _ref['aria-describedby'],
12533
+ ariaLabel = _ref['aria-label'],
12534
+ _ref$autoComplete = _ref.autoComplete,
12535
+ autoComplete = _ref$autoComplete === void 0 ? 'one-time-code' : _ref$autoComplete,
12536
+ className = _ref.className,
12537
+ disabled = _ref.disabled,
12538
+ error = _ref.error,
12539
+ focus = _ref.focus,
12540
+ id = _ref.id,
12541
+ inputClassName = _ref.inputClassName,
12542
+ _ref$inputType = _ref.inputType,
12543
+ inputType = _ref$inputType === void 0 ? 'tel' : _ref$inputType,
12544
+ label = _ref.label,
12545
+ name = _ref.name,
12546
+ _ref$numInputs = _ref.numInputs,
12547
+ numInputs = _ref$numInputs === void 0 ? 4 : _ref$numInputs,
12548
+ onChange = _ref.onChange,
12549
+ onPaste = _ref.onPaste,
12550
+ placeholder = _ref.placeholder,
12551
+ renderSeparator = _ref.renderSeparator,
12552
+ required = _ref.required,
12553
+ shouldAutoFocus = _ref.shouldAutoFocus,
12554
+ _ref$value = _ref.value,
12555
+ value = _ref$value === void 0 ? '' : _ref$value;
12556
+ return /*#__PURE__*/React__default.createElement(OTPInput, {
12557
+ containerStyle: classNames('OtpInput', className),
12558
+ inputType: inputType,
12559
+ numInputs: numInputs,
12560
+ onChange: onChange,
12561
+ onPaste: onPaste,
12562
+ placeholder: placeholder,
12563
+ renderSeparator: renderSeparator,
12564
+ shouldAutoFocus: shouldAutoFocus,
12565
+ skipDefaultStyles: true,
12566
+ value: value,
12567
+ renderInput: function renderInput(inputProps, index) {
12568
+ return /*#__PURE__*/React__default.createElement("input", _extends$1({}, inputProps, {
12569
+ id: id ? "".concat(id, "-").concat(index + 1) : undefined,
12570
+ autoComplete: autoComplete,
12571
+ "aria-describedby": ariaDescribedBy,
12572
+ "aria-invalid": error ? true : undefined,
12573
+ "aria-label": getInputAriaLabel(index, numInputs, ariaLabel, label),
12574
+ className: classNames(inputProps.className, 'OtpInput__input', inputClassName, {
12575
+ 'OtpInput__input--disabled': disabled,
12576
+ 'OtpInput__input--error': error,
12577
+ 'OtpInput__input--focus': focus
12578
+ }),
12579
+ disabled: disabled,
12580
+ name: name,
12581
+ required: required
12582
+ }));
12583
+ }
12584
+ });
12585
+ };
12586
+ OtpInput.displayName = 'OtpInput';
12587
+
12321
12588
  var PasswordInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
12322
12589
  var rest = _extends$1({}, (_objectDestructuringEmpty(_ref), _ref));
12323
12590
  var _useState = useState(false),
@@ -12816,4 +13083,4 @@ var Utilities = {
12816
13083
  States: States
12817
13084
  };
12818
13085
 
12819
- export { ArcWindow, AspectRatioBox, Badge, Bradley, Brancusi, Breadcrumb, Breadcrumbs, Breakpoint, BreakpointContext, BreakpointProvider, Button, Checkbox, Circle, Clickable, ColorSwatch, ConfirmProvider, Constrain, ControlLink, CreditCardNumberInput, DEFAULT_PRODUCT_ASPECT_HEIGHT, DEFAULT_PRODUCT_ASPECT_RATIO, DEFAULT_PRODUCT_ASPECT_WIDTH, DefinitionList, Dialog, Diamond, Divider, ExceedConstrain, Expandable, Field, FocusOn, FormControlContext, Field as Input, Keyhole, Label, LineAboveDivider, Loading, LoadingBalls, LoadingItems1, LoadingItems2, LoadingItems3, LoadingItems4, LoadingItems5, LoadingItemsLavenderBlue1, LoadingItemsLavenderBlue2, LoadingItemsLavenderBlue3, LoadingItemsLavenderBlue4, LoadingItemsLavenderBlue5, LoadingShapes, LoadingShapesLavenderBlue, LoadingShapesPinkGreen, Mirror, Modal, ModalOverlay, MultiSelect, NoLineDivider, Oval, Overlay, POPOVER_MOUSEOUT_DELAY_MS, Paginator, PasswordInput, Popover, PromoBanner, RadioButton, SKU_COLORS, SearchInput, Select, SelectableButton, SlidingPane, Stack, Tab, TabList, TabPanel, TabPanels, Tabs, Tag, Text, TextInput, Textarea, Toast, Triangle, TwoLineDivider, UNMOUNT_DELAY_MS, Utilities, VerticalDivider, Window, discriminate, tokens, useBreakpoint, useClickOutside, useConfirm, useFormControlContext, useKeyboardListNavigation, useUpdateEffect };
13086
+ export { ArcWindow, AspectRatioBox, Badge, Bradley, Brancusi, Breadcrumb, Breadcrumbs, Breakpoint, BreakpointContext, BreakpointProvider, Button, Checkbox, Circle, Clickable, ColorSwatch, ConfirmProvider, Constrain, ControlLink, CreditCardNumberInput, DEFAULT_PRODUCT_ASPECT_HEIGHT, DEFAULT_PRODUCT_ASPECT_RATIO, DEFAULT_PRODUCT_ASPECT_WIDTH, DefinitionList, Dialog, Diamond, Divider, ExceedConstrain, Expandable, Field, FocusOn, FormControlContext, Field as Input, Keyhole, Label, LineAboveDivider, Loading, LoadingBalls, LoadingItems1, LoadingItems2, LoadingItems3, LoadingItems4, LoadingItems5, LoadingItemsLavenderBlue1, LoadingItemsLavenderBlue2, LoadingItemsLavenderBlue3, LoadingItemsLavenderBlue4, LoadingItemsLavenderBlue5, LoadingShapes, LoadingShapesLavenderBlue, LoadingShapesPinkGreen, Mirror, Modal, ModalOverlay, MultiSelect, NoLineDivider, OtpInput, Oval, Overlay, POPOVER_MOUSEOUT_DELAY_MS, Paginator, PasswordInput, Popover, PromoBanner, RadioButton, SKU_COLORS, SearchInput, Select, SelectableButton, SlidingPane, Stack, Tab, TabList, TabPanel, TabPanels, Tabs, Tag, Text, TextInput, Textarea, Toast, Triangle, TwoLineDivider, UNMOUNT_DELAY_MS, Utilities, VerticalDivider, Window, discriminate, tokens, useBreakpoint, useClickOutside, useConfirm, useFormControlContext, useKeyboardListNavigation, useUpdateEffect };