2024-03-28 14:05:58 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { useEvent } from 'rc-util';
|
|
|
|
import pickAttrs from 'rc-util/lib/pickAttrs';
|
|
|
|
|
2024-04-08 14:04:08 +08:00
|
|
|
import { getMergedStatus } from '../../_util/statusUtils';
|
|
|
|
import type { InputStatus } from '../../_util/statusUtils';
|
2024-04-09 14:47:58 +08:00
|
|
|
import { devUseWarning } from '../../_util/warning';
|
2024-03-28 14:05:58 +08:00
|
|
|
import { ConfigContext } from '../../config-provider';
|
|
|
|
import useCSSVarCls from '../../config-provider/hooks/useCSSVarCls';
|
|
|
|
import useSize from '../../config-provider/hooks/useSize';
|
2024-04-08 14:04:08 +08:00
|
|
|
import type { SizeType } from '../../config-provider/SizeContext';
|
2024-03-28 14:05:58 +08:00
|
|
|
import { FormItemInputContext } from '../../form/context';
|
2024-04-09 14:47:58 +08:00
|
|
|
import type { FormItemStatusContextProps } from '../../form/context';
|
2024-06-21 17:47:09 +08:00
|
|
|
import type { Variant } from '../../config-provider';
|
2024-04-08 14:04:08 +08:00
|
|
|
import type { InputRef } from '../Input';
|
2024-03-28 14:05:58 +08:00
|
|
|
import useStyle from '../style/otp';
|
2024-04-08 14:04:08 +08:00
|
|
|
import OTPInput from './OTPInput';
|
|
|
|
import type { OTPInputProps } from './OTPInput';
|
2024-03-28 14:05:58 +08:00
|
|
|
|
|
|
|
export interface OTPRef {
|
|
|
|
focus: VoidFunction;
|
|
|
|
blur: VoidFunction;
|
|
|
|
nativeElement: HTMLDivElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface OTPProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
|
|
|
|
prefixCls?: string;
|
|
|
|
length?: number;
|
|
|
|
|
|
|
|
// Style
|
|
|
|
variant?: Variant;
|
|
|
|
rootClassName?: string;
|
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
size?: SizeType;
|
|
|
|
|
|
|
|
// Values
|
|
|
|
defaultValue?: string;
|
|
|
|
value?: string;
|
|
|
|
onChange?: (value: string) => void;
|
|
|
|
formatter?: (value: string) => string;
|
|
|
|
|
|
|
|
// Status
|
|
|
|
disabled?: boolean;
|
|
|
|
status?: InputStatus;
|
2024-04-09 14:47:58 +08:00
|
|
|
|
|
|
|
mask?: boolean | string;
|
2024-03-28 14:05:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function strToArr(str: string) {
|
2024-04-11 15:24:18 +08:00
|
|
|
return (str || '').split('');
|
2024-03-28 14:05:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const OTP = React.forwardRef<OTPRef, OTPProps>((props, ref) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
length = 6,
|
|
|
|
size: customSize,
|
|
|
|
defaultValue,
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
formatter,
|
|
|
|
variant,
|
|
|
|
disabled,
|
|
|
|
status: customStatus,
|
|
|
|
autoFocus,
|
2024-04-09 14:47:58 +08:00
|
|
|
mask,
|
2024-03-28 14:05:58 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
2024-04-09 14:47:58 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
const warning = devUseWarning('Input.OTP');
|
|
|
|
warning(
|
|
|
|
!(typeof mask === 'string' && mask.length > 1),
|
|
|
|
'usage',
|
|
|
|
'`mask` prop should be a single character.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:05:58 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('otp', customizePrefixCls);
|
|
|
|
|
|
|
|
const domAttrs = pickAttrs(restProps, {
|
|
|
|
aria: true,
|
|
|
|
data: true,
|
|
|
|
attr: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// ========================= Root =========================
|
|
|
|
// Style
|
|
|
|
const rootCls = useCSSVarCls(prefixCls);
|
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
|
|
|
|
|
|
|
|
// ========================= Size =========================
|
|
|
|
const mergedSize = useSize((ctx) => customSize ?? ctx);
|
|
|
|
|
|
|
|
// ======================== Status ========================
|
|
|
|
const formContext = React.useContext(FormItemInputContext);
|
|
|
|
const mergedStatus = getMergedStatus(formContext.status, customStatus);
|
|
|
|
|
2024-04-09 14:47:58 +08:00
|
|
|
const proxyFormContext = React.useMemo<FormItemStatusContextProps>(
|
2024-03-28 14:05:58 +08:00
|
|
|
() => ({
|
|
|
|
...formContext,
|
|
|
|
status: mergedStatus,
|
|
|
|
hasFeedback: false,
|
|
|
|
feedbackIcon: null,
|
|
|
|
}),
|
|
|
|
[formContext, mergedStatus],
|
|
|
|
);
|
|
|
|
|
|
|
|
// ========================= Refs =========================
|
|
|
|
const containerRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
const refs = React.useRef<Record<number, InputRef | null>>({});
|
|
|
|
|
|
|
|
React.useImperativeHandle(ref, () => ({
|
|
|
|
focus: () => {
|
|
|
|
refs.current[0]?.focus();
|
|
|
|
},
|
|
|
|
blur: () => {
|
|
|
|
for (let i = 0; i < length; i += 1) {
|
|
|
|
refs.current[i]?.blur();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
nativeElement: containerRef.current!,
|
|
|
|
}));
|
|
|
|
|
|
|
|
// ======================= Formatter ======================
|
|
|
|
const internalFormatter = (txt: string) => (formatter ? formatter(txt) : txt);
|
|
|
|
|
|
|
|
// ======================== Values ========================
|
|
|
|
const [valueCells, setValueCells] = React.useState<string[]>(
|
|
|
|
strToArr(internalFormatter(defaultValue || '')),
|
|
|
|
);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2024-04-11 15:24:18 +08:00
|
|
|
if (value !== undefined) {
|
2024-03-28 14:05:58 +08:00
|
|
|
setValueCells(strToArr(value));
|
|
|
|
}
|
|
|
|
}, [value]);
|
|
|
|
|
|
|
|
const triggerValueCellsChange = useEvent((nextValueCells: string[]) => {
|
|
|
|
setValueCells(nextValueCells);
|
|
|
|
|
|
|
|
// Trigger if all cells are filled
|
|
|
|
if (
|
|
|
|
onChange &&
|
|
|
|
nextValueCells.length === length &&
|
|
|
|
nextValueCells.every((c) => c) &&
|
|
|
|
nextValueCells.some((c, index) => valueCells[index] !== c)
|
|
|
|
) {
|
|
|
|
onChange(nextValueCells.join(''));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const patchValue = useEvent((index: number, txt: string) => {
|
|
|
|
let nextCells = [...valueCells];
|
|
|
|
|
|
|
|
// Fill cells till index
|
|
|
|
for (let i = 0; i < index; i += 1) {
|
|
|
|
if (!nextCells[i]) {
|
|
|
|
nextCells[i] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (txt.length <= 1) {
|
|
|
|
nextCells[index] = txt;
|
|
|
|
} else {
|
|
|
|
nextCells = nextCells.slice(0, index).concat(strToArr(txt));
|
|
|
|
}
|
|
|
|
nextCells = nextCells.slice(0, length);
|
|
|
|
|
|
|
|
// Clean the last empty cell
|
|
|
|
for (let i = nextCells.length - 1; i >= 0; i -= 1) {
|
|
|
|
if (nextCells[i]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nextCells.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format if needed
|
|
|
|
const formattedValue = internalFormatter(nextCells.map((c) => c || ' ').join(''));
|
|
|
|
nextCells = strToArr(formattedValue).map((c, i) => {
|
|
|
|
if (c === ' ' && !nextCells[i]) {
|
|
|
|
return nextCells[i];
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
});
|
|
|
|
|
|
|
|
return nextCells;
|
|
|
|
});
|
|
|
|
|
|
|
|
// ======================== Change ========================
|
|
|
|
const onInputChange: OTPInputProps['onChange'] = (index, txt) => {
|
|
|
|
const nextCells = patchValue(index, txt);
|
|
|
|
|
|
|
|
const nextIndex = Math.min(index + txt.length, length - 1);
|
|
|
|
if (nextIndex !== index) {
|
|
|
|
refs.current[nextIndex]?.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
triggerValueCellsChange(nextCells);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onInputActiveChange: OTPInputProps['onActiveChange'] = (nextIndex) => {
|
|
|
|
refs.current[nextIndex]?.focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
// ======================== Render ========================
|
2024-04-09 14:47:58 +08:00
|
|
|
const inputSharedProps: Partial<OTPInputProps> = {
|
2024-03-28 14:05:58 +08:00
|
|
|
variant,
|
|
|
|
disabled,
|
|
|
|
status: mergedStatus as InputStatus,
|
2024-04-09 14:47:58 +08:00
|
|
|
mask,
|
2024-03-28 14:05:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return wrapCSSVar(
|
|
|
|
<div
|
|
|
|
{...domAttrs}
|
|
|
|
ref={containerRef}
|
|
|
|
className={classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-sm`]: mergedSize === 'small',
|
|
|
|
[`${prefixCls}-lg`]: mergedSize === 'large',
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
cssVarCls,
|
|
|
|
hashId,
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<FormItemInputContext.Provider value={proxyFormContext}>
|
2024-04-09 14:47:58 +08:00
|
|
|
{Array.from({ length }).map((_, index) => {
|
2024-03-28 14:05:58 +08:00
|
|
|
const key = `otp-${index}`;
|
|
|
|
const singleValue = valueCells[index] || '';
|
|
|
|
return (
|
|
|
|
<OTPInput
|
|
|
|
ref={(inputEle) => {
|
|
|
|
refs.current[index] = inputEle;
|
|
|
|
}}
|
|
|
|
key={key}
|
|
|
|
index={index}
|
|
|
|
size={mergedSize}
|
|
|
|
htmlSize={1}
|
|
|
|
className={`${prefixCls}-input`}
|
|
|
|
onChange={onInputChange}
|
|
|
|
value={singleValue}
|
|
|
|
onActiveChange={onInputActiveChange}
|
|
|
|
autoFocus={index === 0 && autoFocus}
|
|
|
|
{...inputSharedProps}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</FormItemInputContext.Provider>
|
|
|
|
</div>,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default OTP;
|