OTPInput.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. "use client";
  3. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  4. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  5. Object.defineProperty(exports, "__esModule", {
  6. value: true
  7. });
  8. exports.default = void 0;
  9. var React = _interopRequireWildcard(require("react"));
  10. var _classnames = _interopRequireDefault(require("classnames"));
  11. var _raf = _interopRequireDefault(require("rc-util/lib/raf"));
  12. var _configProvider = require("../../config-provider");
  13. var _Input = _interopRequireDefault(require("../Input"));
  14. var __rest = void 0 && (void 0).__rest || function (s, e) {
  15. var t = {};
  16. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  17. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  18. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  19. }
  20. return t;
  21. };
  22. const OTPInput = /*#__PURE__*/React.forwardRef((props, ref) => {
  23. const {
  24. className,
  25. value,
  26. onChange,
  27. onActiveChange,
  28. index,
  29. mask
  30. } = props,
  31. restProps = __rest(props, ["className", "value", "onChange", "onActiveChange", "index", "mask"]);
  32. const {
  33. getPrefixCls
  34. } = React.useContext(_configProvider.ConfigContext);
  35. const prefixCls = getPrefixCls('otp');
  36. const maskValue = typeof mask === 'string' ? mask : value;
  37. // ========================== Ref ===========================
  38. const inputRef = React.useRef(null);
  39. React.useImperativeHandle(ref, () => inputRef.current);
  40. // ========================= Input ==========================
  41. const onInternalChange = e => {
  42. onChange(index, e.target.value);
  43. };
  44. // ========================= Focus ==========================
  45. const syncSelection = () => {
  46. (0, _raf.default)(() => {
  47. var _a;
  48. const inputEle = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.input;
  49. if (document.activeElement === inputEle && inputEle) {
  50. inputEle.select();
  51. }
  52. });
  53. };
  54. // ======================== Keyboard ========================
  55. const onInternalKeyDown = event => {
  56. const {
  57. key,
  58. ctrlKey,
  59. metaKey
  60. } = event;
  61. if (key === 'ArrowLeft') {
  62. onActiveChange(index - 1);
  63. } else if (key === 'ArrowRight') {
  64. onActiveChange(index + 1);
  65. } else if (key === 'z' && (ctrlKey || metaKey)) {
  66. event.preventDefault();
  67. } else if (key === 'Backspace' && !value) {
  68. onActiveChange(index - 1);
  69. }
  70. syncSelection();
  71. };
  72. // ========================= Render =========================
  73. return /*#__PURE__*/React.createElement("span", {
  74. className: `${prefixCls}-input-wrapper`,
  75. role: "presentation"
  76. }, mask && value !== '' && value !== undefined && (/*#__PURE__*/React.createElement("span", {
  77. className: `${prefixCls}-mask-icon`,
  78. "aria-hidden": "true"
  79. }, maskValue)), /*#__PURE__*/React.createElement(_Input.default, Object.assign({
  80. "aria-label": `OTP Input ${index + 1}`,
  81. type: mask === true ? 'password' : 'text'
  82. }, restProps, {
  83. ref: inputRef,
  84. value: value,
  85. onInput: onInternalChange,
  86. onFocus: syncSelection,
  87. onKeyDown: onInternalKeyDown,
  88. onMouseDown: syncSelection,
  89. onMouseUp: syncSelection,
  90. className: (0, _classnames.default)(className, {
  91. [`${prefixCls}-mask-input`]: mask
  92. })
  93. })));
  94. });
  95. var _default = exports.default = OTPInput;