UNPKG

antd

Version:

An enterprise-class UI design language and React components implementation

120 lines (119 loc) • 3.59 kB
"use client"; import * as React from 'react'; import { raf } from '@rc-component/util'; import { clsx } from 'clsx'; import { ConfigContext } from '../../config-provider'; import Input from '../Input'; const DEFAULT_MASK_VALUE = '•'; const OTPInput = /*#__PURE__*/React.forwardRef((props, ref) => { const { className, value, onChange, onActiveChange, index, mask, onFocus, onCompositionStart, onCompositionEnd, type, ...restProps } = props; const { getPrefixCls } = React.useContext(ConfigContext); const prefixCls = getPrefixCls('otp'); const maskValue = typeof mask === 'string' ? mask : DEFAULT_MASK_VALUE; // ========================== Ref =========================== const inputRef = React.useRef(null); const composingRef = React.useRef(false); const composedValueRef = React.useRef(null); React.useImperativeHandle(ref, () => inputRef.current); // ========================= Input ========================== const onInternalChange = e => { if (composingRef.current || e.nativeEvent.isComposing) { return; } const nextValue = e.target.value; if (composedValueRef.current !== null) { const composedValue = composedValueRef.current; composedValueRef.current = null; if (nextValue === composedValue) { return; } } onChange(index, nextValue); }; const onInternalCompositionStart = e => { composingRef.current = true; composedValueRef.current = null; onCompositionStart?.(e); }; const onInternalCompositionEnd = e => { composingRef.current = false; const nextValue = e.target.value; composedValueRef.current = nextValue; if (nextValue !== value) { onChange(index, nextValue); } raf(() => { composedValueRef.current = null; }); onCompositionEnd?.(e); }; // ========================= Focus ========================== const syncSelection = () => { raf(() => { const inputEle = inputRef.current?.input; if (document.activeElement === inputEle && inputEle) { inputEle.select(); } }); }; const onInternalFocus = e => { onFocus?.(e); syncSelection(); }; // ======================== Keyboard ======================== const onInternalKeyDown = event => { const { key, ctrlKey, metaKey } = event; if (key === 'ArrowLeft') { onActiveChange(index - 1); } else if (key === 'ArrowRight') { onActiveChange(index + 1); } else if (key === 'z' && (ctrlKey || metaKey)) { event.preventDefault(); } else if (key === 'Backspace' && !value) { onActiveChange(index - 1); } syncSelection(); }; // ========================= Render ========================= return /*#__PURE__*/React.createElement("span", { className: `${prefixCls}-input-wrapper`, role: "presentation" }, mask && value !== '' && value !== undefined && (/*#__PURE__*/React.createElement("span", { className: `${prefixCls}-mask-icon`, "aria-hidden": "true" }, maskValue)), /*#__PURE__*/React.createElement(Input, { ...restProps, type: type ?? (mask ? 'password' : 'text'), ref: inputRef, value: value, onInput: onInternalChange, onCompositionStart: onInternalCompositionStart, onCompositionEnd: onInternalCompositionEnd, onFocus: onInternalFocus, onKeyDown: onInternalKeyDown, onMouseDown: syncSelection, onMouseUp: syncSelection, className: clsx(className, { [`${prefixCls}-mask-input`]: mask }) })); }); export default OTPInput;