feat(FE-Storyless): integrate NumberInput and PatternInput components with react-number-format for enhanced input handling

This commit is contained in:
rstubryan
2025-10-25 10:49:07 +07:00
parent 896a0c6de2
commit 6290199074
4 changed files with 104 additions and 394 deletions
+60
View File
@@ -0,0 +1,60 @@
'use client';
import { ChangeEvent } from 'react';
import { PatternFormat, OnValueChange } from 'react-number-format';
import TextInput, { TextInputProps } from '@/components/input/TextInput';
interface PatternInputProps extends Omit<TextInputProps, 'type'> {
type?: 'password' | 'tel' | 'text' | undefined;
/** Format pattern, e.g. "##/##/####", "(###) ###-####", "####-####-####" */
format: string;
/** Mask character for empty slots, e.g. "_" */
mask?: string;
/** Allow showing mask even when value is empty */
allowEmptyFormatting?: boolean;
patternChar?: string;
}
const PatternInput = ({
type = 'text',
format,
mask = '_',
allowEmptyFormatting = false,
patternChar = '#',
onChange,
...restProps
}: PatternInputProps) => {
const valueChangeHandler: OnValueChange = (
patternFormatValues,
sourceInfo
) => {
const newChangeEvent = sourceInfo.event as
| ChangeEvent<HTMLInputElement>
| undefined;
if (newChangeEvent) {
newChangeEvent.target.value = patternFormatValues.value;
onChange?.(newChangeEvent);
}
};
return (
<PatternFormat
type={type}
format={format}
mask={mask}
allowEmptyFormatting={allowEmptyFormatting}
patternChar={patternChar}
customInput={TextInput}
onValueChange={valueChangeHandler}
{...restProps}
/>
);
};
export default PatternInput;